Files
dolt/nomdl/codegen/struct.tmpl
2015-09-25 17:20:23 -04:00

147 lines
4.5 KiB
Cheetah

// {{.Name}}
type {{.Name}} struct {
m types.Map
}
func New{{.Name}}() {{.Name}} {
return {{.Name}}{types.NewMap(
types.NewString("$name"), types.NewString("{{.Name}}"),
types.NewString("$type"), types.MakeTypeRef("{{.Name}}", __{{.PackageName}}PackageInFile_{{.FileID}}_CachedRef),
{{range .Fields}}{{if (not .Optional)}}types.NewString("{{.Name}}"), {{valueZero .T}},
{{end}}{{end}}{{if .HasUnion}}types.NewString("$unionIndex"), types.UInt32(0),
types.NewString("$unionValue"), {{valueZero .UnionZeroType}},{{end}}
)}
}
{{if .CanUseDef}}
type {{.Name}}Def struct {
{{range .Fields}}{{title .Name}} {{defType .T}}
{{end}}{{if .HasUnion}}__unionIndex uint32
__unionValue interface{}
{{end}}}
func (def {{.Name}}Def) New() {{.Name}} {
return {{.Name}}{
types.NewMap(
types.NewString("$name"), types.NewString("{{.Name}}"),
types.NewString("$type"), types.MakeTypeRef("{{.Name}}", __{{.PackageName}}PackageInFile_{{.FileID}}_CachedRef),
{{range .Fields}}types.NewString("{{.Name}}"), {{defToValue (print "def." (title .Name)) .T}},
{{end}}{{if .HasUnion}}types.NewString("$unionIndex"), types.UInt32(def.__unionIndex),
types.NewString("$unionValue"), def.__unionDefToValue(),
{{end}}
)}
}
func (s {{.Name}}) Def() (d {{.Name}}Def) {
{{range .Fields}}{{if .Optional}}if v, ok := s.m.MaybeGet(types.NewString("{{.Name}}")); ok {
d.{{title .Name}} = {{valueToDef "v" .T}}
}{{else}}d.{{title .Name}} = {{valueToDef (printf `s.m.Get(types.NewString("%s"))` .Name) .T}}{{end}}
{{end}}{{if .HasUnion}}d.__unionIndex = uint32(s.m.Get(types.NewString("$unionIndex")).(types.UInt32))
d.__unionValue = s.__unionValueToDef()
{{end}}return
}
{{if .HasUnion}}
func (def {{.Name}}Def) __unionDefToValue() types.Value {
switch def.__unionIndex {
{{range $index, $field := .Choices}}case {{$index}}:
return {{defToValue (printf "def.__unionValue.(%s)" (defType .T)) .T}}
{{end}}}
panic("unreachable")
}
func (s {{.Name}}) __unionValueToDef() interface{} {
switch uint32(s.m.Get(types.NewString("$unionIndex")).(types.UInt32)) {
{{range $index, $field := .Choices}}case {{$index}}:
return {{valueToDef `s.m.Get(types.NewString("$unionValue"))` .T}}
{{end}}}
panic("unreachable")
}
{{end}}
{{end}}
// Creates and returns a Noms Value that describes {{.Name}}.
func __typeRefOf{{.Name}}() types.TypeRef {
return types.MakeStructTypeRef("{{.Name}}",
[]types.Field{
{{range .Fields}}types.Field{"{{.Name}}", {{toTypesTypeRef .T}}, {{.Optional}}},
{{end}}
},
{{if .HasUnion}}[]types.Field{
{{range .Choices}}types.Field{"{{.Name}}", {{toTypesTypeRef .T}}, {{.Optional}}},
{{end}}
}{{else}}nil{{end}})
}
func {{.Name}}FromVal(val types.Value) {{.Name}} {
// TODO: Validate here
return {{.Name}}{val.(types.Map)}
}
func (s {{.Name}}) NomsValue() types.Value {
return s.m
}
func (s {{.Name}}) Equals(other {{.Name}}) bool {
return s.m.Equals(other.m)
}
func (s {{.Name}}) Ref() ref.Ref {
return s.m.Ref()
}
func (s {{.Name}}) Type() types.TypeRef {
return s.m.Get(types.NewString("$type")).(types.TypeRef)
}
{{$name := .Name}}
{{range $index, $field := .Fields}}
{{if .Optional}}
func (s {{$name}}) {{title .Name}}() (v {{userType .T}}, ok bool) {
var vv types.Value
if vv, ok = s.m.MaybeGet(types.NewString("{{.Name}}")); ok {
v = {{valueToUser "vv" .T}}
}
return
}
{{else}}
func (s {{$name}}) {{title .Name}}() {{userType .T}} {
return {{valueToUser (printf `s.m.Get(types.NewString("%s"))` .Name) .T}}
}
{{end}}
func (s {{$name}}) Set{{title .Name}}(val {{userType .T}}) {{$name}} {
return {{$name}}{s.m.Set(types.NewString("{{.Name}}"), {{userToValue "val" .T}})}
}
{{end}}
{{$canUseDef := .CanUseDef}}
{{range $index, $field := .Choices}}
func (s {{$name}}) {{title .Name}}() (val {{userType .T}}, ok bool) {
if s.m.Get(types.NewString("$unionIndex")).(types.UInt32) != {{$index}} {
return
}
return {{valueToUser `s.m.Get(types.NewString("$unionValue"))` .T}}, true
}
func (s {{$name}}) Set{{title .Name}}(val {{userType .T}}) {{$name}} {
return {{$name}}{s.m.Set(types.NewString("$unionIndex"), types.UInt32({{$index}})).Set(types.NewString("$unionValue"), {{userToValue "val" .T}})}
}
{{if $canUseDef}}
func (def {{$name}}Def) {{title .Name}}() (val {{defType .T}}, ok bool) {
if def.__unionIndex != {{$index}} {
return
}
return def.__unionValue.({{defType .T}}), true
}
func (def {{$name}}Def) Set{{title .Name}}(val {{defType .T}}) {{$name}}Def {
def.__unionIndex = {{$index}}
def.__unionValue = val
return def
}
{{end}}
{{end}}