Documentation ¶
Overview ¶
Package reflect2go generates the Go code for a given reflect.Type.
This is useful when you want to generate the Go code for a dynamically defined type, like with reflect.StructOf.
An example use case is a program that updates or manipulates the tags on the fields of an existing struct.
Index ¶
Examples ¶
Constants ¶
const DefaultCommentLineWrap = 79
DefaultCommentLineWrap is the default CommentLineWrap limit used to wrap all comments.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type GoFile ¶
type GoFile struct {
// contains filtered or unexported fields
}
GoFile represents and renders a file with Golang code.
Example ¶
package main import ( "fmt" "os" "reflect" "strings" "github.com/AdamSLevy/reflect2go" ) func main() { // We will generate a new type based on this predefined Original type. // The updated type will include an additional `env` struct field tag // based on the existing `json` tag. type Original struct { A int `json:"a"` B int `json:"b"` } typ := reflect.TypeOf(Original{}) var fields []reflect.StructField for i := 0; i < typ.NumField(); i++ { sf := typ.Field(i) val := strings.ToUpper(sf.Tag.Get("json")) sf.Tag += reflect.StructTag(fmt.Sprintf(" env:%q", val)) fields = append(fields, sf) } newTyp := reflect.StructOf(fields) gofile, err := reflect2go.NewGoFile("pkg", reflect2go.DefineType("UpdatedTags", newTyp)) if err != nil { panic(err) } if err := gofile.Render(os.Stdout); err != nil { panic(err) } }
Output: package pkg type UpdatedTags struct { A int `json:"a" env:"A"` B int `json:"b" env:"B"` }
func Must ¶
Must returns the GoFile but panics if err is not nil.
err := Must(NewGoFile("pkg", DefineType("X", reflect.TypeOf(struct{}{})), DefineType("Y", reflect.TypeOf(struct{}{}), Comment("Y is a type.")), )).Render(os.Stdout)
func NewGoFile ¶
NewGoFile returns a GoFile that belongs to the package with import path pkgPath.
The pkgPath is used to identify reflected types that belong to the same package, and thus don't require a package prefix in their declaration (e.g. pkg.Type vs Type).
The file's package declaration will use path.Base(pkgPath) by default. Use the Option PkgName to override this.
All comments are word wrapped at the limit set by CommentLineWrap, which defaults to DefaultCommentLineWrap.
The order of Options has no significance or affect on the output. Comments are wrapped and defined types are sorted alphabetically prior to output.
func (*GoFile) DefineType ¶
DefineType adds a new type declaration to the GoFile. An error is returned if the name has already been used on a type declaration within the GoFile.
type Option ¶
Option is an optional setting for NewGoFile.
func CommentLineWrap ¶
CommentLineWrap sets the limit used to wrap all comments on word boundaries.
func DefineType ¶
func DefineType(name string, typ reflect.Type, opts ...TypeOption) Option
DefineType defines a type on the NewGoFile.
It is equivalent to calling GoFile.DefineType on an existing GoFile.
func PkgComment ¶
PkgComment sets a top level package comment which immediately precedes the package declaration.
func PkgName ¶
PkgName overrides the package name, which by default is derived from the path.Base of the package path passed to NewGoFile.
func PreambleComment ¶
PreambleComment adds a top level comment that precedes all other content in the rendered GoFile. A blank line is added immediately after the comment to avoid it being confused with a package comment.
type TypeOption ¶
type TypeOption func(*typeDef)
TypeOption is an option for DefineType.
func Comment ¶
func Comment(cmt string) TypeOption
Comment adds a top level comment to a defined type.