mirror of
https://github.com/dolthub/dolt.git
synced 2026-02-10 10:30:57 -06:00
In pursuit of issue #654, we want to be able to figure out all the refs contained in a given Value, along with the Types of the Values to which those refs point. Value.Chunks() _almost_ met those needs, but it returned a slice of ref.Ref, which doesn't convey any type info. To address this, this patch does two things: 1) RefBase embeds the Value interface, and 2) Chunks() now returns []types.RefBase RefBase now provides Type() as well, by virtue of embedding Value, so callers can just iterate through the slice returned from Chunks() and gather type info for all the refs embedded in a given Value. I went all the way and made RefBase a Value instead of just adding the Type() method because both types.Ref and the generated Ref types are actually all Values, and doing so allowed me to change the definition of refBuilderFunc in package_registry.go to be more precise. It now returns RefBase instead of just Value.
137 lines
4.0 KiB
Cheetah
137 lines
4.0 KiB
Cheetah
{{$typesPackage := .TypesPackage}}
|
|
|
|
// {{.Name}}
|
|
|
|
type {{.Name}} struct {
|
|
m {{$typesPackage}}Map
|
|
ref *ref.Ref
|
|
}
|
|
|
|
func New{{.Name}}() {{.Name}} {
|
|
return {{.Name}}{ {{$typesPackage}}NewTypedMap(__typeFor{{.Name}}), &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}}NewTypedMap(__typeFor{{.Name}}, 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 && __typeFor{{.Name}}.Equals(other.Type()) && m.Ref() == other.Ref()
|
|
}
|
|
|
|
func (m {{.Name}}) Ref() ref.Ref {
|
|
return {{$typesPackage}}EnsureRef(m.ref, m)
|
|
}
|
|
|
|
func (m {{.Name}}) Chunks() (chunks []{{$typesPackage}}RefBase) {
|
|
chunks = append(chunks, m.Type().Chunks()...)
|
|
chunks = append(chunks, m.m.Chunks()...)
|
|
return
|
|
}
|
|
|
|
func (m {{.Name}}) ChildValues() []{{$typesPackage}}Value {
|
|
return append([]{{$typesPackage}}Value{}, m.m.ChildValues()...)
|
|
}
|
|
|
|
// A Noms Value that describes {{.Name}}.
|
|
var __typeFor{{.Name}} {{$typesPackage}}Type
|
|
|
|
func (m {{.Name}}) Type() {{$typesPackage}}Type {
|
|
return __typeFor{{.Name}}
|
|
}
|
|
|
|
func init() {
|
|
__typeFor{{.Name}} = {{toTypesType .Type .FileID .PackageName}}
|
|
{{$typesPackage}}RegisterValue(__typeFor{{.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}})
|
|
})
|
|
}
|
|
|
|
func (m {{.Name}}) IterAllP(concurrency int, cb {{.Name}}IterAllCallback) {
|
|
m.m.IterAllP(concurrency, 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}} {
|
|
out := m.m.Filter(func(k, v {{$typesPackage}}Value) bool {
|
|
return cb({{valueToUser "k" .KeyType}}, {{valueToUser "v" .ValueType}})
|
|
})
|
|
return {{.Name}}{out, &ref.Ref{}}
|
|
}
|