Skip to content

Commit

Permalink
refactor(dynamite): split JsonSchema into per type classes
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <[email protected]>
  • Loading branch information
Leptopoda committed Apr 27, 2024
1 parent 79aa887 commit ffa5b32
Show file tree
Hide file tree
Showing 18 changed files with 4,517 additions and 499 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Spec buildBuiltClassSerializer(
State state,
String identifier,
openapi.OpenAPI spec,
json_schema.JsonSchema schema,
json_schema.ObjectSchema schema,
) =>
Class(
(b) => b
Expand Down Expand Up @@ -137,7 +137,7 @@ Iterable<String> deserializeProperty(
State state,
String identifier,
openapi.OpenAPI spec,
json_schema.JsonSchema schema,
json_schema.ObjectSchema schema,
) sync* {
for (final property in schema.properties!.entries) {
final propertyName = property.key;
Expand Down Expand Up @@ -169,7 +169,7 @@ Iterable<String> serializePropertyNullable(
State state,
String identifier,
openapi.OpenAPI spec,
json_schema.JsonSchema schema,
json_schema.ObjectSchema schema,
) sync* {
for (final property in schema.properties!.entries) {
final propertyName = property.key;
Expand Down Expand Up @@ -204,7 +204,7 @@ Iterable<String> serializeProperty(
State state,
String identifier,
openapi.OpenAPI spec,
json_schema.JsonSchema schema,
json_schema.ObjectSchema schema,
) sync* {
for (final property in schema.properties!.entries) {
final propertyName = property.key;
Expand Down
2 changes: 1 addition & 1 deletion packages/dynamite/dynamite/lib/src/builder/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ return ${allocate(responseType)}.fromRawResponse(_rawResponse);
spec,
state,
identifierBuilder.toString(),
json_schema.JsonSchema(
json_schema.ObjectSchema(
(b) => b
..properties.replace(
response.headers!.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Spec buildInterface(

if (schema case json_schema.JsonSchema(:final allOf) when allOf != null) {
for (final schema in allOf) {
if (schema case json_schema.JsonSchema(properties: != null)) {
if (schema case json_schema.ObjectSchema(properties: != null)) {
_generateProperties(
schema,
spec,
Expand Down Expand Up @@ -74,7 +74,7 @@ Spec buildInterface(
}
} else {
_generateProperties(
schema,
schema as json_schema.ObjectSchema,
spec,
state,
identifier,
Expand Down Expand Up @@ -136,7 +136,7 @@ Spec buildInterface(
}

void _generateProperties(
json_schema.JsonSchema schema,
json_schema.ObjectSchema schema,
openapi.OpenAPI spec,
State state,
String identifier,
Expand Down
25 changes: 12 additions & 13 deletions packages/dynamite/dynamite/lib/src/builder/resolve_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ TypeResult _resolveType(
nullable: nullable,
);

case json_schema.JsonSchema(type: null, ref: null, allOf: null, anyOf: null, oneOf: null):
case json_schema.GenericSchema(ref: null, allOf: null, anyOf: null, oneOf: null):
return TypeResultBase(
'JsonObject',
nullable: nullable,
Expand All @@ -94,7 +94,7 @@ TypeResult _resolveType(
nullable: nullable,
);

case json_schema.JsonSchema(isContentString: true):
case json_schema.StringSchema(isContentString: true):
final subResult = resolveType(
spec,
state,
Expand All @@ -108,43 +108,43 @@ TypeResult _resolveType(
nullable: nullable,
);

case json_schema.JsonSchema(type: json_schema.JsonSchemaType.boolean):
case json_schema.BooleanSchema():
return TypeResultBase(
'bool',
nullable: nullable,
);

case json_schema.JsonSchema(type: json_schema.JsonSchemaType.integer):
case json_schema.IntegerSchema():
return TypeResultBase(
'int',
nullable: nullable,
);

case json_schema.JsonSchema(type: json_schema.JsonSchemaType.number, format: 'float' || 'double'):
case json_schema.NumberSchema(format: 'float' || 'double'):
return TypeResultBase(
'double',
nullable: nullable,
);

case json_schema.JsonSchema(type: json_schema.JsonSchemaType.number):
case json_schema.NumberSchema():
return TypeResultBase(
'num',
nullable: nullable,
);

case json_schema.JsonSchema(type: json_schema.JsonSchemaType.string, format: 'binary'):
case json_schema.StringSchema(format: 'binary'):
return TypeResultBase(
'Uint8List',
nullable: nullable,
);

case json_schema.JsonSchema(type: json_schema.JsonSchemaType.string):
case json_schema.StringSchema():
return TypeResultBase(
'String',
nullable: nullable,
);

case json_schema.JsonSchema(type: json_schema.JsonSchemaType.array):
case json_schema.ArraySchema():
final TypeResult subResult;
if (schema.maxItems == 0) {
subResult = TypeResultBase('Never');
Expand All @@ -165,7 +165,7 @@ TypeResult _resolveType(
nullable: nullable,
);

case json_schema.JsonSchema(type: json_schema.JsonSchemaType.object, properties: null):
case json_schema.ObjectSchema(properties: null):
if (schema.additionalProperties == null) {
return TypeResultBase(
'JsonObject',
Expand All @@ -185,15 +185,14 @@ TypeResult _resolveType(
);
}

case json_schema.JsonSchema(type: json_schema.JsonSchemaType.object, :final properties)
when properties != null && properties.isEmpty:
case json_schema.ObjectSchema(:final properties) when properties != null && properties.isEmpty:
return TypeResultMap(
'BuiltMap',
TypeResultBase('JsonObject'),
nullable: nullable,
);

case json_schema.JsonSchema(type: json_schema.JsonSchemaType.object):
case json_schema.ObjectSchema():
return resolveObject(
spec,
state,
Expand Down
32 changes: 17 additions & 15 deletions packages/dynamite/dynamite/lib/src/helpers/pattern_check.dart
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
import 'package:code_builder/code_builder.dart';
import 'package:dynamite/src/models/json_schema.dart' as json_schema;
import 'package:dynamite/src/models/json_schema.dart';

Iterable<Expression> buildPatternCheck(
json_schema.JsonSchema schema,
Validator schema,
String value,
String name,
) sync* {
switch (schema.type) {
case json_schema.JsonSchemaType.string:
if (schema.pattern != null) {
switch (schema) {
case StringValidator():
if (schema case StringValidator(:final pattern) when pattern != null) {
yield refer('checkPattern', 'package:dynamite_runtime/utils.dart').call([
refer(value).asA(refer('String?')),
refer('RegExp').call([literalString(schema.pattern!, raw: true)]),
refer('RegExp').call([literalString(pattern.pattern, raw: true)]),
literalString(name),
]);
}
if (schema.minLength != null) {
if (schema case StringValidator(:final minLength) when minLength != null) {
yield refer('checkMinLength', 'package:dynamite_runtime/utils.dart').call([
refer(value).asA(refer('String?')),
literalNum(schema.minLength!),
literalNum(minLength),
literalString(name),
]);
}
if (schema.maxLength != null) {
if (schema case StringValidator(:final maxLength) when maxLength != null) {
yield refer('checkMaxLength', 'package:dynamite_runtime/utils.dart').call([
refer(value).asA(refer('String?')),
literalNum(schema.maxLength!),
literalNum(maxLength),
literalString(name),
]);
}
case json_schema.JsonSchemaType.array:
if (schema.minItems != null) {

case ArrayValidator():
if (schema case ArrayValidator(:final minItems) when minItems != null) {
yield refer('checkMinItems', 'package:dynamite_runtime/utils.dart').call([
refer(value).nullSafeProperty('length'),
literalNum(schema.minItems!),
literalNum(minItems),
literalString(name),
]);
}
if (schema.maxItems != null) {
if (schema case ArrayValidator(:final maxItems) when maxItems != null) {
yield refer('checkMaxItems', 'package:dynamite_runtime/utils.dart').call([
refer(value).nullSafeProperty('length'),
literalNum(schema.maxItems!),
literalNum(maxItems),
literalString(name),
]);
}

default:
break;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/dynamite/dynamite/lib/src/models/json_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ import 'package:dynamite/src/models/json_schema.dart';
import 'package:dynamite/src/models/openapi/discriminator.dart' show Discriminator;

export 'json_schema/schema.dart';
export 'json_schema/validators.dart';

part 'json_schema.g.dart';

@SerializersFor([
JsonSchema,
GenericSchema,
BooleanSchema,
IntegerSchema,
NumberSchema,
StringSchema,
ArraySchema,
ObjectSchema,
NullSchema,
JsonSchemaType,
])
final Serializers serializers = (_$serializers.toBuilder()
..add(JsonSchema.serializer)
..addBuilderFactory(
const FullType(BuiltMap, [
FullType(String),
Expand Down
40 changes: 38 additions & 2 deletions packages/dynamite/dynamite/lib/src/models/json_schema.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ffa5b32

Please sign in to comment.