Files
dolt/nomdl/codegen/ref.tmpl
Erik Arvidsson 30cc7d518f Make Equals compare by Ref (after comparing TypeRef)
This changes equal to compare by ref. Since computing the ref can be
expensive we first check that the type refs are equal.

Fixes #532
2015-10-30 16:50:49 -04:00

47 lines
1.1 KiB
Cheetah

{{$typesPackage := .TypesPackage}}
// {{.Name}}
type {{.Name}} struct {
target ref.Ref
ref *ref.Ref
}
func New{{.Name}}(target ref.Ref) {{.Name}} {
return {{.Name}}{target, &ref.Ref{}}
}
func (r {{.Name}}) TargetRef() ref.Ref {
return r.target
}
func (r {{.Name}}) Ref() ref.Ref {
return {{$typesPackage}}EnsureRef(r.ref, r)
}
func (r {{.Name}}) Equals(other {{$typesPackage}}Value) bool {
return other != nil && __typeRefFor{{.Name}}.Equals(other.TypeRef()) && r.Ref() == other.Ref()
}
func (r {{.Name}}) Chunks() []ref.Ref {
return r.TypeRef().Chunks()
}
func {{.Name}}FromVal(val {{$typesPackage}}Value) {{.Name}} {
// TODO: Do we still need FromVal?
if val, ok := val.({{.Name}}); ok {
return val
}
return New{{.Name}}(val.({{$typesPackage}}Ref).TargetRef())
}
{{template "type_ref.tmpl" .}}
func (r {{.Name}}) TargetValue(cs chunks.ChunkSource) {{userType .ElemType}} {
return {{valueToUser (printf "%sReadValue(r.target, cs)" $typesPackage) .ElemType}}
}
func (r {{.Name}}) SetTargetValue(val {{userType .ElemType}}, cs chunks.ChunkSink) {{.Name}} {
return New{{.Name}}({{$typesPackage}}WriteValue({{userToValue "val" .ElemType}}, cs))
}