Files
dolt/nomdl/codegen/map.tmpl
Erik Arvidsson 9ba1c4a508 Remove InternalImplementation() from List, Map & Set
Instead register a function to extract the internal backing store.
2015-11-04 18:57:36 -05:00

131 lines
3.6 KiB
Cheetah

{{$typesPackage := .TypesPackage}}
// {{.Name}}
type {{.Name}} struct {
m {{$typesPackage}}Map
ref *ref.Ref
}
func New{{.Name}}() {{.Name}} {
return {{.Name}}{ {{$typesPackage}}NewMap(), &ref.Ref{}}
}
{{if .CanUseDef}}
type {{.Name}}Def map[{{defType .KeyType}}]{{defType .ValueType}}
func (def {{.Name}}Def) New() {{.Name}} {
kv := make([]{{$typesPackage}}Value, 0, len(def)*2)
for k, v := range def {
kv = append(kv, {{defToValue "k" .KeyType}}, {{defToValue "v" .ValueType}})
}
return {{.Name}}{ {{$typesPackage}}NewMap(kv...), &ref.Ref{}}
}
func (m {{.Name}}) Def() {{.Name}}Def {
def := make(map[{{defType .KeyType}}]{{defType .ValueType}})
m.m.Iter(func(k, v {{$typesPackage}}Value) bool {
def[{{valueToDef "k" .KeyType}}] = {{valueToDef "v" .ValueType}}
return false
})
return def
}
{{end}}
func (m {{.Name}}) Equals(other {{$typesPackage}}Value) bool {
return other != nil && __typeRefFor{{.Name}}.Equals(other.TypeRef()) && m.Ref() == other.Ref()
}
func (m {{.Name}}) Ref() ref.Ref {
return {{$typesPackage}}EnsureRef(m.ref, m)
}
func (m {{.Name}}) Chunks() (chunks []ref.Ref) {
chunks = append(chunks, m.TypeRef().Chunks()...)
chunks = append(chunks, m.m.Chunks()...)
return
}
// A Noms Value that describes {{.Name}}.
var __typeRefFor{{.Name}} {{$typesPackage}}TypeRef
func (m {{.Name}}) TypeRef() {{$typesPackage}}TypeRef {
return __typeRefFor{{.Name}}
}
func init() {
__typeRefFor{{.Name}} = {{toTypesTypeRef .Type .FileID .PackageName}}
{{$typesPackage}}RegisterValue(__typeRefFor{{.Name}}, builderFor{{.Name}}, readerFor{{.Name}})
}
func builderFor{{.Name}}(v {{$typesPackage}}Value) {{$typesPackage}}Value {
return {{.Name}}{v.({{$typesPackage}}Map), &ref.Ref{}}
}
func readerFor{{.Name}}(v {{$typesPackage}}Value) {{$typesPackage}}Value {
return v.({{.Name}}).m
}
func (m {{.Name}}) Empty() bool {
return m.m.Empty()
}
func (m {{.Name}}) Len() uint64 {
return m.m.Len()
}
func (m {{.Name}}) Has(p {{userType .KeyType}}) bool {
return m.m.Has({{userToValue "p" .KeyType}})
}
func (m {{.Name}}) Get(p {{userType .KeyType}}) {{userType .ValueType}} {
return {{valueToUser (printf "m.m.Get(%s)" (userToValue "p" .KeyType)) .ValueType}}
}
func (m {{.Name}}) MaybeGet(p {{userType .KeyType}}) ({{userType .ValueType}}, bool) {
v, ok := m.m.MaybeGet({{userToValue "p" .KeyType}})
if !ok {
return {{userZero .ValueType}}, false
}
return {{valueToUser "v" .ValueType}}, ok
}
func (m {{.Name}}) Set(k {{userType .KeyType}}, v {{userType .ValueType}}) {{.Name}} {
return {{.Name}}{m.m.Set({{userToValue "k" .KeyType}}, {{userToValue "v" .ValueType}}), &ref.Ref{}}
}
// TODO: Implement SetM?
func (m {{.Name}}) Remove(p {{userType .KeyType}}) {{.Name}} {
return {{.Name}}{m.m.Remove({{userToValue "p" .KeyType}}), &ref.Ref{}}
}
type {{.Name}}IterCallback func(k {{userType .KeyType}}, v {{userType .ValueType}}) (stop bool)
func (m {{.Name}}) Iter(cb {{.Name}}IterCallback) {
m.m.Iter(func(k, v {{$typesPackage}}Value) bool {
return cb({{valueToUser "k" .KeyType}}, {{valueToUser "v" .ValueType}})
})
}
type {{.Name}}IterAllCallback func(k {{userType .KeyType}}, v {{userType .ValueType}})
func (m {{.Name}}) IterAll(cb {{.Name}}IterAllCallback) {
m.m.IterAll(func(k, v {{$typesPackage}}Value) {
cb({{valueToUser "k" .KeyType}}, {{valueToUser "v" .ValueType}})
})
}
type {{.Name}}FilterCallback func(k {{userType .KeyType}}, v {{userType .ValueType}}) (keep bool)
func (m {{.Name}}) Filter(cb {{.Name}}FilterCallback) {{.Name}} {
nm := New{{.Name}}()
m.IterAll(func(k {{userType .KeyType}}, v {{userType .ValueType}}) {
if cb(k, v) {
nm = nm.Set(k, v)
}
})
return nm
}