Skip to content

Commit 2218232

Browse files
authored
refactor(dart): Use patterns in buildQueryLines (#3011)
1 parent 0011416 commit 2218232

File tree

2 files changed

+50
-58
lines changed

2 files changed

+50
-58
lines changed

internal/sidekick/dart/annotate.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -949,26 +949,26 @@ func (annotate *annotateModel) buildQueryLines(
949949

950950
var preable string
951951
if codec.Nullable {
952-
preable = fmt.Sprintf("if (%s != null) '%s'", ref, param)
952+
preable = fmt.Sprintf("if (%s case final $1?) '%s'", ref, param)
953953
} else {
954-
preable = fmt.Sprintf("if (%s.isNotDefault) '%s'", ref, param)
954+
preable = fmt.Sprintf("if (%s case final $1 when $1.isNotDefault) '%s'", ref, param)
955955
}
956956

957957
switch {
958958
case field.Repeated:
959959
// Handle lists; these should be lists of strings or other primitives.
960960
switch field.Typez {
961961
case api.STRING_TYPE:
962-
return append(result, fmt.Sprintf("%s: %s", preable, ref))
962+
return append(result, fmt.Sprintf("%s: $1", preable))
963963
case api.ENUM_TYPE:
964-
return append(result, fmt.Sprintf("%s: %s!.map((e) => e.value)", preable, ref))
964+
return append(result, fmt.Sprintf("%s: $1.map((e) => e.value)", preable))
965965
case api.BOOL_TYPE, api.INT32_TYPE, api.UINT32_TYPE, api.SINT32_TYPE,
966966
api.FIXED32_TYPE, api.SFIXED32_TYPE, api.INT64_TYPE,
967967
api.UINT64_TYPE, api.SINT64_TYPE, api.FIXED64_TYPE, api.SFIXED64_TYPE,
968968
api.FLOAT_TYPE, api.DOUBLE_TYPE:
969-
return append(result, fmt.Sprintf("%s: %s!.map((e) => '$e')", preable, ref))
969+
return append(result, fmt.Sprintf("%s: $1.map((e) => '$e')", preable))
970970
case api.BYTES_TYPE:
971-
return append(result, fmt.Sprintf("%s: %s!.map((e) => encodeBytes(e)!)", preable, ref))
971+
return append(result, fmt.Sprintf("%s: $1.map((e) => encodeBytes(e)!)", preable))
972972
default:
973973
slog.Error("unhandled list query param", "type", field.Typez)
974974
return append(result, fmt.Sprintf("/* unhandled list query param type: %d */", field.Typez))
@@ -988,7 +988,7 @@ func (annotate *annotateModel) buildQueryLines(
988988
_, hasCustomEncoding := usesCustomEncoding[field.TypezID]
989989
if hasCustomEncoding {
990990
// Example: 'fieldMask': fieldMask!.toJson()
991-
return append(result, fmt.Sprintf("%s: %s%stoJson()", preable, ref, deref))
991+
return append(result, fmt.Sprintf("%s: $1.toJson()", preable))
992992
}
993993

994994
// Unroll the fields for messages.
@@ -998,17 +998,9 @@ func (annotate *annotateModel) buildQueryLines(
998998
return result
999999

10001000
case field.Typez == api.STRING_TYPE:
1001-
deref := ""
1002-
if codec.Nullable {
1003-
deref = "!"
1004-
}
1005-
return append(result, fmt.Sprintf("%s: %s%s", preable, ref, deref))
1001+
return append(result, fmt.Sprintf("%s: $1", preable))
10061002
case field.Typez == api.ENUM_TYPE:
1007-
deref := "."
1008-
if codec.Nullable {
1009-
deref = "!."
1010-
}
1011-
return append(result, fmt.Sprintf("%s: %s%svalue", preable, ref, deref))
1003+
return append(result, fmt.Sprintf("%s: $1.value", preable))
10121004
case field.Typez == api.BOOL_TYPE ||
10131005
field.Typez == api.INT32_TYPE ||
10141006
field.Typez == api.UINT32_TYPE || field.Typez == api.SINT32_TYPE ||
@@ -1017,9 +1009,9 @@ func (annotate *annotateModel) buildQueryLines(
10171009
field.Typez == api.UINT64_TYPE || field.Typez == api.SINT64_TYPE ||
10181010
field.Typez == api.FIXED64_TYPE || field.Typez == api.SFIXED64_TYPE ||
10191011
field.Typez == api.FLOAT_TYPE || field.Typez == api.DOUBLE_TYPE:
1020-
return append(result, fmt.Sprintf("%s: '${%s}'", preable, ref))
1012+
return append(result, fmt.Sprintf("%s: '${$1}'", preable))
10211013
case field.Typez == api.BYTES_TYPE:
1022-
return append(result, fmt.Sprintf("%s: encodeBytes(%s)!", preable, ref))
1014+
return append(result, fmt.Sprintf("%s: encodeBytes($1)!", preable))
10231015
default:
10241016
slog.Error("unhandled query param", "type", field.Typez)
10251017
return append(result, fmt.Sprintf("/* unhandled query param type: %d */", field.Typez))

internal/sidekick/dart/annotate_test.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -446,100 +446,100 @@ func TestBuildQueryLines(t *testing.T) {
446446
// primitives
447447
{
448448
&api.Field{Name: "bool", JSONName: "bool", Typez: api.BOOL_TYPE},
449-
[]string{"if (result.bool$.isNotDefault) 'bool': '${result.bool$}'"},
449+
[]string{"if (result.bool$ case final $1 when $1.isNotDefault) 'bool': '${$1}'"},
450450
}, {
451451
&api.Field{Name: "int32", JSONName: "int32", Typez: api.INT32_TYPE},
452-
[]string{"if (result.int32.isNotDefault) 'int32': '${result.int32}'"},
452+
[]string{"if (result.int32 case final $1 when $1.isNotDefault) 'int32': '${$1}'"},
453453
}, {
454454
&api.Field{Name: "fixed32", JSONName: "fixed32", Typez: api.FIXED32_TYPE},
455-
[]string{"if (result.fixed32.isNotDefault) 'fixed32': '${result.fixed32}'"},
455+
[]string{"if (result.fixed32 case final $1 when $1.isNotDefault) 'fixed32': '${$1}'"},
456456
}, {
457457
&api.Field{Name: "sfixed32", JSONName: "sfixed32", Typez: api.SFIXED32_TYPE},
458-
[]string{"if (result.sfixed32.isNotDefault) 'sfixed32': '${result.sfixed32}'"},
458+
[]string{"if (result.sfixed32 case final $1 when $1.isNotDefault) 'sfixed32': '${$1}'"},
459459
}, {
460460
&api.Field{Name: "int64", JSONName: "int64", Typez: api.INT64_TYPE},
461-
[]string{"if (result.int64.isNotDefault) 'int64': '${result.int64}'"},
461+
[]string{"if (result.int64 case final $1 when $1.isNotDefault) 'int64': '${$1}'"},
462462
}, {
463463
&api.Field{Name: "fixed64", JSONName: "fixed64", Typez: api.FIXED64_TYPE},
464-
[]string{"if (result.fixed64.isNotDefault) 'fixed64': '${result.fixed64}'"},
464+
[]string{"if (result.fixed64 case final $1 when $1.isNotDefault) 'fixed64': '${$1}'"},
465465
}, {
466466
&api.Field{Name: "sfixed64", JSONName: "sfixed64", Typez: api.SFIXED64_TYPE},
467-
[]string{"if (result.sfixed64.isNotDefault) 'sfixed64': '${result.sfixed64}'"},
467+
[]string{"if (result.sfixed64 case final $1 when $1.isNotDefault) 'sfixed64': '${$1}'"},
468468
}, {
469469
&api.Field{Name: "double", JSONName: "double", Typez: api.DOUBLE_TYPE},
470-
[]string{"if (result.double$.isNotDefault) 'double': '${result.double$}'"},
470+
[]string{"if (result.double$ case final $1 when $1.isNotDefault) 'double': '${$1}'"},
471471
}, {
472472
&api.Field{Name: "string", JSONName: "string", Typez: api.STRING_TYPE},
473-
[]string{"if (result.string.isNotDefault) 'string': result.string"},
473+
[]string{"if (result.string case final $1 when $1.isNotDefault) 'string': $1"},
474474
},
475475

476476
// optional primitives
477477
{
478478
&api.Field{Name: "bool_opt", JSONName: "bool", Typez: api.BOOL_TYPE, Optional: true},
479-
[]string{"if (result.boolOpt != null) 'bool': '${result.boolOpt}'"},
479+
[]string{"if (result.boolOpt case final $1?) 'bool': '${$1}'"},
480480
}, {
481481
&api.Field{Name: "int32_opt", JSONName: "int32", Typez: api.INT32_TYPE, Optional: true},
482-
[]string{"if (result.int32Opt != null) 'int32': '${result.int32Opt}'"},
482+
[]string{"if (result.int32Opt case final $1?) 'int32': '${$1}'"},
483483
}, {
484484
&api.Field{Name: "fixed32_opt", JSONName: "fixed32", Typez: api.FIXED32_TYPE, Optional: true},
485-
[]string{"if (result.fixed32Opt != null) 'fixed32': '${result.fixed32Opt}'"},
485+
[]string{"if (result.fixed32Opt case final $1?) 'fixed32': '${$1}'"},
486486
}, {
487487
&api.Field{Name: "sfixed32_opt", JSONName: "sfixed32", Typez: api.SFIXED32_TYPE, Optional: true},
488-
[]string{"if (result.sfixed32Opt != null) 'sfixed32': '${result.sfixed32Opt}'"},
488+
[]string{"if (result.sfixed32Opt case final $1?) 'sfixed32': '${$1}'"},
489489
}, {
490490
&api.Field{Name: "int64_opt", JSONName: "int64", Typez: api.INT64_TYPE, Optional: true},
491-
[]string{"if (result.int64Opt != null) 'int64': '${result.int64Opt}'"},
491+
[]string{"if (result.int64Opt case final $1?) 'int64': '${$1}'"},
492492
}, {
493493
&api.Field{Name: "fixed64_opt", JSONName: "fixed64", Typez: api.FIXED64_TYPE, Optional: true},
494-
[]string{"if (result.fixed64Opt != null) 'fixed64': '${result.fixed64Opt}'"},
494+
[]string{"if (result.fixed64Opt case final $1?) 'fixed64': '${$1}'"},
495495
}, {
496496
&api.Field{Name: "sfixed64_opt", JSONName: "sfixed64", Typez: api.SFIXED64_TYPE, Optional: true},
497-
[]string{"if (result.sfixed64Opt != null) 'sfixed64': '${result.sfixed64Opt}'"},
497+
[]string{"if (result.sfixed64Opt case final $1?) 'sfixed64': '${$1}'"},
498498
}, {
499499
&api.Field{Name: "double_opt", JSONName: "double", Typez: api.DOUBLE_TYPE, Optional: true},
500-
[]string{"if (result.doubleOpt != null) 'double': '${result.doubleOpt}'"},
500+
[]string{"if (result.doubleOpt case final $1?) 'double': '${$1}'"},
501501
}, {
502502
&api.Field{Name: "string_opt", JSONName: "string", Typez: api.STRING_TYPE, Optional: true},
503-
[]string{"if (result.stringOpt != null) 'string': result.stringOpt!"},
503+
[]string{"if (result.stringOpt case final $1?) 'string': $1"},
504504
},
505505

506506
// one ofs
507507
{
508508
&api.Field{Name: "bool", JSONName: "bool", Typez: api.BOOL_TYPE, IsOneOf: true},
509-
[]string{"if (result.bool$ != null) 'bool': '${result.bool$}'"},
509+
[]string{"if (result.bool$ case final $1?) 'bool': '${$1}'"},
510510
},
511511

512512
// repeated primitives
513513
{
514514
&api.Field{Name: "boolList", JSONName: "boolList", Typez: api.BOOL_TYPE, Repeated: true},
515-
[]string{"if (result.boolList.isNotDefault) 'boolList': result.boolList!.map((e) => '$e')"},
515+
[]string{"if (result.boolList case final $1 when $1.isNotDefault) 'boolList': $1.map((e) => '$e')"},
516516
}, {
517517
&api.Field{Name: "int32List", JSONName: "int32List", Typez: api.INT32_TYPE, Repeated: true},
518-
[]string{"if (result.int32List.isNotDefault) 'int32List': result.int32List!.map((e) => '$e')"},
518+
[]string{"if (result.int32List case final $1 when $1.isNotDefault) 'int32List': $1.map((e) => '$e')"},
519519
}, {
520520
&api.Field{Name: "int64List", JSONName: "int64List", Typez: api.INT64_TYPE, Repeated: true},
521-
[]string{"if (result.int64List.isNotDefault) 'int64List': result.int64List!.map((e) => '$e')"},
521+
[]string{"if (result.int64List case final $1 when $1.isNotDefault) 'int64List': $1.map((e) => '$e')"},
522522
}, {
523523
&api.Field{Name: "doubleList", JSONName: "doubleList", Typez: api.DOUBLE_TYPE, Repeated: true},
524-
[]string{"if (result.doubleList.isNotDefault) 'doubleList': result.doubleList!.map((e) => '$e')"},
524+
[]string{"if (result.doubleList case final $1 when $1.isNotDefault) 'doubleList': $1.map((e) => '$e')"},
525525
}, {
526526
&api.Field{Name: "stringList", JSONName: "stringList", Typez: api.STRING_TYPE, Repeated: true},
527-
[]string{"if (result.stringList.isNotDefault) 'stringList': result.stringList"},
527+
[]string{"if (result.stringList case final $1 when $1.isNotDefault) 'stringList': $1"},
528528
},
529529

530530
// repeated primitives w/ optional
531531
{
532532
&api.Field{Name: "int32List_opt", JSONName: "int32List", Typez: api.INT32_TYPE, Repeated: true, Optional: true},
533-
[]string{"if (result.int32ListOpt.isNotDefault) 'int32List': result.int32ListOpt!.map((e) => '$e')"},
533+
[]string{"if (result.int32ListOpt case final $1 when $1.isNotDefault) 'int32List': $1.map((e) => '$e')"},
534534
},
535535

536536
// bytes, repeated bytes
537537
{
538538
&api.Field{Name: "bytes", JSONName: "bytes", Typez: api.BYTES_TYPE},
539-
[]string{"if (result.bytes != null) 'bytes': encodeBytes(result.bytes)!"},
539+
[]string{"if (result.bytes case final $1?) 'bytes': encodeBytes($1)!"},
540540
}, {
541541
&api.Field{Name: "bytesList", JSONName: "bytesList", Typez: api.BYTES_TYPE, Repeated: true},
542-
[]string{"if (result.bytesList != null) 'bytesList': result.bytesList!.map((e) => encodeBytes(e)!)"},
542+
[]string{"if (result.bytesList case final $1?) 'bytesList': $1.map((e) => encodeBytes(e)!)"},
543543
},
544544
} {
545545
t.Run(test.field.Name, func(t *testing.T) {
@@ -596,7 +596,7 @@ func TestBuildQueryLinesEnums(t *testing.T) {
596596
JSONName: "jsonEnumName",
597597
Typez: api.ENUM_TYPE,
598598
TypezID: enum.ID},
599-
[]string{"if (result.enumName.isNotDefault) 'jsonEnumName': result.enumName.value"},
599+
[]string{"if (result.enumName case final $1 when $1.isNotDefault) 'jsonEnumName': $1.value"},
600600
},
601601
{
602602
&api.Field{
@@ -605,7 +605,7 @@ func TestBuildQueryLinesEnums(t *testing.T) {
605605
Typez: api.ENUM_TYPE,
606606
TypezID: enum.ID,
607607
Optional: true},
608-
[]string{"if (result.optionalEnum != null) 'optionalJsonEnum': result.optionalEnum!.value"},
608+
[]string{"if (result.optionalEnum case final $1?) 'optionalJsonEnum': $1.value"},
609609
},
610610
{
611611
&api.Field{
@@ -614,7 +614,7 @@ func TestBuildQueryLinesEnums(t *testing.T) {
614614
Typez: api.ENUM_TYPE,
615615
TypezID: foreignEnumState.ID,
616616
Optional: false},
617-
[]string{"if (result.enumName.isNotDefault) 'jsonEnumName': result.enumName.value"},
617+
[]string{"if (result.enumName case final $1 when $1.isNotDefault) 'jsonEnumName': $1.value"},
618618
},
619619
} {
620620
t.Run(test.enumField.Name, func(t *testing.T) {
@@ -683,17 +683,17 @@ func TestBuildQueryLinesMessages(t *testing.T) {
683683
// messages
684684
got := annotate.buildQueryLines([]string{}, "result.", "", messageField1, model.State)
685685
want := []string{
686-
"if (result.message1!.name.isNotDefault) 'message1.name': result.message1!.name",
687-
"if (result.message1!.state.isNotDefault) 'message1.state': result.message1!.state.value",
686+
"if (result.message1!.name case final $1 when $1.isNotDefault) 'message1.name': $1",
687+
"if (result.message1!.state case final $1 when $1.isNotDefault) 'message1.state': $1.value",
688688
}
689689
if diff := cmp.Diff(want, got); diff != "" {
690690
t.Errorf("mismatch in TestBuildQueryLines (-want, +got)\n:%s", diff)
691691
}
692692

693693
got = annotate.buildQueryLines([]string{}, "result.", "", messageField2, model.State)
694694
want = []string{
695-
"if (result.message2!.data != null) 'message2.data': encodeBytes(result.message2!.data)!",
696-
"if (result.message2!.dataCrc32C != null) 'message2.dataCrc32c': '${result.message2!.dataCrc32C}'",
695+
"if (result.message2!.data case final $1?) 'message2.data': encodeBytes($1)!",
696+
"if (result.message2!.dataCrc32C case final $1?) 'message2.dataCrc32c': '${$1}'",
697697
}
698698
if diff := cmp.Diff(want, got); diff != "" {
699699
t.Errorf("mismatch in TestBuildQueryLines (-want, +got)\n:%s", diff)
@@ -702,8 +702,8 @@ func TestBuildQueryLinesMessages(t *testing.T) {
702702
// nested messages
703703
got = annotate.buildQueryLines([]string{}, "result.", "", messageField3, model.State)
704704
want = []string{
705-
"if (result.message3!.secret!.name.isNotDefault) 'message3.secret.name': result.message3!.secret!.name",
706-
"if (result.message3!.fieldMask != null) 'message3.fieldMask': result.message3!.fieldMask!.toJson()",
705+
"if (result.message3!.secret!.name case final $1 when $1.isNotDefault) 'message3.secret.name': $1",
706+
"if (result.message3!.fieldMask case final $1?) 'message3.fieldMask': $1.toJson()",
707707
}
708708
if diff := cmp.Diff(want, got); diff != "" {
709709
t.Errorf("mismatch in TestBuildQueryLines (-want, +got)\n:%s", diff)
@@ -712,23 +712,23 @@ func TestBuildQueryLinesMessages(t *testing.T) {
712712
// custom encoded messages
713713
got = annotate.buildQueryLines([]string{}, "result.", "", fieldMaskField, model.State)
714714
want = []string{
715-
"if (result.fieldMask != null) 'fieldMask': result.fieldMask!.toJson()",
715+
"if (result.fieldMask case final $1?) 'fieldMask': $1.toJson()",
716716
}
717717
if diff := cmp.Diff(want, got); diff != "" {
718718
t.Errorf("mismatch in TestBuildQueryLines (-want, +got)\n:%s", diff)
719719
}
720720

721721
got = annotate.buildQueryLines([]string{}, "result.", "", durationField, model.State)
722722
want = []string{
723-
"if (result.duration != null) 'duration': result.duration!.toJson()",
723+
"if (result.duration case final $1?) 'duration': $1.toJson()",
724724
}
725725
if diff := cmp.Diff(want, got); diff != "" {
726726
t.Errorf("mismatch in TestBuildQueryLines (-want, +got)\n:%s", diff)
727727
}
728728

729729
got = annotate.buildQueryLines([]string{}, "result.", "", timestampField, model.State)
730730
want = []string{
731-
"if (result.time != null) 'time': result.time!.toJson()",
731+
"if (result.time case final $1?) 'time': $1.toJson()",
732732
}
733733
if diff := cmp.Diff(want, got); diff != "" {
734734
t.Errorf("mismatch in TestBuildQueryLines (-want, +got)\n:%s", diff)

0 commit comments

Comments
 (0)