mirror of
https://github.com/dolthub/dolt.git
synced 2026-05-11 19:11:10 -05:00
@@ -1,8 +0,0 @@
|
||||
func (s {{.StructName}}) {{.GoFieldName}}() {{.FieldType}} {
|
||||
return {{fromVal .FieldType}}(s.m.Get(types.NewString("{{.FieldName}}")))
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Set{{.GoFieldName}}(p {{.FieldType}}) {{.StructName}} {
|
||||
return {{fromVal .StructName}}(s.m.Set(types.NewString("{{.FieldName}}"), p{{toVal .FieldType}}))
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
// This file was generated by nomgen.
|
||||
// To regenerate, run `go generate` in this package.
|
||||
|
||||
package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"github.com/attic-labs/noms/ref"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
// {{.StructName}}
|
||||
|
||||
type {{.StructName}} struct {
|
||||
l types.List
|
||||
}
|
||||
|
||||
type {{.StructName}}IterCallback (func (p {{.ElemName}}) (stop bool))
|
||||
|
||||
func New{{.StructName}}() {{.StructName}} {
|
||||
return {{.StructName}}{types.NewList()}
|
||||
}
|
||||
|
||||
func {{.StructName}}FromVal(p types.Value) {{.StructName}} {
|
||||
return {{.StructName}}{p.(types.List)}
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) NomsValue() types.List {
|
||||
return l.l
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) Equals(p {{.StructName}}) bool {
|
||||
return l.l.Equals(p.l)
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) Ref() ref.Ref {
|
||||
return l.l.Ref()
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) Len() uint64 {
|
||||
return l.l.Len()
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) Empty() bool {
|
||||
return l.Len() == uint64(0)
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) Get(idx uint64) {{.ElemName}} {
|
||||
return {{fromVal .ElemName}}(l.l.Get(idx))
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) Slice(idx uint64, end uint64) {{.StructName}} {
|
||||
return {{.StructName}}{l.l.Slice(idx, end)}
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) Set(idx uint64, v {{.ElemName}}) {{.StructName}} {
|
||||
return {{.StructName}}{l.l.Set(idx, v{{toVal .ElemName}})}
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) Append(v ...{{.ElemName}}) {{.StructName}} {
|
||||
return {{.StructName}}{l.l.Append(l.fromElemSlice(v)...)}
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) Insert(idx uint64, v ...{{.ElemName}}) {{.StructName}} {
|
||||
return {{.StructName}}{l.l.Insert(idx, l.fromElemSlice(v)...)}
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) Remove(idx uint64, end uint64) {{.StructName}} {
|
||||
return {{.StructName}}{l.l.Remove(idx, end)}
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) RemoveAt(idx uint64) {{.StructName}} {
|
||||
return {{.StructName}}{(l.l.RemoveAt(idx))}
|
||||
}
|
||||
|
||||
func (l {{.StructName}}) fromElemSlice(p []{{.ElemName}}) []types.Value {
|
||||
r := make([]types.Value, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = v{{toVal .ElemName}}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
// {{.StructName}}
|
||||
|
||||
type {{.StructName}} struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
type {{.StructName}}IterCallback (func(k {{.KeyName}}, v {{.ValueName}}) (stop bool))
|
||||
|
||||
func New{{.StructName}}() {{.StructName}} {
|
||||
return {{.StructName}}{types.NewMap()}
|
||||
}
|
||||
|
||||
func {{.StructName}}FromVal(p types.Value) {{.StructName}} {
|
||||
return {{.StructName}}{p.(types.Map)}
|
||||
}
|
||||
|
||||
func (m {{.StructName}}) NomsValue() types.Map {
|
||||
return m.m
|
||||
}
|
||||
|
||||
func (m {{.StructName}}) Equals(p {{.StructName}}) bool {
|
||||
return m.m.Equals(p.m)
|
||||
}
|
||||
|
||||
func (m {{.StructName}}) Ref() ref.Ref {
|
||||
return m.m.Ref()
|
||||
}
|
||||
|
||||
func (m {{.StructName}}) Empty() bool {
|
||||
return m.m.Empty()
|
||||
}
|
||||
|
||||
func (m {{.StructName}}) Len() uint64 {
|
||||
return m.m.Len()
|
||||
}
|
||||
|
||||
func (m {{.StructName}}) Has(p {{.KeyName}}) bool {
|
||||
return m.m.Has(p{{toVal .KeyName}})
|
||||
}
|
||||
|
||||
func (m {{.StructName}}) Get(p {{.KeyName}}) {{.ValueName}} {
|
||||
return {{fromVal .ValueName}}(m.m.Get(p{{toVal .KeyName}}))
|
||||
}
|
||||
|
||||
func (m {{.StructName}}) Set(k {{.KeyName}}, v {{.ValueName}}) {{.StructName}} {
|
||||
return {{fromVal .StructName}}(m.m.Set(k{{toVal .KeyName}}, v{{toVal .ValueName}}))
|
||||
}
|
||||
|
||||
// TODO: Implement SetM?
|
||||
|
||||
func (m {{.StructName}}) Remove(p {{.KeyName}}) {{.StructName}} {
|
||||
return {{fromVal .StructName}}(m.m.Remove(p{{toVal .KeyName}}))
|
||||
}
|
||||
|
||||
func (m {{.StructName}}) Iter(cb {{.StructName}}IterCallback) {
|
||||
m.m.Iter(func(k, v types.Value) bool {
|
||||
return cb({{fromVal .KeyName}}(k), {{fromVal .ValueName}}(v))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,249 +0,0 @@
|
||||
package nomgen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/attic-labs/noms/d"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
|
||||
var (
|
||||
fieldTempl = readTemplate("field.tmpl")
|
||||
headerTmpl = readTemplate("header.tmpl")
|
||||
listTempl = readTemplate("list.tmpl")
|
||||
mapTempl = readTemplate("map.tmpl")
|
||||
setTempl = readTemplate("set.tmpl")
|
||||
structTempl = readTemplate("struct.tmpl")
|
||||
)
|
||||
|
||||
type NG struct {
|
||||
w io.WriteCloser
|
||||
written types.Set
|
||||
|
||||
// typename -> type
|
||||
// We use a map rather than a set because we want order to be stable across builds
|
||||
// If we used a set, order would be based on ref, which changes as the types change
|
||||
toWrite types.Map
|
||||
}
|
||||
|
||||
func New(outFile string) NG {
|
||||
f, err := os.OpenFile(outFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
d.Chk.NoError(err)
|
||||
return NG{w: f, written: types.NewSet(), toWrite: types.NewMap()}
|
||||
}
|
||||
|
||||
func (ng *NG) WriteGo(pkg string) {
|
||||
headerTmpl.Execute(ng.w, struct{ PackageName string }{pkg})
|
||||
|
||||
for !ng.toWrite.Empty() {
|
||||
n, t := ng.toWrite.First()
|
||||
ng.toWrite = ng.toWrite.Remove(n)
|
||||
ng.written = ng.written.Insert(t)
|
||||
ng.writeType(t.(types.Map))
|
||||
}
|
||||
|
||||
ng.w.Close()
|
||||
}
|
||||
|
||||
func (ng *NG) AddType(val types.Value) types.Value {
|
||||
switch val := val.(type) {
|
||||
case types.String:
|
||||
// Nothing to do, the type is primitive
|
||||
return val
|
||||
case types.Map:
|
||||
name := types.NewString(getGoTypeName(val))
|
||||
if ng.written.Has(val) || ng.toWrite.Has(name) {
|
||||
return val
|
||||
}
|
||||
ng.toWrite = ng.toWrite.Set(name, val)
|
||||
default:
|
||||
d.Chk.Fail(fmt.Sprintf("Unexpected typedef: %+v", val))
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func fromNomsValue(name string) string {
|
||||
if name == "types.Value" {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%sFromVal", name)
|
||||
}
|
||||
|
||||
func toNomsValue(name string) string {
|
||||
if strings.HasPrefix(name, "types.") {
|
||||
return ""
|
||||
}
|
||||
return ".NomsValue()"
|
||||
}
|
||||
|
||||
func readTemplate(name string) *template.Template {
|
||||
_, thisfile, _, _ := runtime.Caller(1)
|
||||
f, err := os.Open(path.Join(path.Dir(thisfile), name))
|
||||
d.Chk.NoError(err)
|
||||
defer f.Close()
|
||||
b, err := ioutil.ReadAll(f)
|
||||
d.Chk.NoError(err)
|
||||
t, err := template.New(name).Funcs(template.FuncMap{
|
||||
"fromVal": fromNomsValue,
|
||||
"toVal": toNomsValue,
|
||||
}).Parse(string(b))
|
||||
d.Chk.NoError(err)
|
||||
return t
|
||||
}
|
||||
|
||||
func (ng *NG) writeType(val types.Map) {
|
||||
typ := val.Get(types.NewString("$typeDef")).(types.String).String()
|
||||
switch typ {
|
||||
case "noms.ListDef":
|
||||
ng.writeList(val)
|
||||
return
|
||||
case "noms.MapDef":
|
||||
ng.writeMap(val)
|
||||
return
|
||||
case "noms.SetDef":
|
||||
ng.writeSet(val)
|
||||
return
|
||||
case "noms.StructDef":
|
||||
ng.writeStruct(val)
|
||||
return
|
||||
}
|
||||
d.Chk.Fail(fmt.Sprintf("Unexpected typedef: %+v", val))
|
||||
}
|
||||
|
||||
func (ng *NG) writeSet(val types.Map) {
|
||||
elem := val.Get(types.NewString("elem"))
|
||||
ng.AddType(elem)
|
||||
|
||||
data := struct {
|
||||
StructName string
|
||||
ElemName string
|
||||
}{
|
||||
getGoTypeName(val),
|
||||
getGoTypeName(elem),
|
||||
}
|
||||
|
||||
setTempl.Execute(ng.w, data)
|
||||
}
|
||||
|
||||
func (ng *NG) writeList(val types.Map) {
|
||||
elem := val.Get(types.NewString("elem"))
|
||||
ng.AddType(elem)
|
||||
|
||||
data := struct {
|
||||
StructName string
|
||||
ElemName string
|
||||
}{
|
||||
getGoTypeName(val),
|
||||
getGoTypeName(elem),
|
||||
}
|
||||
|
||||
listTempl.Execute(ng.w, data)
|
||||
}
|
||||
|
||||
func (ng *NG) writeMap(val types.Map) {
|
||||
key := val.Get(types.NewString("key"))
|
||||
ng.AddType(key)
|
||||
valueName := val.Get(types.NewString("value"))
|
||||
ng.AddType(valueName)
|
||||
|
||||
data := struct {
|
||||
StructName string
|
||||
KeyName string
|
||||
ValueName string
|
||||
}{
|
||||
getGoTypeName(val),
|
||||
getGoTypeName(key),
|
||||
getGoTypeName(valueName),
|
||||
}
|
||||
|
||||
mapTempl.Execute(ng.w, data)
|
||||
}
|
||||
|
||||
func (ng *NG) writeStruct(val types.Map) {
|
||||
structName := getGoTypeName(val)
|
||||
structTempl.Execute(ng.w, struct {
|
||||
StructName string
|
||||
}{
|
||||
getGoTypeName(val),
|
||||
})
|
||||
|
||||
val.Iter(func(k, v types.Value) (stop bool) {
|
||||
sk := k.(types.String).String()
|
||||
if sk[0] != '$' {
|
||||
ng.writeField(structName, sk, v)
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
func (ng *NG) writeField(structName, fieldName string, typeDef types.Value) {
|
||||
ng.AddType(typeDef)
|
||||
|
||||
data := struct {
|
||||
StructName string
|
||||
FieldType string
|
||||
GoFieldName string
|
||||
FieldName string
|
||||
}{
|
||||
structName,
|
||||
getGoTypeName(typeDef),
|
||||
strings.Title(fieldName),
|
||||
fieldName,
|
||||
}
|
||||
|
||||
fieldTempl.Execute(ng.w, data)
|
||||
}
|
||||
|
||||
func getGoTypeName(typeDef types.Value) string {
|
||||
typeName := getGoStructName(typeDef)
|
||||
switch typeDef.(type) {
|
||||
case types.String:
|
||||
return fmt.Sprintf("types.%s", typeName)
|
||||
}
|
||||
return typeName
|
||||
}
|
||||
|
||||
func getGoStructName(typeDef types.Value) string {
|
||||
switch typeDef := typeDef.(type) {
|
||||
case types.String:
|
||||
name := typeDef.String()
|
||||
switch name {
|
||||
case "bool", "int8", "int16", "int32", "int64", "float32", "float64", "blob", "string", "set", "map", "value":
|
||||
return strings.Title(typeDef.String())
|
||||
case "uint8", "uint16", "uint32", "uint64":
|
||||
return strings.ToUpper(typeDef.String()[:2]) + typeDef.String()[2:]
|
||||
}
|
||||
|
||||
d.Chk.Fail("unexpected noms type name: %s", name)
|
||||
case types.Map:
|
||||
if typeDef.Has(types.NewString("$name")) {
|
||||
return typeDef.Get(types.NewString("$name")).(types.String).String()
|
||||
}
|
||||
typ := typeDef.Get(types.NewString("$typeDef")).(types.String).String()
|
||||
switch typ {
|
||||
case "noms.ListDef":
|
||||
return fmt.Sprintf("ListOf%s", getGoStructName(typeDef.Get(types.NewString("elem"))))
|
||||
case "noms.MapDef":
|
||||
return fmt.Sprintf("MapOf%sTo%s",
|
||||
getGoStructName(typeDef.Get(types.NewString("key"))),
|
||||
getGoStructName(typeDef.Get(types.NewString("value"))))
|
||||
case "noms.SetDef":
|
||||
return fmt.Sprintf("SetOf%s", getGoStructName(typeDef.Get(types.NewString("elem"))))
|
||||
case "noms.StructDef":
|
||||
d.Chk.Fail("noms.StructDef must have a $name filed: %+v", typeDef)
|
||||
}
|
||||
}
|
||||
d.Chk.Fail("Unexpected typeDef struct: %+v", typeDef)
|
||||
return ""
|
||||
}
|
||||
|
||||
func (ng *NG) writeStr(str string, vals ...interface{}) {
|
||||
io.WriteString(ng.w, fmt.Sprintf(str, vals...))
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package nomgen
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/suite"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
|
||||
type NomgenTestSuite struct {
|
||||
suite.Suite
|
||||
ng NG
|
||||
path string
|
||||
}
|
||||
|
||||
func (suite *NomgenTestSuite) SetupTest() {
|
||||
dir, err := ioutil.TempDir("", "nomgen")
|
||||
suite.NoError(err)
|
||||
suite.path = path.Join(dir, "types.go")
|
||||
suite.ng = New(suite.path)
|
||||
}
|
||||
|
||||
func (suite *NomgenTestSuite) TearDownTest() {
|
||||
os.Remove(suite.path)
|
||||
}
|
||||
|
||||
func (suite *NomgenTestSuite) TestListSmokeTest() {
|
||||
suite.ng.AddType(types.NewMap(
|
||||
types.NewString("$typeDef"), types.NewString("noms.ListDef"),
|
||||
types.NewString("elem"), types.NewString("int32")))
|
||||
suite.ng.WriteGo("test")
|
||||
}
|
||||
|
||||
func (suite *NomgenTestSuite) TestSetSmokeTest() {
|
||||
suite.ng.AddType(types.NewMap(
|
||||
types.NewString("$typeDef"), types.NewString("noms.SetDef"),
|
||||
types.NewString("elem"), types.NewString("int32")))
|
||||
suite.ng.WriteGo("test")
|
||||
}
|
||||
|
||||
func (suite *NomgenTestSuite) TestMapSmokeTest() {
|
||||
suite.ng.AddType(types.NewMap(
|
||||
types.NewString("$typeDef"), types.NewString("noms.MapDef"),
|
||||
types.NewString("key"), types.NewString("int32"),
|
||||
types.NewString("value"), types.NewString("bool")))
|
||||
|
||||
suite.ng.WriteGo("test")
|
||||
}
|
||||
|
||||
func (suite *NomgenTestSuite) TestStructSmokeTest() {
|
||||
suite.ng.AddType(types.NewMap(
|
||||
types.NewString("$typeDef"), types.NewString("noms.StructDef"),
|
||||
types.NewString("$name"), types.NewString("MyStruct"),
|
||||
types.NewString("key"), types.NewString("int32"),
|
||||
types.NewString("value"), types.NewString("bool")))
|
||||
suite.ng.WriteGo("test")
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
// {{.StructName}}
|
||||
|
||||
type {{.StructName}} struct {
|
||||
s types.Set
|
||||
}
|
||||
|
||||
type {{.StructName}}IterCallback (func(p {{.ElemName}}) (stop bool))
|
||||
|
||||
func New{{.StructName}}() {{.StructName}} {
|
||||
return {{.StructName}}{types.NewSet()}
|
||||
}
|
||||
|
||||
func {{.StructName}}FromVal(p types.Value) {{.StructName}} {
|
||||
return {{.StructName}}{p.(types.Set)}
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) NomsValue() types.Set {
|
||||
return s.s
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Equals(p {{.StructName}}) bool {
|
||||
return s.s.Equals(p.s)
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Ref() ref.Ref {
|
||||
return s.s.Ref()
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Empty() bool {
|
||||
return s.s.Empty()
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Len() uint64 {
|
||||
return s.s.Len()
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Has(p {{.ElemName}}) bool {
|
||||
return s.s.Has(p{{toVal .ElemName}})
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Iter(cb {{.StructName}}IterCallback) {
|
||||
s.s.Iter(func(v types.Value) bool {
|
||||
return cb({{fromVal .ElemName}}(v))
|
||||
})
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Insert(p ...{{.ElemName}}) {{.StructName}} {
|
||||
return {{.StructName}}{s.s.Insert(s.fromElemSlice(p)...)}
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Remove(p ...{{.ElemName}}) {{.StructName}} {
|
||||
return {{.StructName}}{s.s.Remove(s.fromElemSlice(p)...)}
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Union(others ...{{.StructName}}) {{.StructName}} {
|
||||
return {{.StructName}}{s.s.Union(s.fromStructSlice(others)...)}
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Subtract(others ...{{.StructName}}) {{.StructName}} {
|
||||
return {{.StructName}}{s.s.Subtract(s.fromStructSlice(others)...)}
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Any() {{.ElemName}} {
|
||||
return {{fromVal .ElemName}}(s.s.Any())
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) fromStructSlice(p []{{.StructName}}) []types.Set {
|
||||
r := make([]types.Set, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = v.s
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) fromElemSlice(p []{{.ElemName}}) []types.Value {
|
||||
r := make([]types.Value, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = v{{toVal .ElemName}}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
// {{.StructName}}
|
||||
|
||||
type {{.StructName}} struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
func New{{.StructName}}() {{.StructName}} {
|
||||
return {{.StructName}}{
|
||||
types.NewMap(types.NewString("$name"), types.NewString("{{.StructName}}")),
|
||||
}
|
||||
}
|
||||
|
||||
func {{.StructName}}FromVal(v types.Value) {{.StructName}} {
|
||||
return {{.StructName}}{v.(types.Map)}
|
||||
}
|
||||
|
||||
// TODO: This was going to be called Value() but it collides with root.value. We need some other place to put the built-in fields like Value() and Equals().
|
||||
func (s {{.StructName}}) NomsValue() types.Map {
|
||||
return s.m
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Equals(p {{.StructName}}) bool {
|
||||
return s.m.Equals(p.m)
|
||||
}
|
||||
|
||||
func (s {{.StructName}}) Ref() ref.Ref {
|
||||
return s.m.Ref()
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
test
|
||||
@@ -1,40 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/attic-labs/noms/nomgen"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ng := nomgen.New("types.go")
|
||||
|
||||
ng.AddType(types.NewMap(
|
||||
types.NewString("$typeDef"), types.NewString("noms.ListDef"),
|
||||
types.NewString("elem"), types.NewString("int32")))
|
||||
|
||||
testSet := ng.AddType(types.NewMap(
|
||||
types.NewString("$typeDef"), types.NewString("noms.SetDef"),
|
||||
types.NewString("elem"), types.NewString("bool")))
|
||||
|
||||
ng.AddType(types.NewMap(
|
||||
types.NewString("$typeDef"), types.NewString("noms.MapDef"),
|
||||
types.NewString("key"), types.NewString("string"),
|
||||
types.NewString("value"), types.NewString("float64")))
|
||||
|
||||
testStruct := ng.AddType(types.NewMap(
|
||||
types.NewString("$typeDef"), types.NewString("noms.StructDef"),
|
||||
types.NewString("$name"), types.NewString("TestStruct"),
|
||||
types.NewString("title"), types.NewString("string")))
|
||||
|
||||
ng.AddType(types.NewMap(
|
||||
types.NewString("$typeDef"), types.NewString("noms.MapDef"),
|
||||
types.NewString("key"), testStruct,
|
||||
types.NewString("value"), testSet))
|
||||
|
||||
ng.AddType(types.NewMap(
|
||||
types.NewString("$typeDef"), types.NewString("noms.SetDef"),
|
||||
types.NewString("$name"), types.NewString("MyTestSet"),
|
||||
types.NewString("elem"), types.NewString("uint32")))
|
||||
|
||||
ng.WriteGo("main")
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/assert"
|
||||
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// TestCodegen uses runs "go run gen/types.go" and then runs
|
||||
// "go build" and finally "./test" which depends on types.go
|
||||
func TestCodegen(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
_, thisfile, _, _ := runtime.Caller(0)
|
||||
dir := path.Dir(thisfile)
|
||||
os.Chdir(dir)
|
||||
|
||||
cmd := exec.Command("go", "run", path.Join("gen", "types.go"))
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Run()
|
||||
assert.NoError(err, "go generate failed")
|
||||
|
||||
cmd = exec.Command("go", "build")
|
||||
cmd.Stderr = os.Stderr
|
||||
err = cmd.Run()
|
||||
assert.NoError(err)
|
||||
|
||||
cmd = exec.Command("./test")
|
||||
cmd.Stderr = os.Stderr
|
||||
err = cmd.Run()
|
||||
assert.NoError(err)
|
||||
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
package main
|
||||
|
||||
//go:generate go run gen/types.go
|
||||
@@ -1,94 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/attic-labs/noms/types"
|
||||
|
||||
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestList(t *testingT) {
|
||||
assert := assert.New(t)
|
||||
|
||||
l := NewListOfInt32()
|
||||
assert.Equal(uint64(0), l.Len())
|
||||
assert.True(l.Empty())
|
||||
l = l.Append(types.Int32(1), types.Int32(2))
|
||||
assert.Equal(uint64(2), l.Len())
|
||||
assert.False(l.Empty())
|
||||
}
|
||||
|
||||
func TestMap(t *testingT) {
|
||||
assert := assert.New(t)
|
||||
|
||||
m := NewMapOfStringToFloat64()
|
||||
assert.Equal(uint64(0), m.Len())
|
||||
assert.True(m.Empty())
|
||||
|
||||
m = m.Set(types.NewString("hi"), types.Float64(float64(42)))
|
||||
assert.Equal(uint64(1), m.Len())
|
||||
assert.False(m.Empty())
|
||||
}
|
||||
|
||||
func TestSet(t *testingT) {
|
||||
assert := assert.New(t)
|
||||
|
||||
s := NewSetOfBool()
|
||||
assert.Equal(uint64(0), s.Len())
|
||||
assert.True(s.Empty())
|
||||
s = s.Insert(types.Bool(true), types.Bool(false))
|
||||
assert.Equal(uint64(2), s.Len())
|
||||
assert.False(s.Empty())
|
||||
}
|
||||
|
||||
func TestStructTest(t *testingT) {
|
||||
assert := assert.New(t)
|
||||
|
||||
s := NewTestStruct()
|
||||
s = s.SetTitle(types.NewString("Hello"))
|
||||
assert.True(s.Title().Equals(types.NewString("Hello")))
|
||||
}
|
||||
|
||||
func TestCompound(t *testingT) {
|
||||
assert := assert.New(t)
|
||||
|
||||
m := NewMapOfTestStructToSetOfBool()
|
||||
k := NewTestStruct()
|
||||
k = k.SetTitle(types.NewString("Hello"))
|
||||
v := NewSetOfBool()
|
||||
m = m.Set(k, v)
|
||||
assert.Equal(m.Get(k), v)
|
||||
}
|
||||
|
||||
func TestCustomName(t *testingT) {
|
||||
assert := assert.New(t)
|
||||
|
||||
s := NewMyTestSet()
|
||||
assert.True(s.Empty())
|
||||
s = s.Insert(types.UInt32(32))
|
||||
assert.Equal(uint64(1), s.Len())
|
||||
}
|
||||
|
||||
type testingT struct {
|
||||
testing.T
|
||||
errors int
|
||||
}
|
||||
|
||||
func (t *testingT) Errorf(format string, args ...interface{}) {
|
||||
t.errors++
|
||||
fmt.Fprintf(os.Stderr, format, args...)
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := &testingT{}
|
||||
TestList(t)
|
||||
TestMap(t)
|
||||
TestSet(t)
|
||||
TestStructTest(t)
|
||||
TestCompound(t)
|
||||
TestCustomName(t)
|
||||
os.Exit(t.errors)
|
||||
}
|
||||
@@ -1,403 +0,0 @@
|
||||
// This file was generated by nomgen.
|
||||
// To regenerate, run `go generate` in this package.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/attic-labs/noms/ref"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
|
||||
// MapOfStringToFloat64
|
||||
|
||||
type MapOfStringToFloat64 struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
type MapOfStringToFloat64IterCallback (func(k types.String, v types.Float64) (stop bool))
|
||||
|
||||
func NewMapOfStringToFloat64() MapOfStringToFloat64 {
|
||||
return MapOfStringToFloat64{types.NewMap()}
|
||||
}
|
||||
|
||||
func MapOfStringToFloat64FromVal(p types.Value) MapOfStringToFloat64 {
|
||||
return MapOfStringToFloat64{p.(types.Map)}
|
||||
}
|
||||
|
||||
func (m MapOfStringToFloat64) NomsValue() types.Map {
|
||||
return m.m
|
||||
}
|
||||
|
||||
func (m MapOfStringToFloat64) Equals(p MapOfStringToFloat64) bool {
|
||||
return m.m.Equals(p.m)
|
||||
}
|
||||
|
||||
func (m MapOfStringToFloat64) Ref() ref.Ref {
|
||||
return m.m.Ref()
|
||||
}
|
||||
|
||||
func (m MapOfStringToFloat64) Empty() bool {
|
||||
return m.m.Empty()
|
||||
}
|
||||
|
||||
func (m MapOfStringToFloat64) Len() uint64 {
|
||||
return m.m.Len()
|
||||
}
|
||||
|
||||
func (m MapOfStringToFloat64) Has(p types.String) bool {
|
||||
return m.m.Has(p)
|
||||
}
|
||||
|
||||
func (m MapOfStringToFloat64) Get(p types.String) types.Float64 {
|
||||
return types.Float64FromVal(m.m.Get(p))
|
||||
}
|
||||
|
||||
func (m MapOfStringToFloat64) Set(k types.String, v types.Float64) MapOfStringToFloat64 {
|
||||
return MapOfStringToFloat64FromVal(m.m.Set(k, v))
|
||||
}
|
||||
|
||||
// TODO: Implement SetM?
|
||||
|
||||
func (m MapOfStringToFloat64) Remove(p types.String) MapOfStringToFloat64 {
|
||||
return MapOfStringToFloat64FromVal(m.m.Remove(p))
|
||||
}
|
||||
|
||||
func (m MapOfStringToFloat64) Iter(cb MapOfStringToFloat64IterCallback) {
|
||||
m.m.Iter(func(k, v types.Value) bool {
|
||||
return cb(types.StringFromVal(k), types.Float64FromVal(v))
|
||||
})
|
||||
}
|
||||
|
||||
// MapOfTestStructToSetOfBool
|
||||
|
||||
type MapOfTestStructToSetOfBool struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
type MapOfTestStructToSetOfBoolIterCallback (func(k TestStruct, v SetOfBool) (stop bool))
|
||||
|
||||
func NewMapOfTestStructToSetOfBool() MapOfTestStructToSetOfBool {
|
||||
return MapOfTestStructToSetOfBool{types.NewMap()}
|
||||
}
|
||||
|
||||
func MapOfTestStructToSetOfBoolFromVal(p types.Value) MapOfTestStructToSetOfBool {
|
||||
return MapOfTestStructToSetOfBool{p.(types.Map)}
|
||||
}
|
||||
|
||||
func (m MapOfTestStructToSetOfBool) NomsValue() types.Map {
|
||||
return m.m
|
||||
}
|
||||
|
||||
func (m MapOfTestStructToSetOfBool) Equals(p MapOfTestStructToSetOfBool) bool {
|
||||
return m.m.Equals(p.m)
|
||||
}
|
||||
|
||||
func (m MapOfTestStructToSetOfBool) Ref() ref.Ref {
|
||||
return m.m.Ref()
|
||||
}
|
||||
|
||||
func (m MapOfTestStructToSetOfBool) Empty() bool {
|
||||
return m.m.Empty()
|
||||
}
|
||||
|
||||
func (m MapOfTestStructToSetOfBool) Len() uint64 {
|
||||
return m.m.Len()
|
||||
}
|
||||
|
||||
func (m MapOfTestStructToSetOfBool) Has(p TestStruct) bool {
|
||||
return m.m.Has(p.NomsValue())
|
||||
}
|
||||
|
||||
func (m MapOfTestStructToSetOfBool) Get(p TestStruct) SetOfBool {
|
||||
return SetOfBoolFromVal(m.m.Get(p.NomsValue()))
|
||||
}
|
||||
|
||||
func (m MapOfTestStructToSetOfBool) Set(k TestStruct, v SetOfBool) MapOfTestStructToSetOfBool {
|
||||
return MapOfTestStructToSetOfBoolFromVal(m.m.Set(k.NomsValue(), v.NomsValue()))
|
||||
}
|
||||
|
||||
// TODO: Implement SetM?
|
||||
|
||||
func (m MapOfTestStructToSetOfBool) Remove(p TestStruct) MapOfTestStructToSetOfBool {
|
||||
return MapOfTestStructToSetOfBoolFromVal(m.m.Remove(p.NomsValue()))
|
||||
}
|
||||
|
||||
func (m MapOfTestStructToSetOfBool) Iter(cb MapOfTestStructToSetOfBoolIterCallback) {
|
||||
m.m.Iter(func(k, v types.Value) bool {
|
||||
return cb(TestStructFromVal(k), SetOfBoolFromVal(v))
|
||||
})
|
||||
}
|
||||
|
||||
// TestStruct
|
||||
|
||||
type TestStruct struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
func NewTestStruct() TestStruct {
|
||||
return TestStruct{
|
||||
types.NewMap(types.NewString("$name"), types.NewString("TestStruct")),
|
||||
}
|
||||
}
|
||||
|
||||
func TestStructFromVal(v types.Value) TestStruct {
|
||||
return TestStruct{v.(types.Map)}
|
||||
}
|
||||
|
||||
// TODO: This was going to be called Value() but it collides with root.value. We need some other place to put the built-in fields like Value() and Equals().
|
||||
func (s TestStruct) NomsValue() types.Map {
|
||||
return s.m
|
||||
}
|
||||
|
||||
func (s TestStruct) Equals(p TestStruct) bool {
|
||||
return s.m.Equals(p.m)
|
||||
}
|
||||
|
||||
func (s TestStruct) Ref() ref.Ref {
|
||||
return s.m.Ref()
|
||||
}
|
||||
|
||||
func (s TestStruct) Title() types.String {
|
||||
return types.StringFromVal(s.m.Get(types.NewString("title")))
|
||||
}
|
||||
|
||||
func (s TestStruct) SetTitle(p types.String) TestStruct {
|
||||
return TestStructFromVal(s.m.Set(types.NewString("title"), p))
|
||||
}
|
||||
|
||||
// MyTestSet
|
||||
|
||||
type MyTestSet struct {
|
||||
s types.Set
|
||||
}
|
||||
|
||||
type MyTestSetIterCallback (func(p types.UInt32) (stop bool))
|
||||
|
||||
func NewMyTestSet() MyTestSet {
|
||||
return MyTestSet{types.NewSet()}
|
||||
}
|
||||
|
||||
func MyTestSetFromVal(p types.Value) MyTestSet {
|
||||
return MyTestSet{p.(types.Set)}
|
||||
}
|
||||
|
||||
func (s MyTestSet) NomsValue() types.Set {
|
||||
return s.s
|
||||
}
|
||||
|
||||
func (s MyTestSet) Equals(p MyTestSet) bool {
|
||||
return s.s.Equals(p.s)
|
||||
}
|
||||
|
||||
func (s MyTestSet) Ref() ref.Ref {
|
||||
return s.s.Ref()
|
||||
}
|
||||
|
||||
func (s MyTestSet) Empty() bool {
|
||||
return s.s.Empty()
|
||||
}
|
||||
|
||||
func (s MyTestSet) Len() uint64 {
|
||||
return s.s.Len()
|
||||
}
|
||||
|
||||
func (s MyTestSet) Has(p types.UInt32) bool {
|
||||
return s.s.Has(p)
|
||||
}
|
||||
|
||||
func (s MyTestSet) Iter(cb MyTestSetIterCallback) {
|
||||
s.s.Iter(func(v types.Value) bool {
|
||||
return cb(types.UInt32FromVal(v))
|
||||
})
|
||||
}
|
||||
|
||||
func (s MyTestSet) Insert(p ...types.UInt32) MyTestSet {
|
||||
return MyTestSet{s.s.Insert(s.fromElemSlice(p)...)}
|
||||
}
|
||||
|
||||
func (s MyTestSet) Remove(p ...types.UInt32) MyTestSet {
|
||||
return MyTestSet{s.s.Remove(s.fromElemSlice(p)...)}
|
||||
}
|
||||
|
||||
func (s MyTestSet) Union(others ...MyTestSet) MyTestSet {
|
||||
return MyTestSet{s.s.Union(s.fromStructSlice(others)...)}
|
||||
}
|
||||
|
||||
func (s MyTestSet) Subtract(others ...MyTestSet) MyTestSet {
|
||||
return MyTestSet{s.s.Subtract(s.fromStructSlice(others)...)}
|
||||
}
|
||||
|
||||
func (s MyTestSet) Any() types.UInt32 {
|
||||
return types.UInt32FromVal(s.s.Any())
|
||||
}
|
||||
|
||||
func (s MyTestSet) fromStructSlice(p []MyTestSet) []types.Set {
|
||||
r := make([]types.Set, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = v.s
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (s MyTestSet) fromElemSlice(p []types.UInt32) []types.Value {
|
||||
r := make([]types.Value, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = v
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// ListOfInt32
|
||||
|
||||
type ListOfInt32 struct {
|
||||
l types.List
|
||||
}
|
||||
|
||||
type ListOfInt32IterCallback (func (p types.Int32) (stop bool))
|
||||
|
||||
func NewListOfInt32() ListOfInt32 {
|
||||
return ListOfInt32{types.NewList()}
|
||||
}
|
||||
|
||||
func ListOfInt32FromVal(p types.Value) ListOfInt32 {
|
||||
return ListOfInt32{p.(types.List)}
|
||||
}
|
||||
|
||||
func (l ListOfInt32) NomsValue() types.List {
|
||||
return l.l
|
||||
}
|
||||
|
||||
func (l ListOfInt32) Equals(p ListOfInt32) bool {
|
||||
return l.l.Equals(p.l)
|
||||
}
|
||||
|
||||
func (l ListOfInt32) Ref() ref.Ref {
|
||||
return l.l.Ref()
|
||||
}
|
||||
|
||||
func (l ListOfInt32) Len() uint64 {
|
||||
return l.l.Len()
|
||||
}
|
||||
|
||||
func (l ListOfInt32) Empty() bool {
|
||||
return l.Len() == uint64(0)
|
||||
}
|
||||
|
||||
func (l ListOfInt32) Get(idx uint64) types.Int32 {
|
||||
return types.Int32FromVal(l.l.Get(idx))
|
||||
}
|
||||
|
||||
func (l ListOfInt32) Slice(idx uint64, end uint64) ListOfInt32 {
|
||||
return ListOfInt32{l.l.Slice(idx, end)}
|
||||
}
|
||||
|
||||
func (l ListOfInt32) Set(idx uint64, v types.Int32) ListOfInt32 {
|
||||
return ListOfInt32{l.l.Set(idx, v)}
|
||||
}
|
||||
|
||||
func (l ListOfInt32) Append(v ...types.Int32) ListOfInt32 {
|
||||
return ListOfInt32{l.l.Append(l.fromElemSlice(v)...)}
|
||||
}
|
||||
|
||||
func (l ListOfInt32) Insert(idx uint64, v ...types.Int32) ListOfInt32 {
|
||||
return ListOfInt32{l.l.Insert(idx, l.fromElemSlice(v)...)}
|
||||
}
|
||||
|
||||
func (l ListOfInt32) Remove(idx uint64, end uint64) ListOfInt32 {
|
||||
return ListOfInt32{l.l.Remove(idx, end)}
|
||||
}
|
||||
|
||||
func (l ListOfInt32) RemoveAt(idx uint64) ListOfInt32 {
|
||||
return ListOfInt32{(l.l.RemoveAt(idx))}
|
||||
}
|
||||
|
||||
func (l ListOfInt32) fromElemSlice(p []types.Int32) []types.Value {
|
||||
r := make([]types.Value, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = v
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// SetOfBool
|
||||
|
||||
type SetOfBool struct {
|
||||
s types.Set
|
||||
}
|
||||
|
||||
type SetOfBoolIterCallback (func(p types.Bool) (stop bool))
|
||||
|
||||
func NewSetOfBool() SetOfBool {
|
||||
return SetOfBool{types.NewSet()}
|
||||
}
|
||||
|
||||
func SetOfBoolFromVal(p types.Value) SetOfBool {
|
||||
return SetOfBool{p.(types.Set)}
|
||||
}
|
||||
|
||||
func (s SetOfBool) NomsValue() types.Set {
|
||||
return s.s
|
||||
}
|
||||
|
||||
func (s SetOfBool) Equals(p SetOfBool) bool {
|
||||
return s.s.Equals(p.s)
|
||||
}
|
||||
|
||||
func (s SetOfBool) Ref() ref.Ref {
|
||||
return s.s.Ref()
|
||||
}
|
||||
|
||||
func (s SetOfBool) Empty() bool {
|
||||
return s.s.Empty()
|
||||
}
|
||||
|
||||
func (s SetOfBool) Len() uint64 {
|
||||
return s.s.Len()
|
||||
}
|
||||
|
||||
func (s SetOfBool) Has(p types.Bool) bool {
|
||||
return s.s.Has(p)
|
||||
}
|
||||
|
||||
func (s SetOfBool) Iter(cb SetOfBoolIterCallback) {
|
||||
s.s.Iter(func(v types.Value) bool {
|
||||
return cb(types.BoolFromVal(v))
|
||||
})
|
||||
}
|
||||
|
||||
func (s SetOfBool) Insert(p ...types.Bool) SetOfBool {
|
||||
return SetOfBool{s.s.Insert(s.fromElemSlice(p)...)}
|
||||
}
|
||||
|
||||
func (s SetOfBool) Remove(p ...types.Bool) SetOfBool {
|
||||
return SetOfBool{s.s.Remove(s.fromElemSlice(p)...)}
|
||||
}
|
||||
|
||||
func (s SetOfBool) Union(others ...SetOfBool) SetOfBool {
|
||||
return SetOfBool{s.s.Union(s.fromStructSlice(others)...)}
|
||||
}
|
||||
|
||||
func (s SetOfBool) Subtract(others ...SetOfBool) SetOfBool {
|
||||
return SetOfBool{s.s.Subtract(s.fromStructSlice(others)...)}
|
||||
}
|
||||
|
||||
func (s SetOfBool) Any() types.Bool {
|
||||
return types.BoolFromVal(s.s.Any())
|
||||
}
|
||||
|
||||
func (s SetOfBool) fromStructSlice(p []SetOfBool) []types.Set {
|
||||
r := make([]types.Set, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = v.s
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (s SetOfBool) fromElemSlice(p []types.Bool) []types.Value {
|
||||
r := make([]types.Value, len(p))
|
||||
for i, v := range p {
|
||||
r[i] = v
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user