Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions tool/src/json_map.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/// Type definition for JSON data.
typedef JsonMap = Map<String, Object?>;

/// Type definition of a [JsonMap] key.
typedef JsonMapEntry = MapEntry<String, Object?>;

/// 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<T> getList<T>(String key, {List<T> defaultTo = const []}) {
final value = this[key];

if (value == null) {
return defaultTo;
}

if (value is List<T>) {
return value;
}

return value is List ? value.cast<T>() : <T>[value as T];
}

/// Retrieves a [Map] at the given [key].
///
/// If the [key] is not present an empty [Map] is returned.
Map<K, V> getMap<K, V>(String key, {Map<K, V> defaultTo = const {}}) {
final value = this[key] as Map?;

if (value == null) {
return defaultTo;
}

if (value is Map<K, V>) {
return value;
}

return value.cast<K, V>();
}

/// Retrieve a [List] of [Map]s at the given [key].
///
/// If the [key] is not present an empty [Map] is returned.
List<Map<K, V>> getMapList<K, V>(
String key, {
List<Map<K, V>> defaultTo = const [],
}) {
final list = this[key] as List?;

if (list == null) {
return defaultTo;
}

if (list is List<Map<K, V>>) {
return list;
}

return list.map((m) => (m as Map).cast<K, V>()).toList();
}

/// Retrieve a [JsonMap] at the given [key].
///
/// Uses [getMap] with the expected type parameters.
JsonMap getJson(String key) => getMap<String, Object?>(key);

/// Retrieve a [List] of [JsonMap]s at the given [key].
///
/// Uses [getMap] with the expected type parameters.
List<JsonMap> getJsonList(String key) => getMapList<String, Object?>(key);
}
32 changes: 32 additions & 0 deletions tool/src/schema.dart
Original file line number Diff line number Diff line change
@@ -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);
}
Loading