Skip to content

Commit

Permalink
Merge pull request #15 from whyrusleeping/feat/array
Browse files Browse the repository at this point in the history
Support array types
  • Loading branch information
whyrusleeping authored Apr 14, 2020
2 parents 3d27c14 + 6c356c7 commit 429a0b5
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
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

0 comments on commit 429a0b5

Please sign in to comment.