mirror of
https://github.com/dolthub/dolt.git
synced 2026-01-26 10:37:04 -06:00
149 lines
4.5 KiB
Cheetah
149 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}}
|
|
|
|
var __typeRefFor{{.Name}} = types.MakeTypeRef("{{.Name}}", __{{.PackageName}}PackageInFile_{{.FileID}}_CachedRef)
|
|
|
|
func (m {{.Name}}) TypeRef() types.TypeRef {
|
|
return __typeRefFor{{.Name}}
|
|
}
|
|
|
|
func init() {
|
|
types.RegisterFromValFunction(__typeRefFor{{.Name}}, func(v types.Value) types.NomsValue {
|
|
return {{.Name}}FromVal(v)
|
|
})
|
|
}
|
|
|
|
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 types.Value) bool {
|
|
if other, ok := other.({{.Name}}); ok {
|
|
return s.m.Equals(other.m)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (s {{.Name}}) Ref() ref.Ref {
|
|
return s.m.Ref()
|
|
}
|
|
|
|
func (s {{.Name}}) Chunks() []types.Future {
|
|
return s.m.Chunks()
|
|
}
|
|
|
|
{{$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}}
|
|
|