func (s *structGenerator) Reader(g Generator, spec *compile.StructSpec) (string, error) { name, err := readerFuncName(spec) if err != nil { return "", err } if s.HasReader(name) { return name, nil } err = g.DeclareFromTemplate( ` <$wire := import "go.uber.org/thriftrw/wire"> <$v := newVar "v"> <$w := newVar "w"> func <.Name>(<$w> <$wire>.Value) (<typeReference .Spec>, error) { var <$v> <typeName .Spec> err := <$v>.FromWire(<$w>) return &<$v>, err } `, struct { Name string Spec *compile.StructSpec }{Name: name, Spec: spec}, ) return name, wrapGenerateError(spec.ThriftName(), err) }
func structure(g Generator, spec *compile.StructSpec) error { name, err := goName(spec) if err != nil { return err } fg := fieldGroupGenerator{ Namespace: NewNamespace(), Name: name, Fields: spec.Fields, IsUnion: spec.Type == ast.UnionType, IsException: spec.Type == ast.ExceptionType, } if err := fg.Generate(g); err != nil { return wrapGenerateError(spec.ThriftName(), err) } if spec.Type == ast.ExceptionType { err := g.DeclareFromTemplate( ` <$v := newVar "v"> func (<$v> *<typeName .>) Error() string { return <$v>.String() } `, spec) if err != nil { return wrapGenerateError(spec.ThriftName(), err) } } return nil // TODO(abg): For all struct types, handle the case where fields are named // ToWire or FromWire. // TODO(abg): For exceptions, handle the case where a field is named // Error. }