Skip to content

Commit

Permalink
feat: uintstrgen
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Jul 23, 2024
1 parent 619c737 commit 13d07ee
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions devpkg/uintstrgen/uintstr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package defaultergen

import (
"github.com/octohelm/gengo/pkg/gengo"
"go/types"
)

func init() {
gengo.Register(&unitstr{})
}

type unitstr struct{}

func (*unitstr) Name() string {
return "uintstr"
}

func (g *unitstr) GenerateType(c gengo.Context, t *types.Named) error {
if b, ok := t.Obj().Type().Underlying().(*types.Basic); ok {
switch b.Kind() {
case types.Uint64, types.Uint32, types.Uint16, types.Uint8, types.Uint:
c.Render(gengo.Snippet{gengo.T: `
func(id *@Type) UnmarshalText(text []byte) error {
str := string(text)
if len(str) == 0 {
return nil
}
v, err := @strconvParseUint(str, 10, 64)
if err != nil {
return err
}
*id = @Type(v)
return nil
}
func (id @Type) MarshalText() (text []byte, err error) {
if id == 0 {
return nil, nil
}
return []byte(@strconvFormatUint(uint64(id), 10)), nil
}
func (id @Type) String() string {
return @strconvFormatUint(uint64(id), 10)
}
`,
"Type": gengo.ID(t.Obj()),
"strconvParseUint": gengo.ID("strconv.ParseUint"),
"strconvFormatUint": gengo.ID("strconv.FormatUint"),
})
default:

}

}

return nil
}

0 comments on commit 13d07ee

Please sign in to comment.