|
1 | 1 | import 'dart:mirrors';
|
2 | 2 |
|
3 | 3 | import 'package:analyzer/dart/element/type.dart';
|
| 4 | +import 'package:openapi_generator/src/utils.dart'; |
| 5 | +import 'package:openapi_generator_annotations/openapi_generator_annotations.dart'; |
4 | 6 | import 'package:source_gen/source_gen.dart' show ConstantReader, TypeChecker;
|
5 | 7 |
|
6 | 8 | /// Extension adding the type methods to `ConstantReader`.
|
@@ -71,3 +73,100 @@ extension TypeMethods on ConstantReader {
|
71 | 73 | return values[enumIndex];
|
72 | 74 | }
|
73 | 75 | }
|
| 76 | + |
| 77 | +extension ReadProperty on ConstantReader { |
| 78 | + T readPropertyOrDefault<T>(String name, T defaultValue) { |
| 79 | + final v = peek(name); |
| 80 | + if (v == null) { |
| 81 | + return defaultValue; |
| 82 | + } |
| 83 | + |
| 84 | + if (isA(v, InputSpec)) { |
| 85 | + final revived = v.revive(); |
| 86 | + |
| 87 | + if (isA(v, RemoteSpec)) { |
| 88 | + final map = revived.namedArguments; |
| 89 | + final delegate = map['headerDelegate']; |
| 90 | + final mapped = <String, dynamic>{ |
| 91 | + 'path': convertToPropertyValue(map['path']!), |
| 92 | + }; |
| 93 | + if (delegate?.isNull ?? true) { |
| 94 | + return RemoteSpec.fromMap(mapped) as T; |
| 95 | + } else { |
| 96 | + final delegateReader = ConstantReader(delegate); |
| 97 | + if (isA(delegateReader, AWSRemoteSpecHeaderDelegate)) { |
| 98 | + mapped['headerDelegate'] = AWSRemoteSpecHeaderDelegate.fromMap( |
| 99 | + delegateReader.revive().namedArguments.map( |
| 100 | + (key, value) => MapEntry( |
| 101 | + key, |
| 102 | + convertToPropertyValue(value), |
| 103 | + ), |
| 104 | + ), |
| 105 | + ); |
| 106 | + } |
| 107 | + return RemoteSpec.fromMap(mapped) as T; |
| 108 | + } |
| 109 | + } else { |
| 110 | + final map = revived.namedArguments.map( |
| 111 | + (key, value) => MapEntry( |
| 112 | + key, |
| 113 | + convertToPropertyValue(value), |
| 114 | + ), |
| 115 | + ); |
| 116 | + return InputSpec.fromMap(map) as T; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (isA(v, AdditionalProperties)) { |
| 121 | + final map = v.revive().namedArguments.map( |
| 122 | + (key, value) => MapEntry( |
| 123 | + key, |
| 124 | + convertToPropertyValue(value), |
| 125 | + ), |
| 126 | + ); |
| 127 | + if (isA(v, DioProperties)) { |
| 128 | + return DioProperties.fromMap(map) as T; |
| 129 | + } else if (isA(v, DioAltProperties)) { |
| 130 | + return DioAltProperties.fromMap(map) as T; |
| 131 | + } else { |
| 132 | + return AdditionalProperties.fromMap(map) as T; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (isA(v, InlineSchemaOptions)) { |
| 137 | + return InlineSchemaOptions.fromMap( |
| 138 | + v.revive().namedArguments.map( |
| 139 | + (key, value) => MapEntry( |
| 140 | + key, |
| 141 | + convertToPropertyValue(value), |
| 142 | + ), |
| 143 | + ), |
| 144 | + ) as T; |
| 145 | + } |
| 146 | + |
| 147 | + if (isA(v, Map<String, String>)) { |
| 148 | + return v.mapValue.map((key, value) => MapEntry( |
| 149 | + convertToPropertyValue(key!) as String, |
| 150 | + convertToPropertyValue(value!) as String)) as T; |
| 151 | + } else if (isA(v, bool)) { |
| 152 | + return v.boolValue as T; |
| 153 | + } else if (isA(v, double)) { |
| 154 | + return v.doubleValue as T; |
| 155 | + } else if (isA(v, int)) { |
| 156 | + return v.intValue as T; |
| 157 | + } else if (isA(v, String)) { |
| 158 | + return v.stringValue as T; |
| 159 | + } else if (isA(v, Set)) { |
| 160 | + return v.setValue.map(convertToPropertyValue) as T; |
| 161 | + } else if (isA(v, List)) { |
| 162 | + return v.listValue.map(convertToPropertyValue) as T; |
| 163 | + } else if (isA(v, Enum)) { |
| 164 | + return v.enumValue(); |
| 165 | + } else { |
| 166 | + return defaultValue; |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +bool isA(ConstantReader? v, Type t) => |
| 172 | + v?.instanceOf(TypeChecker.fromRuntime(t)) ?? false; |
0 commit comments