mirror of
https://github.com/dolthub/dolt.git
synced 2026-01-26 10:37:04 -06:00
NomDL: Ref support
A Ref has a SetValue(v, cs) and a GetValue(cs) Fixes #306
This commit is contained in:
@@ -103,7 +103,7 @@ func (gen *codeGen) defType(t parse.TypeRef) string {
|
||||
case parse.ListKind, parse.MapKind, parse.SetKind, parse.StructKind:
|
||||
return gen.userName(t) + "Def"
|
||||
case parse.RefKind:
|
||||
panic("not yet implemented")
|
||||
return "ref.Ref"
|
||||
case parse.ValueKind:
|
||||
return "types.Value"
|
||||
}
|
||||
@@ -118,10 +118,8 @@ func (gen *codeGen) userType(t parse.TypeRef) string {
|
||||
return "types.Blob"
|
||||
case parse.BoolKind, parse.Float32Kind, parse.Float64Kind, parse.Int16Kind, parse.Int32Kind, parse.Int64Kind, parse.Int8Kind, parse.StringKind, parse.UInt16Kind, parse.UInt32Kind, parse.UInt64Kind, parse.UInt8Kind:
|
||||
return strings.ToLower(primitiveKindToString(k))
|
||||
case parse.EnumKind, parse.ListKind, parse.MapKind, parse.SetKind, parse.StructKind:
|
||||
case parse.EnumKind, parse.ListKind, parse.MapKind, parse.RefKind, parse.SetKind, parse.StructKind:
|
||||
return gen.userName(t)
|
||||
case parse.RefKind:
|
||||
panic("not yet implemented")
|
||||
case parse.ValueKind:
|
||||
return "types.Value"
|
||||
}
|
||||
@@ -140,7 +138,7 @@ func (gen *codeGen) defToValue(val string, t parse.TypeRef) string {
|
||||
case parse.ListKind, parse.MapKind, parse.SetKind, parse.StructKind:
|
||||
return fmt.Sprintf("%s.New().NomsValue()", val)
|
||||
case parse.RefKind:
|
||||
panic("not yet implemented")
|
||||
return fmt.Sprintf("types.Ref{R: %s}", val)
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
@@ -157,7 +155,7 @@ func (gen *codeGen) valueToDef(val string, t parse.TypeRef) string {
|
||||
case parse.ListKind, parse.MapKind, parse.SetKind, parse.StructKind:
|
||||
return fmt.Sprintf("%s.Def()", gen.valueToUser(val, t))
|
||||
case parse.RefKind:
|
||||
panic("not yet implemented")
|
||||
return fmt.Sprintf("%s.Ref()", val)
|
||||
case parse.ValueKind:
|
||||
return val // Value type has no Def
|
||||
}
|
||||
@@ -234,10 +232,8 @@ func (gen *codeGen) userToValue(val string, t parse.TypeRef) string {
|
||||
return val
|
||||
case parse.BoolKind, parse.EnumKind, parse.Float32Kind, parse.Float64Kind, parse.Int16Kind, parse.Int32Kind, parse.Int64Kind, parse.Int8Kind, parse.StringKind, parse.UInt16Kind, parse.UInt32Kind, parse.UInt64Kind, parse.UInt8Kind:
|
||||
return gen.nativeToValue(val, t)
|
||||
case parse.ListKind, parse.MapKind, parse.SetKind, parse.StructKind:
|
||||
case parse.ListKind, parse.MapKind, parse.RefKind, parse.SetKind, parse.StructKind:
|
||||
return fmt.Sprintf("%s.NomsValue()", val)
|
||||
case parse.RefKind:
|
||||
panic("not yet implemented")
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
@@ -250,10 +246,8 @@ func (gen *codeGen) valueToUser(val string, t parse.TypeRef) string {
|
||||
return fmt.Sprintf("%s.(types.Blob)", val)
|
||||
case parse.BoolKind, parse.EnumKind, parse.Float32Kind, parse.Float64Kind, parse.Int16Kind, parse.Int32Kind, parse.Int64Kind, parse.Int8Kind, parse.StringKind, parse.UInt16Kind, parse.UInt32Kind, parse.UInt64Kind, parse.UInt8Kind:
|
||||
return gen.valueToNative(val, t)
|
||||
case parse.ListKind, parse.MapKind, parse.SetKind, parse.StructKind:
|
||||
case parse.ListKind, parse.MapKind, parse.RefKind, parse.SetKind, parse.StructKind:
|
||||
return fmt.Sprintf("%sFromVal(%s)", gen.userName(t), val)
|
||||
case parse.RefKind:
|
||||
panic("not yet implemented")
|
||||
case parse.ValueKind:
|
||||
return val
|
||||
}
|
||||
@@ -275,7 +269,7 @@ func (gen *codeGen) userZero(t parse.TypeRef) string {
|
||||
case parse.ListKind, parse.MapKind, parse.SetKind:
|
||||
return fmt.Sprintf("New%s()", gen.userType(t))
|
||||
case parse.RefKind:
|
||||
panic("not yet implemented")
|
||||
return fmt.Sprintf("%s{ref.Ref{}}", gen.userType(t))
|
||||
case parse.StringKind:
|
||||
return `""`
|
||||
case parse.ValueKind:
|
||||
@@ -302,7 +296,7 @@ func (gen *codeGen) valueZero(t parse.TypeRef) string {
|
||||
case parse.MapKind:
|
||||
return "types.NewMap()"
|
||||
case parse.RefKind:
|
||||
panic("not yet implemented")
|
||||
return "types.Ref{R: ref.Ref{}}"
|
||||
case parse.SetKind:
|
||||
return "types.NewSet()"
|
||||
case parse.StringKind:
|
||||
@@ -330,7 +324,7 @@ func (gen *codeGen) userName(t parse.TypeRef) string {
|
||||
elemTypes := t.Desc.(parse.CompoundDesc).ElemTypes
|
||||
return fmt.Sprintf("MapOf%sTo%s", gen.userName(elemTypes[0]), gen.userName(elemTypes[1]))
|
||||
case parse.RefKind:
|
||||
panic("not yet implemented")
|
||||
return fmt.Sprintf("RefOf%s", gen.userName(t.Desc.(parse.CompoundDesc).ElemTypes[0]))
|
||||
case parse.SetKind:
|
||||
return fmt.Sprintf("SetOf%s", gen.userName(t.Desc.(parse.CompoundDesc).ElemTypes[0]))
|
||||
case parse.StructKind:
|
||||
@@ -397,7 +391,7 @@ func (gen *codeGen) write(t parse.TypeRef) {
|
||||
case parse.MapKind:
|
||||
gen.writeMap(t)
|
||||
case parse.RefKind:
|
||||
panic("not yet implemented")
|
||||
gen.writeRef(t)
|
||||
case parse.SetKind:
|
||||
gen.writeSet(t)
|
||||
case parse.StructKind:
|
||||
@@ -472,6 +466,19 @@ func (gen *codeGen) writeMap(t parse.TypeRef) {
|
||||
gen.write(elemTypes[1])
|
||||
}
|
||||
|
||||
func (gen *codeGen) writeRef(t parse.TypeRef) {
|
||||
elemTypes := t.Desc.(parse.CompoundDesc).ElemTypes
|
||||
data := struct {
|
||||
Name string
|
||||
ElemType parse.TypeRef
|
||||
}{
|
||||
gen.userName(t),
|
||||
elemTypes[0],
|
||||
}
|
||||
gen.writeTemplate("ref.tmpl", t, data)
|
||||
gen.write(elemTypes[0])
|
||||
}
|
||||
|
||||
func (gen *codeGen) writeSet(t parse.TypeRef) {
|
||||
elemTypes := t.Desc.(parse.CompoundDesc).ElemTypes
|
||||
data := struct {
|
||||
|
||||
34
nomdl/codegen/ref.tmpl
Normal file
34
nomdl/codegen/ref.tmpl
Normal file
@@ -0,0 +1,34 @@
|
||||
// {{.Name}}
|
||||
|
||||
type {{.Name}} struct {
|
||||
r ref.Ref
|
||||
}
|
||||
|
||||
func New{{.Name}}(r ref.Ref) {{.Name}} {
|
||||
return {{.Name}}{r}
|
||||
}
|
||||
|
||||
func (r {{.Name}}) Ref() ref.Ref {
|
||||
return r.r
|
||||
}
|
||||
|
||||
func (r {{.Name}}) Equals(other {{.Name}}) bool {
|
||||
return r.Ref() == other.Ref()
|
||||
}
|
||||
|
||||
func (r {{.Name}}) NomsValue() types.Value {
|
||||
return types.Ref{R: r.r}
|
||||
}
|
||||
|
||||
func {{.Name}}FromVal(p types.Value) {{.Name}} {
|
||||
return {{.Name}}{p.(types.Ref).Ref()}
|
||||
}
|
||||
|
||||
func (r {{.Name}}) GetValue(cs chunks.ChunkSource) {{userType .ElemType}} {
|
||||
return {{valueToUser "types.ReadValue(r.r, cs)" .ElemType}}
|
||||
}
|
||||
|
||||
func (r {{.Name}}) SetValue(val {{userType .ElemType}}, cs chunks.ChunkSink) {{.Name}} {
|
||||
ref := types.WriteValue({{userToValue "val" .ElemType}}, cs)
|
||||
return {{.Name}}{ref}
|
||||
}
|
||||
6
nomdl/codegen/test/gen/ref.noms
Normal file
6
nomdl/codegen/test/gen/ref.noms
Normal file
@@ -0,0 +1,6 @@
|
||||
using Ref(List(String))
|
||||
using List(Ref(Float32))
|
||||
|
||||
struct StructWithRef {
|
||||
R: Ref(Set(Float32))
|
||||
}
|
||||
527
nomdl/codegen/test/ref.go
Normal file
527
nomdl/codegen/test/ref.go
Normal file
@@ -0,0 +1,527 @@
|
||||
// This file was generated by nomdl/codegen.
|
||||
|
||||
package test
|
||||
|
||||
import (
|
||||
"github.com/attic-labs/noms/chunks"
|
||||
"github.com/attic-labs/noms/ref"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
|
||||
// RefOfListOfString
|
||||
|
||||
type RefOfListOfString struct {
|
||||
r ref.Ref
|
||||
}
|
||||
|
||||
func NewRefOfListOfString(r ref.Ref) RefOfListOfString {
|
||||
return RefOfListOfString{r}
|
||||
}
|
||||
|
||||
func (r RefOfListOfString) Ref() ref.Ref {
|
||||
return r.r
|
||||
}
|
||||
|
||||
func (r RefOfListOfString) Equals(other RefOfListOfString) bool {
|
||||
return r.Ref() == other.Ref()
|
||||
}
|
||||
|
||||
func (r RefOfListOfString) NomsValue() types.Value {
|
||||
return types.Ref{R: r.r}
|
||||
}
|
||||
|
||||
func RefOfListOfStringFromVal(p types.Value) RefOfListOfString {
|
||||
return RefOfListOfString{p.(types.Ref).Ref()}
|
||||
}
|
||||
|
||||
func (r RefOfListOfString) GetValue(cs chunks.ChunkSource) ListOfString {
|
||||
return ListOfStringFromVal(types.ReadValue(r.r, cs))
|
||||
}
|
||||
|
||||
func (r RefOfListOfString) SetValue(val ListOfString, cs chunks.ChunkSink) RefOfListOfString {
|
||||
ref := types.WriteValue(val.NomsValue(), cs)
|
||||
return RefOfListOfString{ref}
|
||||
}
|
||||
|
||||
// ListOfString
|
||||
|
||||
type ListOfString struct {
|
||||
l types.List
|
||||
}
|
||||
|
||||
type ListOfStringDef []string
|
||||
|
||||
func NewListOfString() ListOfString {
|
||||
return ListOfString{types.NewList()}
|
||||
}
|
||||
|
||||
func (def ListOfStringDef) New() ListOfString {
|
||||
l := make([]types.Value, len(def))
|
||||
for i, d := range def {
|
||||
l[i] = types.NewString(d)
|
||||
}
|
||||
return ListOfString{types.NewList(l...)}
|
||||
}
|
||||
|
||||
func ListOfStringFromVal(val types.Value) ListOfString {
|
||||
// TODO: Validate here
|
||||
return ListOfString{val.(types.List)}
|
||||
}
|
||||
|
||||
func (self ListOfString) Def() ListOfStringDef {
|
||||
l := make([]string, self.Len())
|
||||
for i := uint64(0); i < self.Len(); i++ {
|
||||
l[i] = self.l.Get(i).(types.String).String()
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (self ListOfString) NomsValue() types.Value {
|
||||
return self.l
|
||||
}
|
||||
|
||||
func (l ListOfString) Equals(p ListOfString) bool {
|
||||
return l.l.Equals(p.l)
|
||||
}
|
||||
|
||||
func (l ListOfString) Ref() ref.Ref {
|
||||
return l.l.Ref()
|
||||
}
|
||||
|
||||
func (l ListOfString) Len() uint64 {
|
||||
return l.l.Len()
|
||||
}
|
||||
|
||||
func (l ListOfString) Empty() bool {
|
||||
return l.Len() == uint64(0)
|
||||
}
|
||||
|
||||
func (self ListOfString) Get(i uint64) string {
|
||||
return self.l.Get(i).(types.String).String()
|
||||
}
|
||||
|
||||
func (l ListOfString) Slice(idx uint64, end uint64) ListOfString {
|
||||
return ListOfString{l.l.Slice(idx, end)}
|
||||
}
|
||||
|
||||
func (self ListOfString) Set(i uint64, val string) ListOfString {
|
||||
return ListOfString{self.l.Set(i, types.NewString(val))}
|
||||
}
|
||||
|
||||
func (l ListOfString) Append(v ...string) ListOfString {
|
||||
return ListOfString{l.l.Append(l.fromElemSlice(v)...)}
|
||||
}
|
||||
|
||||
func (l ListOfString) Insert(idx uint64, v ...string) ListOfString {
|
||||
return ListOfString{l.l.Insert(idx, l.fromElemSlice(v)...)}
|
||||
}
|
||||
|
||||
func (l ListOfString) Remove(idx uint64, end uint64) ListOfString {
|
||||
return ListOfString{l.l.Remove(idx, end)}
|
||||
}
|
||||
|
||||
func (l ListOfString) RemoveAt(idx uint64) ListOfString {
|
||||
return ListOfString{(l.l.RemoveAt(idx))}
|
||||
}
|
||||
|
||||
func (l ListOfString) fromElemSlice(p []string) []types.Value {
|
||||
r := make([]types.Value, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = types.NewString(v)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
type ListOfStringIterCallback func(v string) (stop bool)
|
||||
|
||||
func (l ListOfString) Iter(cb ListOfStringIterCallback) {
|
||||
l.l.Iter(func(v types.Value) bool {
|
||||
return cb(v.(types.String).String())
|
||||
})
|
||||
}
|
||||
|
||||
type ListOfStringIterAllCallback func(v string)
|
||||
|
||||
func (l ListOfString) IterAll(cb ListOfStringIterAllCallback) {
|
||||
l.l.IterAll(func(v types.Value) {
|
||||
cb(v.(types.String).String())
|
||||
})
|
||||
}
|
||||
|
||||
type ListOfStringFilterCallback func(v string) (keep bool)
|
||||
|
||||
func (l ListOfString) Filter(cb ListOfStringFilterCallback) ListOfString {
|
||||
nl := NewListOfString()
|
||||
l.IterAll(func(v string) {
|
||||
if cb(v) {
|
||||
nl = nl.Append(v)
|
||||
}
|
||||
})
|
||||
return nl
|
||||
}
|
||||
|
||||
// ListOfRefOfFloat32
|
||||
|
||||
type ListOfRefOfFloat32 struct {
|
||||
l types.List
|
||||
}
|
||||
|
||||
type ListOfRefOfFloat32Def []ref.Ref
|
||||
|
||||
func NewListOfRefOfFloat32() ListOfRefOfFloat32 {
|
||||
return ListOfRefOfFloat32{types.NewList()}
|
||||
}
|
||||
|
||||
func (def ListOfRefOfFloat32Def) New() ListOfRefOfFloat32 {
|
||||
l := make([]types.Value, len(def))
|
||||
for i, d := range def {
|
||||
l[i] = types.Ref{R: d}
|
||||
}
|
||||
return ListOfRefOfFloat32{types.NewList(l...)}
|
||||
}
|
||||
|
||||
func ListOfRefOfFloat32FromVal(val types.Value) ListOfRefOfFloat32 {
|
||||
// TODO: Validate here
|
||||
return ListOfRefOfFloat32{val.(types.List)}
|
||||
}
|
||||
|
||||
func (self ListOfRefOfFloat32) Def() ListOfRefOfFloat32Def {
|
||||
l := make([]ref.Ref, self.Len())
|
||||
for i := uint64(0); i < self.Len(); i++ {
|
||||
l[i] = self.l.Get(i).Ref()
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (self ListOfRefOfFloat32) NomsValue() types.Value {
|
||||
return self.l
|
||||
}
|
||||
|
||||
func (l ListOfRefOfFloat32) Equals(p ListOfRefOfFloat32) bool {
|
||||
return l.l.Equals(p.l)
|
||||
}
|
||||
|
||||
func (l ListOfRefOfFloat32) Ref() ref.Ref {
|
||||
return l.l.Ref()
|
||||
}
|
||||
|
||||
func (l ListOfRefOfFloat32) Len() uint64 {
|
||||
return l.l.Len()
|
||||
}
|
||||
|
||||
func (l ListOfRefOfFloat32) Empty() bool {
|
||||
return l.Len() == uint64(0)
|
||||
}
|
||||
|
||||
func (self ListOfRefOfFloat32) Get(i uint64) RefOfFloat32 {
|
||||
return RefOfFloat32FromVal(self.l.Get(i))
|
||||
}
|
||||
|
||||
func (l ListOfRefOfFloat32) Slice(idx uint64, end uint64) ListOfRefOfFloat32 {
|
||||
return ListOfRefOfFloat32{l.l.Slice(idx, end)}
|
||||
}
|
||||
|
||||
func (self ListOfRefOfFloat32) Set(i uint64, val RefOfFloat32) ListOfRefOfFloat32 {
|
||||
return ListOfRefOfFloat32{self.l.Set(i, val.NomsValue())}
|
||||
}
|
||||
|
||||
func (l ListOfRefOfFloat32) Append(v ...RefOfFloat32) ListOfRefOfFloat32 {
|
||||
return ListOfRefOfFloat32{l.l.Append(l.fromElemSlice(v)...)}
|
||||
}
|
||||
|
||||
func (l ListOfRefOfFloat32) Insert(idx uint64, v ...RefOfFloat32) ListOfRefOfFloat32 {
|
||||
return ListOfRefOfFloat32{l.l.Insert(idx, l.fromElemSlice(v)...)}
|
||||
}
|
||||
|
||||
func (l ListOfRefOfFloat32) Remove(idx uint64, end uint64) ListOfRefOfFloat32 {
|
||||
return ListOfRefOfFloat32{l.l.Remove(idx, end)}
|
||||
}
|
||||
|
||||
func (l ListOfRefOfFloat32) RemoveAt(idx uint64) ListOfRefOfFloat32 {
|
||||
return ListOfRefOfFloat32{(l.l.RemoveAt(idx))}
|
||||
}
|
||||
|
||||
func (l ListOfRefOfFloat32) fromElemSlice(p []RefOfFloat32) []types.Value {
|
||||
r := make([]types.Value, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = v.NomsValue()
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
type ListOfRefOfFloat32IterCallback func(v RefOfFloat32) (stop bool)
|
||||
|
||||
func (l ListOfRefOfFloat32) Iter(cb ListOfRefOfFloat32IterCallback) {
|
||||
l.l.Iter(func(v types.Value) bool {
|
||||
return cb(RefOfFloat32FromVal(v))
|
||||
})
|
||||
}
|
||||
|
||||
type ListOfRefOfFloat32IterAllCallback func(v RefOfFloat32)
|
||||
|
||||
func (l ListOfRefOfFloat32) IterAll(cb ListOfRefOfFloat32IterAllCallback) {
|
||||
l.l.IterAll(func(v types.Value) {
|
||||
cb(RefOfFloat32FromVal(v))
|
||||
})
|
||||
}
|
||||
|
||||
type ListOfRefOfFloat32FilterCallback func(v RefOfFloat32) (keep bool)
|
||||
|
||||
func (l ListOfRefOfFloat32) Filter(cb ListOfRefOfFloat32FilterCallback) ListOfRefOfFloat32 {
|
||||
nl := NewListOfRefOfFloat32()
|
||||
l.IterAll(func(v RefOfFloat32) {
|
||||
if cb(v) {
|
||||
nl = nl.Append(v)
|
||||
}
|
||||
})
|
||||
return nl
|
||||
}
|
||||
|
||||
// RefOfFloat32
|
||||
|
||||
type RefOfFloat32 struct {
|
||||
r ref.Ref
|
||||
}
|
||||
|
||||
func NewRefOfFloat32(r ref.Ref) RefOfFloat32 {
|
||||
return RefOfFloat32{r}
|
||||
}
|
||||
|
||||
func (r RefOfFloat32) Ref() ref.Ref {
|
||||
return r.r
|
||||
}
|
||||
|
||||
func (r RefOfFloat32) Equals(other RefOfFloat32) bool {
|
||||
return r.Ref() == other.Ref()
|
||||
}
|
||||
|
||||
func (r RefOfFloat32) NomsValue() types.Value {
|
||||
return types.Ref{R: r.r}
|
||||
}
|
||||
|
||||
func RefOfFloat32FromVal(p types.Value) RefOfFloat32 {
|
||||
return RefOfFloat32{p.(types.Ref).Ref()}
|
||||
}
|
||||
|
||||
func (r RefOfFloat32) GetValue(cs chunks.ChunkSource) float32 {
|
||||
return float32(types.ReadValue(r.r, cs).(types.Float32))
|
||||
}
|
||||
|
||||
func (r RefOfFloat32) SetValue(val float32, cs chunks.ChunkSink) RefOfFloat32 {
|
||||
ref := types.WriteValue(types.Float32(val), cs)
|
||||
return RefOfFloat32{ref}
|
||||
}
|
||||
|
||||
// StructWithRef
|
||||
|
||||
type StructWithRefDef struct {
|
||||
R ref.Ref
|
||||
}
|
||||
|
||||
type StructWithRef struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
func NewStructWithRef() StructWithRef {
|
||||
return StructWithRef{types.NewMap(
|
||||
types.NewString("$name"), types.NewString("StructWithRef"),
|
||||
types.NewString("R"), types.Ref{R: ref.Ref{}},
|
||||
)}
|
||||
}
|
||||
|
||||
func (def StructWithRefDef) New() StructWithRef {
|
||||
return StructWithRef{
|
||||
types.NewMap(
|
||||
types.NewString("$name"), types.NewString("StructWithRef"),
|
||||
types.NewString("R"), types.Ref{R: def.R},
|
||||
)}
|
||||
}
|
||||
|
||||
func (self StructWithRef) Def() StructWithRefDef {
|
||||
return StructWithRefDef{
|
||||
self.m.Get(types.NewString("R")).Ref(),
|
||||
}
|
||||
}
|
||||
|
||||
func StructWithRefFromVal(val types.Value) StructWithRef {
|
||||
// TODO: Validate here
|
||||
return StructWithRef{val.(types.Map)}
|
||||
}
|
||||
|
||||
func (self StructWithRef) NomsValue() types.Value {
|
||||
return self.m
|
||||
}
|
||||
|
||||
func (self StructWithRef) Equals(other StructWithRef) bool {
|
||||
return self.m.Equals(other.m)
|
||||
}
|
||||
|
||||
func (self StructWithRef) Ref() ref.Ref {
|
||||
return self.m.Ref()
|
||||
}
|
||||
|
||||
func (self StructWithRef) R() RefOfSetOfFloat32 {
|
||||
return RefOfSetOfFloat32FromVal(self.m.Get(types.NewString("R")))
|
||||
}
|
||||
|
||||
func (self StructWithRef) SetR(val RefOfSetOfFloat32) StructWithRef {
|
||||
return StructWithRef{self.m.Set(types.NewString("R"), val.NomsValue())}
|
||||
}
|
||||
|
||||
// RefOfSetOfFloat32
|
||||
|
||||
type RefOfSetOfFloat32 struct {
|
||||
r ref.Ref
|
||||
}
|
||||
|
||||
func NewRefOfSetOfFloat32(r ref.Ref) RefOfSetOfFloat32 {
|
||||
return RefOfSetOfFloat32{r}
|
||||
}
|
||||
|
||||
func (r RefOfSetOfFloat32) Ref() ref.Ref {
|
||||
return r.r
|
||||
}
|
||||
|
||||
func (r RefOfSetOfFloat32) Equals(other RefOfSetOfFloat32) bool {
|
||||
return r.Ref() == other.Ref()
|
||||
}
|
||||
|
||||
func (r RefOfSetOfFloat32) NomsValue() types.Value {
|
||||
return types.Ref{R: r.r}
|
||||
}
|
||||
|
||||
func RefOfSetOfFloat32FromVal(p types.Value) RefOfSetOfFloat32 {
|
||||
return RefOfSetOfFloat32{p.(types.Ref).Ref()}
|
||||
}
|
||||
|
||||
func (r RefOfSetOfFloat32) GetValue(cs chunks.ChunkSource) SetOfFloat32 {
|
||||
return SetOfFloat32FromVal(types.ReadValue(r.r, cs))
|
||||
}
|
||||
|
||||
func (r RefOfSetOfFloat32) SetValue(val SetOfFloat32, cs chunks.ChunkSink) RefOfSetOfFloat32 {
|
||||
ref := types.WriteValue(val.NomsValue(), cs)
|
||||
return RefOfSetOfFloat32{ref}
|
||||
}
|
||||
|
||||
// SetOfFloat32
|
||||
|
||||
type SetOfFloat32 struct {
|
||||
s types.Set
|
||||
}
|
||||
|
||||
type SetOfFloat32Def map[float32]bool
|
||||
|
||||
func NewSetOfFloat32() SetOfFloat32 {
|
||||
return SetOfFloat32{types.NewSet()}
|
||||
}
|
||||
|
||||
func (def SetOfFloat32Def) New() SetOfFloat32 {
|
||||
l := make([]types.Value, len(def))
|
||||
i := 0
|
||||
for d, _ := range def {
|
||||
l[i] = types.Float32(d)
|
||||
i++
|
||||
}
|
||||
return SetOfFloat32{types.NewSet(l...)}
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Def() SetOfFloat32Def {
|
||||
def := make(map[float32]bool, s.Len())
|
||||
s.s.Iter(func(v types.Value) bool {
|
||||
def[float32(v.(types.Float32))] = true
|
||||
return false
|
||||
})
|
||||
return def
|
||||
}
|
||||
|
||||
func SetOfFloat32FromVal(p types.Value) SetOfFloat32 {
|
||||
return SetOfFloat32{p.(types.Set)}
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) NomsValue() types.Value {
|
||||
return s.s
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Equals(p SetOfFloat32) bool {
|
||||
return s.s.Equals(p.s)
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Ref() ref.Ref {
|
||||
return s.s.Ref()
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Empty() bool {
|
||||
return s.s.Empty()
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Len() uint64 {
|
||||
return s.s.Len()
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Has(p float32) bool {
|
||||
return s.s.Has(types.Float32(p))
|
||||
}
|
||||
|
||||
type SetOfFloat32IterCallback func(p float32) (stop bool)
|
||||
|
||||
func (s SetOfFloat32) Iter(cb SetOfFloat32IterCallback) {
|
||||
s.s.Iter(func(v types.Value) bool {
|
||||
return cb(float32(v.(types.Float32)))
|
||||
})
|
||||
}
|
||||
|
||||
type SetOfFloat32IterAllCallback func(p float32)
|
||||
|
||||
func (s SetOfFloat32) IterAll(cb SetOfFloat32IterAllCallback) {
|
||||
s.s.IterAll(func(v types.Value) {
|
||||
cb(float32(v.(types.Float32)))
|
||||
})
|
||||
}
|
||||
|
||||
type SetOfFloat32FilterCallback func(p float32) (keep bool)
|
||||
|
||||
func (s SetOfFloat32) Filter(cb SetOfFloat32FilterCallback) SetOfFloat32 {
|
||||
ns := NewSetOfFloat32()
|
||||
s.IterAll(func(v float32) {
|
||||
if cb(v) {
|
||||
ns = ns.Insert(v)
|
||||
}
|
||||
})
|
||||
return ns
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Insert(p ...float32) SetOfFloat32 {
|
||||
return SetOfFloat32{s.s.Insert(s.fromElemSlice(p)...)}
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Remove(p ...float32) SetOfFloat32 {
|
||||
return SetOfFloat32{s.s.Remove(s.fromElemSlice(p)...)}
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Union(others ...SetOfFloat32) SetOfFloat32 {
|
||||
return SetOfFloat32{s.s.Union(s.fromStructSlice(others)...)}
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Subtract(others ...SetOfFloat32) SetOfFloat32 {
|
||||
return SetOfFloat32{s.s.Subtract(s.fromStructSlice(others)...)}
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) Any() float32 {
|
||||
return float32(s.s.Any().(types.Float32))
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) fromStructSlice(p []SetOfFloat32) []types.Set {
|
||||
r := make([]types.Set, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = v.s
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (s SetOfFloat32) fromElemSlice(p []float32) []types.Value {
|
||||
r := make([]types.Value, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = types.Float32(v)
|
||||
}
|
||||
return r
|
||||
}
|
||||
88
nomdl/codegen/test/ref_test.go
Normal file
88
nomdl/codegen/test/ref_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/assert"
|
||||
"github.com/attic-labs/noms/chunks"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
|
||||
func TestRef(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
l := ListOfStringDef{"a", "b", "c"}.New()
|
||||
l2 := ListOfStringDef{"d", "e", "f"}.New()
|
||||
lRef := l.Ref()
|
||||
r := NewRefOfListOfString(lRef)
|
||||
|
||||
v := types.ReadValue(l.Ref(), cs)
|
||||
assert.Nil(v)
|
||||
|
||||
assert.Panics(func() { r.GetValue(cs) })
|
||||
|
||||
r2 := r.SetValue(l, cs)
|
||||
assert.True(r.Equals(r2))
|
||||
v2 := r2.GetValue(cs)
|
||||
v3 := r.GetValue(cs)
|
||||
assert.True(v2.Equals(v3))
|
||||
|
||||
r3 := r2.SetValue(l2, cs)
|
||||
assert.False(r.Equals(r3))
|
||||
}
|
||||
|
||||
func TestRefFromValAndNomsValue(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
l := ListOfStringDef{"a", "b", "c"}.New()
|
||||
rv := types.Ref{R: l.Ref()}
|
||||
r := RefOfListOfStringFromVal(rv)
|
||||
r2 := NewRefOfListOfString(l.Ref())
|
||||
assert.True(r.Equals(r2))
|
||||
|
||||
rv2 := r.NomsValue()
|
||||
assert.True(rv.Equals(rv2))
|
||||
}
|
||||
|
||||
func TestListOfRef(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
a := types.Float32(0)
|
||||
ra := a.Ref()
|
||||
|
||||
l := NewListOfRefOfFloat32()
|
||||
r := NewRefOfFloat32(ra)
|
||||
l = l.Append(r)
|
||||
r2 := l.Get(0)
|
||||
assert.True(r.Equals(r2))
|
||||
|
||||
l = l.Set(0, r.SetValue(1, cs))
|
||||
r3 := l.Get(0)
|
||||
assert.False(r.Equals(r3))
|
||||
assert.Panics(func() { r.GetValue(cs) })
|
||||
}
|
||||
|
||||
func TestStructWithRef(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
|
||||
set := SetOfFloat32Def{0: true, 1: true, 2: true}.New()
|
||||
str := StructWithRefDef{
|
||||
R: set.Ref(),
|
||||
}.New()
|
||||
|
||||
r := str.R()
|
||||
r2 := NewRefOfSetOfFloat32(set.Ref())
|
||||
assert.True(r.Equals(r2))
|
||||
|
||||
assert.Panics(func() { r2.GetValue(cs) })
|
||||
|
||||
types.WriteValue(str.NomsValue(), cs)
|
||||
assert.Panics(func() { r2.GetValue(cs) })
|
||||
|
||||
types.WriteValue(set.NomsValue(), cs)
|
||||
set2 := r2.GetValue(cs)
|
||||
assert.True(set.Equals(set2))
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package test
|
||||
//go:generate go run ../codegen.go --package=test --in=gen/enum_struct.noms --out=enum_struct.go
|
||||
//go:generate go run ../codegen.go --package=test --in=gen/list_int64.noms --out=list_int64.go
|
||||
//go:generate go run ../codegen.go --package=test --in=gen/map.noms --out=map.go
|
||||
//go:generate go run ../codegen.go --package=test --in=gen/ref.noms --out=ref.go
|
||||
//go:generate go run ../codegen.go --package=test --in=gen/set.noms --out=set.go
|
||||
//go:generate go run ../codegen.go --package=test --in=gen/struct_primitives.noms --out=struct_primitives.go
|
||||
//go:generate go run ../codegen.go --package=test --in=gen/struct_with_list.noms --out=struct_with_list.go
|
||||
|
||||
Reference in New Issue
Block a user