// Write vector code // "val{{.Level}}" should be pointer deference func (v *GenTypeVector) Write(level int) (res string, err error) { res = ` {{ if eq .T.Bare false }} if err = encoder.WriteTypeBegin(TL_ID_VECTOR_T); err != nil { return err } {{ end }} {{ if gt .T.Restrict 0 }} if len(val{{.Level}}) != {{ .T.Restrict }} { return fmt.Errorf("bad multi size {{ .T.Restrict }} != %d", len(val{{.Level}})) } {{ else }} if err = encoder.WriteInt(int32(len(val{{.Level}}))); err != nil { return err } {{ end }} for _, val{{.Deep}} := range val{{.Level}} { {{.T.Sub.Write .Deep}} } if err = encoder.WriteTypeEnd(); err != nil { return err } ` res, err = tpl.RunStr("vec", res, map[string]interface{}{ "T": v, "Level": level, "Deep": level + 1, }) return }
func (p GenField) WRITE() (res string, err error) { return tpl.RunStr("field_write", ` func (t *{{ .BareName }}) writeField_{{ .Ident.ToName }}(encoder tl.Encoder) (err error) { val0 := t.{{ .Ident.ToName }} {{ .GType.Write 0 }} return }`, p) }
func Test_TplMap(t *testing.T) { ctx := map[string]interface{}{} ctx["A"] = 1 ctx["B"] = "T" res, err := tpl.RunStr("TEST", "{{ .A }} {{ .B }}", ctx) assert.NoError(t, err) assert.Equal(t, "1 T", res) }
func (b GenBox) Imports() (res string, err error) { return tpl.RunStr("box_head", ` "fmt" "github.com/nu7hatch/gouuid" "{{ .Options.Core }}" . "{{ .Options.Core }}/builtin"`, b) }
func (s *GenTypeSimple) Write(level int) (res string, err error) { res = ` if err = (&val{{.Level}}).Write(encoder); err != nil { return err } ` res, err = tpl.RunStr("boxed", res, map[string]interface{}{"Level": level}) return }
func (p GenField) READ() (res string, err error) { return tpl.RunStr("field_read", ` func (t *{{ .BareName }}) readField_{{ .Ident.ToName }}(decoder tl.Decoder, waitress tl.Waitress, bind_uuid uuid.UUID) (err error) { val0 := &t.{{.Ident.ToName}} {{ .GType.Read 0 }} return }`, p) }
func (r *GenRPCWithResult) Handler() (res string, err error) { res = ` // {{.Bare.DECL}} {{.Bare.Ident.ToName}}({{.Args .Bare "ctx tl.HandlerContext" ""}}) ({{.ArgsOut "" "err error"}}) // {{.Bare.DECL}} RpcResult handler {{.Bare.Ident.ToName}}Result({{.ArgsOut "ctx tl.HandlerContext, reqMsgId int64" "rpcErr error"}}) (err error) ` res, err = tpl.RunStr("rpc_handler", res, r) return }
func (s *GenTypeBuiltin) Write(level int) (res string, err error) { res = ` if err = encoder.Write{{.Accessor}}(val{{.Level}}); err != nil { return err } ` res, err = tpl.RunStr("builtin", res, map[string]interface{}{ "Level": level, "Accessor": s.Accessor, }) return }
func (b GenBox) Header() (res string, err error) { return tpl.RunStr("box_head", `package {{ .Options.Package }} import ( {{.Imports}} ) var _{{ .Stub }}_err = fmt.Errorf("stub") const ({{.BareConsts}} {{ .Stub }}_VERSION = TL_VERSION )`, b) }
func (p *GenProcessor) Handler() (res string, err error) { res = `package {{ .Options.Package }} import "{{ .Options.Core }}" type Handler interface { {{ range .RPCs }} {{.Handler}} {{ end }} } ` res, err = tpl.RunStr("pipe_handler", res, p) return }
func (v *GenTypeBuiltin) Read(level int) (res string, err error) { res = ` {{if gt .Level 0}} val{{.Level}} := new({{.T.DeclType}}) {{end}} if *val{{.Level}}, err = decoder.Read{{.T.Accessor}}(); err != nil { return err } ` res, err = tpl.RunStr("builtin_read", res, map[string]interface{}{ "Level": level, "T": v, }) return }
func (v *GenTypeSimple) Read(level int) (res string, err error) { res = ` {{if gt .Level 0}} val{{.Level}} := new({{.T.DeclType}}) {{ end }} if err = val{{.Level}}.Read(decoder, waitress, bind_uuid); err != nil { return err } ` res, err = tpl.RunStr("simple_read", res, map[string]interface{}{ "Level": level, "T": v, }) return }
func (r *GenRPCWithNaked) Handler() (res string, err error) { res = `{{$GBOX := .GBox}}{{$RPC := .}} {{range .GBox.Bares}} // Handler for {{.DECL}} // Use ctx.Processor.WaitResult() to register // expected results in waitress {{.Ident.ToName}}({{$RPC.Args . "ctx tl.HandlerContext" ""}}) (res {{$GBOX.BoxName}}, resCtx tl.ResponseContext, err error){{end}} // {{ .GBox.Ident.ToName }} result handler for // {{range .GBox.Bares}} // {{.DECL}}{{end}} {{ .GBox.Ident.ToName }} (ctx tl.HandlerContext, res {{.GBox.BoxName}}) (err error) ` res, err = tpl.RunStr("rpc_handler", res, r) return }
// Read vector func (v *GenTypeVector) Read(level int) (res string, err error) { res = ` {{ if eq .T.Bare false }} h{{.Level}}, err := decoder.ReadTypeBegin() if err != nil { return err } if h{{.Level}} != TL_ID_VECTOR_T { return fmt.Errorf("bad vector crc %0x08d", h{{.Level}}) } {{ end }} {{ if gt .T.Restrict 0 }} n{{.Level}} := {{ .T.Restrict }} {{ else }} n{{.Level}}, err := decoder.ReadInt() if err != nil { return err } {{ end }} {{if gt .Level 0 }} val{{.Level}} := new({{.T.DeclType}}) {{end}} for i{{.Level}} := 0; i{{.Level}} < int(n{{.Level}}); i{{.Level}}++ { {{ .T.Sub.Read .Deep }} *val{{.Level}} = append(*val{{.Level}}, *val{{.Deep}}) } if len(*val{{.Level}}) != int(n{{.Level}}) { return fmt.Errorf("bad multi size %d != %d", n{{.Level}}, len(*val{{.Level}})) } ` res, err = tpl.RunStr("vec_read", res, map[string]interface{}{ "Level": level, "Deep": level + 1, "T": v, }) return }
// Struct declaration func (b GenBare) STRUCT() (string, error) { return tpl.RunStr(b.Ident().ToName(), TPL_BARE_STRUCT, b) }
func (b GenBox) BareConsts() (res string, err error) { return tpl.RunStr("box_head", `{{ range .Bares }} {{ .BareConst }} = 0x{{ .DECL.ID | printf "%0x" }}{{ end }}`, b) }
func (b GenBox) Body() (res string, err error) { return tpl.RunStr("box_body", ` {{ $BOX := .BoxName }} type {{ $BOX }} struct { bare tl.Bare } // Bind to one of // {{ range .Bares }} // {{ .BareName }}{{ end }} // func (b *{{ $BOX }}) Bind(id uint32) (err error) { switch id { {{ range .Bares }} case {{ .BareConst }}: b.bare = &{{ .BareName }}{}{{ end }} default: err = fmt.Errorf("bad constructor %0x08d for {{ $BOX }}", id) } return } func (b *{{ $BOX }}) Bare() (res tl.Bare, err error) { if bare, ok := b.Strip().(tl.Bare); bare != nil && ok { res = bare } else { err = fmt.Errorf("bad bare %#v in {{ $BOX }}", bare) } return } func (b *{{ $BOX }}) Empty() tl.Codec { return &{{ $BOX }}{} } func (b *{{ $BOX }}) Read(decoder tl.Decoder, waitress tl.Waitress, bind_uuid uuid.UUID) (err error) { id, err := decoder.ReadTypeBegin() if err != nil { return } err = b.Bind(id) if err != nil { return } bare, err := b.Bare() if err != nil { return } err = bare.Read(decoder, waitress, bind_uuid) return } func (b *{{ $BOX }}) Write(encoder tl.Encoder) (err error) { bare, err := b.Bare() if err != nil { return } err = encoder.WriteTypeBegin(bare.GetID()) if err != nil { return } err = bare.Write(encoder) return } func (b *{{ $BOX }}) Strip() interface{} { return b.bare } `, b) }