blob: e448a9bf1ae568a7a47c383561b6ccab7e15711d [file] [log] [blame] [edit]
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:discoveryapis_generator/discoveryapis_generator.dart';
extension RestDescriptionExtension on RestDescription {
Map<String, dynamic> toJsonSorted() {
final output = toJson();
String betterKey(String key) =>
const {'parameters', 'resources', 'schemas'}.contains(key)
? '2-$key'
: '1-$key';
final pairs =
output.entries.toList()
..sort((a, b) => betterKey(a.key).compareTo(betterKey(b.key)));
return {for (var p in pairs) p.key: p.value};
}
void sort() {
if (auth != null) {
if (auth!.oauth2 != null) {
auth!.oauth2!.scopes = _sortMap(auth!.oauth2!.scopes);
}
}
for (var schema in schemas!.values) {
schema.properties = _sortProperties(schema.properties);
}
schemas = _sortMap(schemas!);
resources = _sortResources(resources);
methods = _sortMethods(methods);
parameters = _sortMap(parameters);
}
static Map<String, T>? _sortMap<T>(Map<String, T>? map) {
if (map == null) return null;
return Map.fromEntries(
map.entries.toList()..sort((a, b) => a.key.compareTo(b.key)),
);
}
static Map<String, RestMethod>? _sortMethods(
Map<String, RestMethod>? theMethods,
) {
if (theMethods == null) return null;
for (var value in theMethods.values) {
value.parameters = _sortMap(value.parameters);
}
return _sortMap(theMethods);
}
static Map<String, RestResource>? _sortResources(
Map<String, RestResource>? theResources,
) {
if (theResources == null) return null;
for (var value in theResources.values) {
value.resources = _sortResources(value.resources);
value.methods = _sortMethods(value.methods);
}
return _sortMap(theResources);
}
static Map<String, JsonSchema>? _sortProperties(
Map<String, JsonSchema>? theMethods,
) {
if (theMethods == null) return null;
for (var value in theMethods.values) {
_sortSchema(value);
}
return _sortMap(theMethods);
}
static void _sortSchema(JsonSchema schema) {
if (schema.items != null) {
_sortSchema(schema.items!);
}
schema.properties = _sortProperties(schema.properties);
}
}