Skip to content

Commit

Permalink
Merge pull request #2000 from nextcloud/feat/dynamite/add_built_inter…
Browse files Browse the repository at this point in the history
…face_to_interface_classes

feat(dynamite)!: add `Built` interface to the generated interfaces
  • Loading branch information
Leptopoda committed Jun 21, 2024
2 parents c3116aa + a83cd27 commit 4504ef9
Show file tree
Hide file tree
Showing 40 changed files with 10,463 additions and 4 deletions.
29 changes: 29 additions & 0 deletions packages/dynamite/dynamite/example/lib/petstore.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,15 @@ class $Client extends _i1.DynamiteClient {
sealed class $NewPetInterface {
String get name;
String? get tag;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$NewPetInterfaceBuilder].
$NewPetInterface rebuild(void Function($NewPetInterfaceBuilder) updates);

/// Converts the instance to a builder [$NewPetInterfaceBuilder].
$NewPetInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($NewPetInterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -363,6 +372,17 @@ abstract class NewPet implements $NewPetInterface, Built<NewPet, NewPetBuilder>
@BuiltValue(instantiable: false)
sealed class $PetInterface implements $NewPetInterface {
int get id;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$PetInterfaceBuilder].
@override
$PetInterface rebuild(void Function($PetInterfaceBuilder) updates);

/// Converts the instance to a builder [$PetInterfaceBuilder].
@override
$PetInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($PetInterfaceBuilder b) {
$NewPetInterface._defaults(b);
Expand Down Expand Up @@ -414,6 +434,15 @@ abstract class Pet implements $PetInterface, Built<Pet, PetBuilder> {
sealed class $ErrorInterface {
int get code;
String get message;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$ErrorInterfaceBuilder].
$ErrorInterface rebuild(void Function($ErrorInterfaceBuilder) updates);

/// Converts the instance to a builder [$ErrorInterfaceBuilder].
$ErrorInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($ErrorInterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down
35 changes: 33 additions & 2 deletions packages/dynamite/dynamite/lib/src/builder/resolve_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ Spec buildInterface(
);
}

final builderName = '${className}Builder';

b.methods.add(
Method((b) {
b
..docs.add('''
/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$builderName].''')
..returns = refer(className)
..name = 'rebuild'
..requiredParameters.add(
Parameter((b) {
b
..name = 'updates'
..type = refer('void Function($builderName)');
}),
);
}),
);

b.methods.add(
Method((b) {
b
..docs.add('/// Converts the instance to a builder [$builderName].')
..returns = refer(builderName)
..name = 'toBuilder';
}),
);

b.methods.add(
Method((b) {
b
Expand All @@ -92,7 +123,7 @@ Spec buildInterface(
Parameter(
(b) => b
..name = 'b'
..type = refer('${className}Builder'),
..type = refer(builderName),
),
);
if (defaults.isNotEmpty) {
Expand All @@ -118,7 +149,7 @@ Spec buildInterface(
Parameter(
(b) => b
..name = 'b'
..type = refer('${className}Builder'),
..type = refer(builderName),
),
)
..body = validators.build();
Expand Down
64 changes: 64 additions & 0 deletions packages/dynamite/dynamite_end_to_end_test/lib/all_of.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ sealed class $ObjectAllOfInterface {
String get attribute1AllOf;
@BuiltValueField(wireName: 'attribute2-allOf')
String get attribute2AllOf;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$ObjectAllOfInterfaceBuilder].
$ObjectAllOfInterface rebuild(void Function($ObjectAllOfInterfaceBuilder) updates);

/// Converts the instance to a builder [$ObjectAllOfInterfaceBuilder].
$ObjectAllOfInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($ObjectAllOfInterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -67,6 +76,15 @@ abstract class ObjectAllOf implements $ObjectAllOfInterface, Built<ObjectAllOf,
sealed class $OneObjectAllOfInterface {
@BuiltValueField(wireName: 'attribute-allOf')
String get attributeAllOf;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$OneObjectAllOfInterfaceBuilder].
$OneObjectAllOfInterface rebuild(void Function($OneObjectAllOfInterfaceBuilder) updates);

/// Converts the instance to a builder [$OneObjectAllOfInterfaceBuilder].
$OneObjectAllOfInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($OneObjectAllOfInterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -111,6 +129,15 @@ sealed class $PrimitiveAllOfInterface {
int get $int;
@BuiltValueField(wireName: 'String')
String get string;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$PrimitiveAllOfInterfaceBuilder].
$PrimitiveAllOfInterface rebuild(void Function($PrimitiveAllOfInterfaceBuilder) updates);

/// Converts the instance to a builder [$PrimitiveAllOfInterfaceBuilder].
$PrimitiveAllOfInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($PrimitiveAllOfInterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -155,6 +182,15 @@ sealed class $MixedAllOfInterface {
String get string;
@BuiltValueField(wireName: 'attribute-allOf')
String get attributeAllOf;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$MixedAllOfInterfaceBuilder].
$MixedAllOfInterface rebuild(void Function($MixedAllOfInterfaceBuilder) updates);

/// Converts the instance to a builder [$MixedAllOfInterfaceBuilder].
$MixedAllOfInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($MixedAllOfInterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -197,6 +233,15 @@ abstract class MixedAllOf implements $MixedAllOfInterface, Built<MixedAllOf, Mix
sealed class $OneValueAllOfInterface {
@BuiltValueField(wireName: 'String')
String get string;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$OneValueAllOfInterfaceBuilder].
$OneValueAllOfInterface rebuild(void Function($OneValueAllOfInterfaceBuilder) updates);

/// Converts the instance to a builder [$OneValueAllOfInterfaceBuilder].
$OneValueAllOfInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($OneValueAllOfInterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -242,6 +287,15 @@ sealed class $SuperObjectInterface {
)! as String;

String get value;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$SuperObjectInterfaceBuilder].
$SuperObjectInterface rebuild(void Function($SuperObjectInterfaceBuilder) updates);

/// Converts the instance to a builder [$SuperObjectInterfaceBuilder].
$SuperObjectInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($SuperObjectInterfaceBuilder b) {
b.value = _$value;
Expand Down Expand Up @@ -291,6 +345,16 @@ abstract class SuperObject implements $SuperObjectInterface, Built<SuperObject,

@BuiltValue(instantiable: false)
sealed class $SubObjectInterface implements $SuperObjectInterface {
/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$SubObjectInterfaceBuilder].
@override
$SubObjectInterface rebuild(void Function($SubObjectInterfaceBuilder) updates);

/// Converts the instance to a builder [$SubObjectInterfaceBuilder].
@override
$SubObjectInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($SubObjectInterfaceBuilder b) {
$SuperObjectInterface._defaults(b);
Expand Down
36 changes: 36 additions & 0 deletions packages/dynamite/dynamite_end_to_end_test/lib/any_of.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ typedef AnyOfIntDoubleNum = num;
sealed class $ObjectAnyOf0Interface {
@BuiltValueField(wireName: 'attribute1-anyOf')
String get attribute1AnyOf;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$ObjectAnyOf0InterfaceBuilder].
$ObjectAnyOf0Interface rebuild(void Function($ObjectAnyOf0InterfaceBuilder) updates);

/// Converts the instance to a builder [$ObjectAnyOf0InterfaceBuilder].
$ObjectAnyOf0InterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($ObjectAnyOf0InterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -74,6 +83,15 @@ abstract class ObjectAnyOf0 implements $ObjectAnyOf0Interface, Built<ObjectAnyOf
sealed class $ObjectAnyOf1Interface {
@BuiltValueField(wireName: 'attribute2-anyOf')
String get attribute2AnyOf;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$ObjectAnyOf1InterfaceBuilder].
$ObjectAnyOf1Interface rebuild(void Function($ObjectAnyOf1InterfaceBuilder) updates);

/// Converts the instance to a builder [$ObjectAnyOf1InterfaceBuilder].
$ObjectAnyOf1InterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($ObjectAnyOf1InterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -117,6 +135,15 @@ typedef ObjectAnyOf = ({ObjectAnyOf0? objectAnyOf0, ObjectAnyOf1? objectAnyOf1})
sealed class $MixedAnyOf1Interface {
@BuiltValueField(wireName: 'attribute-anyOf')
String get attributeAnyOf;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$MixedAnyOf1InterfaceBuilder].
$MixedAnyOf1Interface rebuild(void Function($MixedAnyOf1InterfaceBuilder) updates);

/// Converts the instance to a builder [$MixedAnyOf1InterfaceBuilder].
$MixedAnyOf1InterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($MixedAnyOf1InterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -160,6 +187,15 @@ typedef MixedAnyOf = ({MixedAnyOf1? mixedAnyOf1, String? string});
sealed class $OneObjectAnyOf0Interface {
@BuiltValueField(wireName: 'attribute-anyOf')
String get attributeAnyOf;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$OneObjectAnyOf0InterfaceBuilder].
$OneObjectAnyOf0Interface rebuild(void Function($OneObjectAnyOf0InterfaceBuilder) updates);

/// Converts the instance to a builder [$OneObjectAnyOf0InterfaceBuilder].
$OneObjectAnyOf0InterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($OneObjectAnyOf0InterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ sealed class $Object2Interface {
@Deprecated('')
String get name;
String? get tag;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$Object2InterfaceBuilder].
$Object2Interface rebuild(void Function($Object2InterfaceBuilder) updates);

/// Converts the instance to a builder [$Object2InterfaceBuilder].
$Object2InterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($Object2InterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -161,6 +170,17 @@ abstract class Object2 implements $Object2Interface, Built<Object2, Object2Build
@BuiltValue(instantiable: false)
sealed class $Object1Interface implements $Object2Interface {
int get id;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$Object1InterfaceBuilder].
@override
$Object1Interface rebuild(void Function($Object1InterfaceBuilder) updates);

/// Converts the instance to a builder [$Object1InterfaceBuilder].
@override
$Object1InterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($Object1InterfaceBuilder b) {
$Object2Interface._defaults(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@ sealed class $Object2Interface {

/// The tag of an Object2 object.
String? get tag;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$Object2InterfaceBuilder].
$Object2Interface rebuild(void Function($Object2InterfaceBuilder) updates);

/// Converts the instance to a builder [$Object2InterfaceBuilder].
$Object2InterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($Object2InterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -442,6 +451,17 @@ abstract class Object2 implements $Object2Interface, Built<Object2, Object2Build
sealed class $Object1Interface implements $Object2Interface {
/// The uuid in an UUIDv4 format.
int get id;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$Object1InterfaceBuilder].
@override
$Object1Interface rebuild(void Function($Object1InterfaceBuilder) updates);

/// Converts the instance to a builder [$Object1InterfaceBuilder].
@override
$Object1InterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($Object1InterfaceBuilder b) {
$Object2Interface._defaults(b);
Expand Down
18 changes: 18 additions & 0 deletions packages/dynamite/dynamite_end_to_end_test/lib/enum.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ sealed class $WrappedEnumInterface {
@BuiltValueField(wireName: 'String')
WrappedEnum_String get string;
WrappedEnum_Integer get integer;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$WrappedEnumInterfaceBuilder].
$WrappedEnumInterface rebuild(void Function($WrappedEnumInterfaceBuilder) updates);

/// Converts the instance to a builder [$WrappedEnumInterfaceBuilder].
$WrappedEnumInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($WrappedEnumInterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down Expand Up @@ -388,6 +397,15 @@ abstract class WrappedEnum implements $WrappedEnumInterface, Built<WrappedEnum,
@BuiltValue(instantiable: false)
sealed class $EnumReferenceInterface {
EnumString get string;

/// Rebuilds the instance.
///
/// The result is the same as this instance but with [updates] applied.
/// [updates] is a function that takes a builder [$EnumReferenceInterfaceBuilder].
$EnumReferenceInterface rebuild(void Function($EnumReferenceInterfaceBuilder) updates);

/// Converts the instance to a builder [$EnumReferenceInterfaceBuilder].
$EnumReferenceInterfaceBuilder toBuilder();
@BuiltValueHook(initializeBuilder: true)
static void _defaults($EnumReferenceInterfaceBuilder b) {}
@BuiltValueHook(finalizeBuilder: true)
Expand Down
Loading

0 comments on commit 4504ef9

Please sign in to comment.