Skip to content

Commit 7fe780a

Browse files
authored
fix DeepCopy for *bytes (#80)
1 parent 335599d commit 7fe780a

File tree

7 files changed

+191
-6
lines changed

7 files changed

+191
-6
lines changed

lib/go_emit.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go_emit.iced

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ exports.GoEmitter = class GoEmitter extends BaseEmitter
213213
@output "}"
214214
@output "tmp := ", {frag : true }
215215
# For pointer-to-type where type has DeepCopy() method, we can simplify
216-
# (*x).DeepCopy() to x.DeepCopy(). But for arrays/maps/primitives, we need to dereference.
216+
# (*x).DeepCopy() to x.DeepCopy(). But for arrays/maps/primitives/bytes, we need to dereference.
217217
inner = t[1]
218-
can_simplify = (typeof(inner) is 'string' and not @is_primitive_type_lax(inner))
218+
can_simplify = (typeof(inner) is 'string' and inner isnt 'bytes' and not @is_primitive_type_lax(inner))
219219
inner_val = if can_simplify then "x" else "(*x)"
220220
@deep_copy { t : inner, val : inner_val, exported : true }
221221
@output "return &tmp"

src/go_emit.test.iced

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,55 @@ describe 'GoEmitter', () ->
350350
""")
351351
return
352352

353+
it "Should emit a struct with bytes and optional bytes fields", () ->
354+
record = {
355+
type: "record"
356+
name: "BytesRecord"
357+
fields: [
358+
{
359+
type: "bytes",
360+
name: "data"
361+
},
362+
{
363+
type: [null, "bytes"],
364+
name: "optionalData"
365+
}
366+
]
367+
}
368+
emitter.emit_record record
369+
code = emitter._code.join "\n"
370+
371+
expect(code).toBe("""
372+
type BytesRecord struct {
373+
\tData\t[]byte\t`codec:"data" json:"data"`
374+
\tOptionalData\t*[]byte\t`codec:"optionalData,omitempty" json:"optionalData,omitempty"`
375+
}
376+
377+
func (o BytesRecord) DeepCopy() BytesRecord {
378+
\treturn BytesRecord{
379+
\t\tData: (func (x []byte) []byte {
380+
\t\t\tif x == nil {
381+
\t\t\t\treturn nil
382+
\t\t\t}
383+
\t\t\treturn append([]byte{}, x...)
384+
\t\t})(o.Data),
385+
\t\tOptionalData: (func (x *[]byte) *[]byte {
386+
\t\t\tif x == nil {
387+
\t\t\t\treturn nil
388+
\t\t\t}
389+
\t\t\ttmp := (func (x []byte) []byte {
390+
\t\t\t\tif x == nil {
391+
\t\t\t\t\treturn nil
392+
\t\t\t\t}
393+
\t\t\t\treturn append([]byte{}, x...)
394+
\t\t\t})((*x))
395+
\t\t\treturn &tmp
396+
\t\t})(o.OptionalData),
397+
\t}
398+
}\n
399+
""")
400+
return
401+
353402
return
354403

355404

test/avdl/sample.avdl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ protocol sample {
8484
union {null, array<Blurp>} u;
8585
}
8686

87+
record BytesRecord {
88+
bytes data;
89+
union {null, bytes} optionalData;
90+
}
91+
92+
record OptionalTypesRecord {
93+
union {null, array<int>} optionalArray;
94+
union {null, map<string>} optionalMap;
95+
union {null, int} optionalInt;
96+
union {null, string} optionalString;
97+
union {null, boolean} optionalBool;
98+
}
99+
87100
@typedef ("bytes") record Hash {}
88101

89102
@layout("array")

test/files/sample.go

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Auto-generated to Go types and interfaces using avdl-compiler v1.4.10 (https://github.com/keybase/node-avdl-compiler)
2-
// Input file: avdl/sample.avdl
2+
// Input file: test/avdl/sample.avdl
33

44
package sample1
55

@@ -566,6 +566,103 @@ func (o Simple) DeepCopy() Simple {
566566
}
567567
}
568568

569+
type BytesRecord struct {
570+
Data []byte `codec:"data" json:"data"`
571+
OptionalData *[]byte `codec:"optionalData,omitempty" json:"optionalData,omitempty"`
572+
}
573+
574+
func (o BytesRecord) DeepCopy() BytesRecord {
575+
return BytesRecord{
576+
Data: (func(x []byte) []byte {
577+
if x == nil {
578+
return nil
579+
}
580+
return append([]byte{}, x...)
581+
})(o.Data),
582+
OptionalData: (func(x *[]byte) *[]byte {
583+
if x == nil {
584+
return nil
585+
}
586+
tmp := (func(x []byte) []byte {
587+
if x == nil {
588+
return nil
589+
}
590+
return append([]byte{}, x...)
591+
})((*x))
592+
return &tmp
593+
})(o.OptionalData),
594+
}
595+
}
596+
597+
type OptionalTypesRecord struct {
598+
OptionalArray *[]int `codec:"optionalArray,omitempty" json:"optionalArray,omitempty"`
599+
OptionalMap *map[string]string `codec:"optionalMap,omitempty" json:"optionalMap,omitempty"`
600+
OptionalInt *int `codec:"optionalInt,omitempty" json:"optionalInt,omitempty"`
601+
OptionalString *string `codec:"optionalString,omitempty" json:"optionalString,omitempty"`
602+
OptionalBool *bool `codec:"optionalBool,omitempty" json:"optionalBool,omitempty"`
603+
}
604+
605+
func (o OptionalTypesRecord) DeepCopy() OptionalTypesRecord {
606+
return OptionalTypesRecord{
607+
OptionalArray: (func(x *[]int) *[]int {
608+
if x == nil {
609+
return nil
610+
}
611+
tmp := (func(x []int) []int {
612+
if x == nil {
613+
return nil
614+
}
615+
ret := make([]int, len(x))
616+
for i, v := range x {
617+
vCopy := v
618+
ret[i] = vCopy
619+
}
620+
return ret
621+
})((*x))
622+
return &tmp
623+
})(o.OptionalArray),
624+
OptionalMap: (func(x *map[string]string) *map[string]string {
625+
if x == nil {
626+
return nil
627+
}
628+
tmp := (func(x map[string]string) map[string]string {
629+
if x == nil {
630+
return nil
631+
}
632+
ret := make(map[string]string, len(x))
633+
for k, v := range x {
634+
kCopy := k
635+
vCopy := v
636+
ret[kCopy] = vCopy
637+
}
638+
return ret
639+
})((*x))
640+
return &tmp
641+
})(o.OptionalMap),
642+
OptionalInt: (func(x *int) *int {
643+
if x == nil {
644+
return nil
645+
}
646+
tmp := (*x)
647+
return &tmp
648+
})(o.OptionalInt),
649+
OptionalString: (func(x *string) *string {
650+
if x == nil {
651+
return nil
652+
}
653+
tmp := (*x)
654+
return &tmp
655+
})(o.OptionalString),
656+
OptionalBool: (func(x *bool) *bool {
657+
if x == nil {
658+
return nil
659+
}
660+
tmp := (*x)
661+
return &tmp
662+
})(o.OptionalBool),
663+
}
664+
}
665+
569666
type Hash []byte
570667

571668
func (o Hash) DeepCopy() Hash {

test/files/sample.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
Auto-generated to Python types by avdl-compiler v1.4.10 (https://github.com/keybase/node-avdl-compiler)
44
Input files:
5-
- avdl/sample.avdl
5+
- test/avdl/sample.avdl
66
"""
77

88
from dataclasses import dataclass, field
@@ -122,6 +122,19 @@ class Simple(DataClassJsonMixin):
122122
t: Optional[Blurp] = field(default=None, metadata=config(field_name='t'))
123123
u: Optional[List[Blurp]] = field(default=None, metadata=config(field_name='u'))
124124

125+
@dataclass
126+
class BytesRecord(DataClassJsonMixin):
127+
data: str = field(metadata=config(field_name='data'))
128+
optional_data: Optional[str] = field(default=None, metadata=config(field_name='optionalData'))
129+
130+
@dataclass
131+
class OptionalTypesRecord(DataClassJsonMixin):
132+
optional_array: Optional[List[int]] = field(default=None, metadata=config(field_name='optionalArray'))
133+
optional_map: Optional[Dict[str, str]] = field(default=None, metadata=config(field_name='optionalMap'))
134+
optional_int: Optional[int] = field(default=None, metadata=config(field_name='optionalInt'))
135+
optional_string: Optional[str] = field(default=None, metadata=config(field_name='optionalString'))
136+
optional_bool: Optional[bool] = field(default=None, metadata=config(field_name='optionalBool'))
137+
125138
Hash = str
126139
@dataclass
127140
class Cat(DataClassJsonMixin):

test/files/sample.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Auto-generated to TypeScript types by avdl-compiler v1.4.10 (https://github.com/keybase/node-avdl-compiler)
66
* Input files:
7-
* - avdl/sample.avdl
7+
* - test/avdl/sample.avdl
88
*/
99

1010
import * as keybase1 from 'github.com/keybase/client/go/protocol/keybase1'
@@ -49,6 +49,19 @@ export type Simple = {
4949
u?: Blurp[] | null
5050
}
5151

52+
export type BytesRecord = {
53+
data: Buffer
54+
optionalData?: Buffer
55+
}
56+
57+
export type OptionalTypesRecord = {
58+
optionalArray?: number[] | null
59+
optionalMap?: {[key: string]: string}
60+
optionalInt?: number
61+
optionalString?: string
62+
optionalBool?: boolean
63+
}
64+
5265
export type Hash = Buffer
5366

5467
export type Cat = {

0 commit comments

Comments
 (0)