diff --git a/tool/src/json_map.dart b/tool/src/json_map.dart new file mode 100644 index 0000000..685f03a --- /dev/null +++ b/tool/src/json_map.dart @@ -0,0 +1,85 @@ +/// Type definition for JSON data. +typedef JsonMap = Map; + +/// Type definition of a [JsonMap] key. +typedef JsonMapEntry = MapEntry; + +/// Helpers for accessing JSON data. +extension JsonMapValue on JsonMap { + /// Retrieve a [bool] at the given [key]. + /// + /// Returns [defaultTo] if the [key] is not present. + bool getBool(String key, {bool defaultTo = false}) => + this[key] as bool? ?? defaultTo; + + /// Retrieve a [String] at the given [key]. + /// + /// Returns [defaultTo] if the [key] is not present. + String getString(String key, {String defaultTo = ''}) => + this[key] as String? ?? defaultTo; + + /// Retrieves a [List] at the given [key]. + /// + /// Single values of [T] are turned into a [List] containing a single value. + /// If the [key] is not present an empty [List] is returned. + List getList(String key, {List defaultTo = const []}) { + final value = this[key]; + + if (value == null) { + return defaultTo; + } + + if (value is List) { + return value; + } + + return value is List ? value.cast() : [value as T]; + } + + /// Retrieves a [Map] at the given [key]. + /// + /// If the [key] is not present an empty [Map] is returned. + Map getMap(String key, {Map defaultTo = const {}}) { + final value = this[key] as Map?; + + if (value == null) { + return defaultTo; + } + + if (value is Map) { + return value; + } + + return value.cast(); + } + + /// Retrieve a [List] of [Map]s at the given [key]. + /// + /// If the [key] is not present an empty [Map] is returned. + List> getMapList( + String key, { + List> defaultTo = const [], + }) { + final list = this[key] as List?; + + if (list == null) { + return defaultTo; + } + + if (list is List>) { + return list; + } + + return list.map((m) => (m as Map).cast()).toList(); + } + + /// Retrieve a [JsonMap] at the given [key]. + /// + /// Uses [getMap] with the expected type parameters. + JsonMap getJson(String key) => getMap(key); + + /// Retrieve a [List] of [JsonMap]s at the given [key]. + /// + /// Uses [getMap] with the expected type parameters. + List getJsonList(String key) => getMapList(key); +} diff --git a/tool/src/schema.dart b/tool/src/schema.dart new file mode 100644 index 0000000..2ee66c2 --- /dev/null +++ b/tool/src/schema.dart @@ -0,0 +1,32 @@ +/// A set of `extension`s over the underlying JSON data that presents a typed +/// API for the OpenAPI specification. +library; + +import 'json_map.dart'; + +/// Extension presenting the root of the OpenAPI schema. +extension OpenApiSchema on JsonMap { + static const String _openApi = 'openapi'; + static const String _info = 'info'; + + /// The OpenAPI version. + String get openApiVersion => getString(_openApi); + + /// Retrieves the [OpenApiInfoSchema]. + JsonMap get info => getJson(_info); +} + +/// Extension presenting the `info` schema. +/// +/// Corresponds to +/// info: +extension OpenApiInfoSchema on JsonMap { + static const String _title = 'title'; + static const String _version = 'version'; + + /// The title of the schema definition. + String get title => getString(_title); + + /// The version of the schema definition. + String get version => getString(_version); +}