Skip to content

Commit

Permalink
Merge pull request #78 from whyrusleeping/feat/const-vals
Browse files Browse the repository at this point in the history
Allow 'const' fields to be declared
  • Loading branch information
whyrusleeping authored Dec 15, 2022
2 parents c09a31a + dae9023 commit 76063ba
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 7 deletions.
28 changes: 24 additions & 4 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type Field struct {
Pointer bool
Type reflect.Type
Pkg string
Const *string

OmitEmpty bool
IterLabel string
Expand Down Expand Up @@ -204,6 +205,14 @@ func ParseTypeInfo(i interface{}) (*GenTypeInfo, error) {
usrMaxLen = val
}

var constval *string
if cv, hasconst := tags["const"]; hasconst {
if ft.Kind() != reflect.String {
return nil, fmt.Errorf("const vals are only supported for string types")
}
constval = &cv
}

_, omitempty := tags["omitempty"]

out.Fields = append(out.Fields, Field{
Expand All @@ -214,6 +223,7 @@ func ParseTypeInfo(i interface{}) (*GenTypeInfo, error) {
Pkg: pkg,
OmitEmpty: omitempty,
MaxLen: usrMaxLen,
Const: constval,
})
}

Expand Down Expand Up @@ -297,6 +307,16 @@ func emitCborMarshalStringField(w io.Writer, f Field) error {
`)
}

if f.Const != nil {
return doTemplate(w, f, `
{{ MajorType "cw" "cbg.MajTextString" (print "len(" .Name ")") }}
if _, err := io.WriteString(w, string("{{ .Const }}")); err != nil {
return err
}
`)

}

return doTemplate(w, f, `
if len({{ .Name }}) > {{ MaxLen .MaxLen "cbg.MaxLength" }} {
return xerrors.Errorf("Value in field {{ .Name | js }} was too long")
Expand Down Expand Up @@ -1228,12 +1248,12 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
}

if hasOmitEmpty {
fmt.Fprintln(w, "var emptyFieldCount int")
fmt.Fprintf(w, "fieldCount := %d\n", len(gti.Fields))
for _, f := range gti.Fields {
if f.OmitEmpty {
err := doTemplate(w, f, `
if t.{{ .Name }} == nil {
emptyFieldCount++
fieldCount--
}
`)
if err != nil {
Expand All @@ -1243,10 +1263,10 @@ func emitCborMarshalStructMap(w io.Writer, gti *GenTypeInfo) error {
}

fmt.Fprintf(w, `
if _, err := cw.Write(cbg.CborEncodeMajorType(cbg.MajMap, uint64(%d - emptyFieldCount))); err != nil {
if _, err := cw.Write(cbg.CborEncodeMajorType(cbg.MajMap, uint64(fieldCount))); err != nil {
return err
}
`, len(gti.Fields))
`)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions testgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
types.SimpleStructV2{},
types.RenamedFields{},
types.TestEmpty{},
types.TestConstField{},
); err != nil {
panic(err)
}
Expand Down
145 changes: 142 additions & 3 deletions testing/cbor_map_gen.go

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

5 changes: 5 additions & 0 deletions testing/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ type TestEmpty struct {
Foo *string `cborgen:"omitempty"`
Cat int64
}

type TestConstField struct {
Cats string `cborgen:"const=dogsdrool"`
Thing int64
}

0 comments on commit 76063ba

Please sign in to comment.