Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparent encoding #91

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 66 additions & 13 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ func (f Field) Len() int {
}

type GenTypeInfo struct {
Name string
Fields []Field
Name string
Fields []Field
Transparent bool
}

func (gti *GenTypeInfo) Imports() []Import {
Expand Down Expand Up @@ -178,10 +179,36 @@ func ParseTypeInfo(itype interface{}) (*GenTypeInfo, error) {
pkg := t.PkgPath()

out := GenTypeInfo{
Name: t.Name(),
Name: t.Name(),
Transparent: false,
}

if t.Kind() != reflect.Struct {
return &GenTypeInfo{
Name: t.Name(),
Transparent: true,
Fields: []Field{
{
Name: ".",
MapKey: "",
Pointer: t.Kind() == reflect.Ptr,
Type: t,
Pkg: pkg,
Const: nil,
OmitEmpty: false,
PreserveNil: false,
IterLabel: "",
MaxLen: NoUsrMaxLen,
},
},
}, nil
}

for i := 0; i < t.NumField(); i++ {
if out.Transparent {
return nil, fmt.Errorf("transparent structs must exactly one field")
magik6k marked this conversation as resolved.
Show resolved Hide resolved
}

f := t.Field(i)
if !nameIsExported(f.Name) {
continue
Expand Down Expand Up @@ -226,6 +253,12 @@ func ParseTypeInfo(itype interface{}) (*GenTypeInfo, error) {
constval = &cv
}

_, transparent := tags["transparent"]
if transparent && len(out.Fields) > 0 {
return nil, fmt.Errorf("only one transparent field is allowed")
}
out.Transparent = transparent

_, omitempty := tags["omitempty"]
_, preservenil := tags["preservenil"]

Expand Down Expand Up @@ -270,6 +303,8 @@ func tagparse(v string) (map[string]string, error) {
out["preservenil"] = "true"
} else if elem == "ignore" || elem == "-" {
out["ignore"] = "true"
} else if elem == "transparent" {
out["transparent"] = "true"
} else {
out["name"] = elem
}
Expand Down Expand Up @@ -680,26 +715,31 @@ func emitCborMarshalArrayField(w io.Writer, f Field) error {

func emitCborMarshalStructTuple(w io.Writer, gti *GenTypeInfo) error {
magik6k marked this conversation as resolved.
Show resolved Hide resolved
// 9 byte buffer to accomodate for the maximum header length (cbor varints are maximum 9 bytes_
err := doTemplate(w, gti, `var lengthBuf{{ .Name }} = {{ .TupleHeaderAsByteString }}
err := doTemplate(w, gti, `{{if not .Transparent}}var lengthBuf{{ .Name }} = {{ .TupleHeaderAsByteString }}{{end}}
magik6k marked this conversation as resolved.
Show resolved Hide resolved
func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error {
if t == nil {
{{if not .Transparent}}if t == nil {
_, err := w.Write(cbg.CborNull)
return err
}
}{{end}}

cw := cbg.NewCborWriter(w)

{{if not .Transparent}}
if _, err := cw.Write(lengthBuf{{ .Name }}); err != nil {
return err
}
{{end}}
`)
if err != nil {
return err
}

for _, f := range gti.Fields {
fmt.Fprintf(w, "\n\t// t.%s (%s) (%s)", f.Name, f.Type, f.Type.Kind())
f.Name = "t." + f.Name
if f.Name == "." {
f.Name = "(*t)"
} else {
f.Name = "t." + f.Name
}
fmt.Fprintf(w, "\n\t// %s (%s) (%s)", f.Name, f.Type, f.Type.Kind())

switch f.Type.Kind() {
case reflect.String:
Expand Down Expand Up @@ -1428,7 +1468,7 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) {
*t = {{.Name}}{}

cr := cbg.NewCborReader(r)

{{ if not .Transparent }}
magik6k marked this conversation as resolved.
Show resolved Hide resolved
maj, extra, err := {{ ReadHeader "cr" }}
if err != nil {
return err
Expand All @@ -1446,15 +1486,24 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) (err error) {
if extra != {{ len .Fields }} {
return fmt.Errorf("cbor input had wrong number of fields")
}

{{ else }}
var maj byte
var extra uint64
_ = maj
_ = extra
{{ end }}
`)
if err != nil {
return err
}

for _, f := range gti.Fields {
fmt.Fprintf(w, "\t// t.%s (%s) (%s)\n", f.Name, f.Type, f.Type.Kind())
f.Name = "t." + f.Name
if f.Name == "." {
f.Name = "(*t)" // self
} else {
f.Name = "t." + f.Name
}
fmt.Fprintf(w, "\t// %s (%s) (%s)\n", f.Name, f.Type, f.Type.Kind())

switch f.Type.Kind() {
case reflect.String:
Expand Down Expand Up @@ -1539,6 +1588,10 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
}
}

if gti.Transparent {
return fmt.Errorf("transparent fields not supported in map mode, use tuple encoding (outcome should be the same)")
}

err := doTemplate(w, gti, `func (t *{{ .Name }}) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)
Expand Down
6 changes: 6 additions & 0 deletions testgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ func main() {
types.FixedArrays{},
types.ThingWithSomeTime{},
types.BigField{},
types.IntArray{},
types.IntAliasArray{},
types.TupleIntArray{},
types.IntArrayNewType{},
types.IntArrayAliasNewType{},
types.MapTransparentType{},
); err != nil {
panic(err)
}
Expand Down
Loading