19. Game struct → AST
type Game struct {
ID int64
Title string
Price int
InDevelopment bool
ShippedAt time.Time
}
ast = go/ast package
ast.GenDecl
20. Game struct → AST
type Game struct {
ID int64
Title string
Price int
InDevelopment bool
ShippedAt time.Time
}
ast = go/ast package
ast.GenDecl
ast.TypeSpec
type (
A struct {
Foo string
}
B struct {
Bar string
}
)
こういう記法もある(怖い
21. Game struct → AST
type Game struct {
ID int64
Title string
Price int
InDevelopment bool
ShippedAt time.Time
}
ast = go/ast package
ast.GenDecl
ast.TypeSpec
ast.Ident
22. Game struct → AST
type Game struct {
ID int64
Title string
Price int
InDevelopment bool
ShippedAt time.Time
}
ast = go/ast package
ast.GenDecl
ast.TypeSpec
ast.Ident
ast.StructType
23. Game struct → AST
type Game struct {
ID int64
Title string
Price int
InDevelopment bool
ShippedAt time.Time
}
ast = go/ast package
ast.GenDecl
ast.TypeSpec
ast.Ident
ast.StructType
ast.FieldList
24. Game struct → AST
type Game struct {
ID int64
Title string
Price int
InDevelopment bool
ShippedAt time.Time
}
ast = go/ast package
ast.GenDecl
ast.TypeSpec
ast.Ident
ast.StructType
ast.FieldList
ast.Field type A struct {
Foo, Bar string
}
こういう記法もある(怖い
25. Game struct → AST
type Game struct {
ID int64
Title string
Price int
InDevelopment bool
ShippedAt time.Time
}
ast = go/ast package
ast.GenDecl
ast.TypeSpec
ast.Ident
ast.StructType
ast.FieldList
ast.Field
ast.Ident
26. Game struct → AST
type Game struct {
ID int64
Title string
Price int
InDevelopment bool
ShippedAt time.Time
}
ast = go/ast package
ast.GenDecl
ast.TypeSpec
ast.Ident
ast.StructType
ast.FieldList
ast.Field
ast.Ident
ast.Ident
30. Goコードの構造
// generated by jwg -output model_json.go .; DO NOT EDIT
package sample
import (
"encoding/json"
"time"
)
// for Game
type GameJson struct {
ID int64 `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Price int `json:"price,omitempty"`
InDevelopment bool `json:"inDevelopment,omitempty"`
ShippedAt time.Time `json:"shippedAt,omitempty"`
}
PackageClause
ImportDecl
TopLevelDecl
31. 俺形式が必要な理由
// generated by jwg -output model_json.go .; DO NOT EDIT
package sample
import (
"encoding/json"
"time"
)
// for Game
type GameJson struct {
ID int64 `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Price int `json:"price,omitempty"`
InDevelopment bool `json:"inDevelopment,omitempty"`
ShippedAt time.Time `json:"shippedAt,omitempty"`
}
正しいPackageClauseの生成には、
TopLevelDecl生成結果の把握が必要! etc..
33. jwgの場合
// BuildStruct represents source code of assembling..
type BuildSource struct {
g *genbase.Generator
pkg *genbase.PackageInfo
typeInfos genbase.TypeInfos
Structs []*BuildStruct
}
// BuildStruct represents struct of assembling..
type BuildStruct struct {
parent *BuildSource
typeInfo *genbase.TypeInfo
Fields []*BuildField
}
// BuildField represents field of BuildStruct.
type BuildField struct {
parent *BuildStruct
fieldInfo *genbase.FieldInfo
Name string
Embed bool
Tag *BuildTag
}
// BuildTag represents tag of BuildField.
type BuildTag struct {
field *BuildField
Name string
Ignore bool // e.g. Secret string `json:"-"`
DoNotEmit bool // e.g. Field int `json:",omitempty"`
String bool // e.g. Int64String int64 `json:",string"`
}