Skip to content

Commit

Permalink
feat: copyWith generation (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegofrata authored Oct 10, 2024
1 parent ef1a1bc commit 121adc7
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 10 deletions.
4 changes: 4 additions & 0 deletions examples/embedded/user.go.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// ignore_for_file: always_use_package_imports
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'user.go.g.dart';

@CopyWith()
@JsonSerializable(explicitToJson: true)
class Address extends Equatable {
@JsonKey(name: "street_line_1")final String street;
Expand All @@ -28,6 +30,7 @@ class Address extends Equatable {
];
}

@CopyWith()
@JsonSerializable(explicitToJson: true)
class Profile extends Equatable {
@JsonKey(name: "ID")final int id;
Expand Down Expand Up @@ -61,6 +64,7 @@ class Profile extends Equatable {
];
}

@CopyWith()
@JsonSerializable(explicitToJson: true)
class User extends Equatable {
@JsonKey(name: "ID")final int id;
Expand Down
3 changes: 3 additions & 0 deletions examples/everything/everything.go.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// ignore_for_file: always_use_package_imports
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'everything.go.g.dart';

@CopyWith()
@JsonSerializable(explicitToJson: true)
class Child extends Equatable {
final int id;
Expand Down Expand Up @@ -38,6 +40,7 @@ class Empty extends Equatable {
List<Object?> get props => [];
}

@CopyWith()
@JsonSerializable(explicitToJson: true)
class Parent extends Equatable {
final String id;
Expand Down
2 changes: 2 additions & 0 deletions examples/firestore/firestore.go.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: always_use_package_imports
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

Expand All @@ -15,6 +16,7 @@ class _TimestampConverter implements JsonConverter<DateTime, Timestamp> {
Timestamp toJson(DateTime object) => Timestamp.fromDate(object);
}

@CopyWith()
@JsonSerializable(explicitToJson: true)
@_TimestampConverter()
class User extends Equatable {
Expand Down
4 changes: 4 additions & 0 deletions examples/generics/generics.go.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// ignore_for_file: always_use_package_imports
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'generics.go.g.dart';

@CopyWith()
@JsonSerializable(explicitToJson: true)
class Instance extends Equatable {
final Map<String, int> m;
Expand All @@ -22,6 +24,7 @@ class Instance extends Equatable {
];
}

@CopyWith()
@JsonSerializable(explicitToJson: true, genericArgumentFactories: true)
class KeyValuePair<TKey, TValue> extends Equatable {
final TKey key;
Expand All @@ -43,6 +46,7 @@ class KeyValuePair<TKey, TValue> extends Equatable {
];
}

@CopyWith()
@JsonSerializable(explicitToJson: true, genericArgumentFactories: true)
class Map<TKey, TValue> extends Equatable {
@JsonKey(defaultValue: <List<KeyValuePair<TKey, TValue>>>[])final List<KeyValuePair<TKey, TValue>> items;
Expand Down
2 changes: 2 additions & 0 deletions examples/multipackage/multipackage.go.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: always_use_package_imports
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

Expand All @@ -15,6 +16,7 @@ class _TimestampConverter implements JsonConverter<DateTime, Timestamp> {
Timestamp toJson(DateTime object) => Timestamp.fromDate(object);
}

@CopyWith()
@JsonSerializable(explicitToJson: true)
@_TimestampConverter()
class Outer extends Equatable {
Expand Down
2 changes: 2 additions & 0 deletions examples/user/user.go.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// ignore_for_file: always_use_package_imports
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'user.go.g.dart';

@CopyWith()
@JsonSerializable(explicitToJson: true)
class User extends Equatable {
@JsonKey(name: "ID")final int id;
Expand Down
22 changes: 12 additions & 10 deletions generator/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,17 @@ func extractFields(st *types.Struct) []fieldProjection {
return fields
}

func generateFields(wr io.Writer, st *types.Struct, registry *format.TypeFormatterRegistry, mode options.Mode) {
fields := extractFields(st)
func generateFields(wr io.Writer, registry *format.TypeFormatterRegistry, mode options.Mode, fields []fieldProjection) {
for _, field := range fields {
generateFieldDeclaration(wr, field.field, field.tag, registry, mode)
fmt.Fprintln(wr, ";")
}
fmt.Fprintln(wr)
}

func generateConstructor(wr io.Writer, ts *types.TypeName, st *types.Struct, registry *format.TypeFormatterRegistry) {
func generateConstructor(wr io.Writer, ts *types.TypeName, registry *format.TypeFormatterRegistry, fields []fieldProjection) {
fmt.Fprintf(wr, "const %s(", ts.Name())

fields := extractFields(st)
if len(fields) > 0 {
fmt.Fprintln(wr, "{")
for _, field := range fields {
Expand All @@ -60,15 +58,14 @@ func generateConstructor(wr io.Writer, ts *types.TypeName, st *types.Struct, reg
fmt.Fprintln(wr)
}

func generateEquatable(wr io.Writer, st *types.Struct) {
func generateEquatable(wr io.Writer, fields []fieldProjection) {
fmt.Fprintln(wr, "@override")
fmt.Fprint(wr, "List<Object?> get props => [")
if st.NumFields() > 0 {
if len(fields) > 0 {
fmt.Fprintln(wr)
}

iwr := indent.NewWriter(wr, "\t")
fields := extractFields(st)
for _, field := range fields {
fmt.Fprintf(iwr, "%s,\n", format.GetFieldName(field.field))
}
Expand All @@ -82,6 +79,11 @@ func generateDartClass(outputFile io.Writer, ts *types.TypeName, st *types.Struc
panic(fmt.Sprintf("expected StructFormatter, got %T", registry.GetTypeFormatter(ts.Type())))
}

fields := extractFields(st)

if len(fields) > 0 {
fmt.Fprintln(outputFile, "@CopyWith()")
}
fmt.Fprintln(outputFile, formatter.Annotation(ts))
if mode == options.Firestore {
fmt.Fprintln(outputFile, "@_TimestampConverter()")
Expand All @@ -90,11 +92,11 @@ func generateDartClass(outputFile io.Writer, ts *types.TypeName, st *types.Struc

wr := indent.NewWriter(outputFile, "\t")

generateFields(wr, st, registry, mode)
generateConstructor(wr, ts, st, registry)
generateFields(wr, registry, mode, fields)
generateConstructor(wr, ts, registry, fields)
fmt.Fprint(wr, formatter.Serialization(ts))
fmt.Fprintln(wr, formatter.Deserialization(ts))
generateEquatable(wr, st)
generateEquatable(wr, fields)

fmt.Fprintln(outputFile, "}")
fmt.Fprintln(outputFile, "")
Expand Down
1 change: 1 addition & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func generateHeader(pkg *packages.Package, wr io.Writer, mode options.Mode, impo
fmt.Fprint(wr, "import 'package:cloud_firestore/cloud_firestore.dart';\n")
}

fmt.Fprint(wr, "import 'package:copy_with_extension/copy_with_extension.dart';\n")
fmt.Fprint(wr, "import 'package:equatable/equatable.dart';\n")
fmt.Fprint(wr, "import 'package:json_annotation/json_annotation.dart';\n")
for _, imp := range imports {
Expand Down

0 comments on commit 121adc7

Please sign in to comment.