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

Support array types #15

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type Field struct {

func typeName(pkg string, t reflect.Type) string {
switch t.Kind() {
case reflect.Array:
return fmt.Sprintf("[%d]%s", t.Len(), typeName(pkg, t.Elem()))
case reflect.Slice:
return "[]" + typeName(pkg, t.Elem())
case reflect.Ptr:
Expand All @@ -73,6 +75,14 @@ func (f Field) ElemName() string {
return typeName(f.Pkg, f.Type.Elem())
}

func (f Field) IsArray() bool {
return f.Type.Kind() == reflect.Array
}

func (f Field) Len() int {
return f.Type.Len()
}

type GenTypeInfo struct {
Name string
Fields []Field
Expand Down Expand Up @@ -466,6 +476,8 @@ func emitCborMarshalStructTuple(w io.Writer, gti *GenTypeInfo) error {
if err := emitCborMarshalInt64Field(w, f); err != nil {
return err
}
case reflect.Array:
fallthrough
case reflect.Slice:
if err := emitCborMarshalSliceField(w, f); err != nil {
return err
Expand Down Expand Up @@ -825,9 +837,17 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error {
if maj != cbg.MajArray {
return fmt.Errorf("expected cbor array")
}
{{if .IsArray}}
if extra != {{ .Len }} {
return fmt.Errorf("expected array to have {{ .Len }} elements")
}
{{ .Name }} = {{ .TypeName }}{}
{{else}}
if extra > 0 {
{{ .Name }} = make({{ .TypeName }}, extra)
}
{{end}}
for {{ .IterLabel }} := 0; {{ .IterLabel }} < int(extra); {{ .IterLabel }}++ {
`)
if err != nil {
Expand Down Expand Up @@ -895,6 +915,8 @@ func emitCborUnmarshalSliceField(w io.Writer, f Field) error {
if err != nil {
return err
}
case reflect.Array:
fallthrough
case reflect.Slice:
nextIter := string([]byte{f.IterLabel[0] + 1})
subf := Field{
Expand Down Expand Up @@ -964,6 +986,8 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) error {
if err := emitCborUnmarshalInt64Field(w, f); err != nil {
return err
}
case reflect.Array:
fallthrough
case reflect.Slice:
if err := emitCborUnmarshalSliceField(w, f); err != nil {
return err
Expand Down Expand Up @@ -1050,6 +1074,8 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
if err := emitCborMarshalUint8Field(w, f); err != nil {
return err
}
case reflect.Array:
fallthrough
case reflect.Slice:
if err := emitCborMarshalSliceField(w, f); err != nil {
return err
Expand Down Expand Up @@ -1141,6 +1167,8 @@ func (t *{{ .Name}}) UnmarshalCBOR(r io.Reader) error {
if err := emitCborUnmarshalUint8Field(w, f); err != nil {
return err
}
case reflect.Array:
fallthrough
case reflect.Slice:
if err := emitCborUnmarshalSliceField(w, f); err != nil {
return err
Expand Down
58 changes: 56 additions & 2 deletions testing/cbor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions testing/cbor_map_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions testing/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
cbg "github.com/whyrusleeping/cbor-gen"
)

const Thingc = 3

type NaturalNumber uint64

type SignedArray struct {
Expand All @@ -26,6 +28,7 @@ type SimpleTypeTwo struct {
Numbers []NaturalNumber
Pizza *uint64
PointyPizza *NaturalNumber
Arrrrrghay [Thingc]SimpleTypeOne
}

type SimpleTypeTree struct {
Expand Down