Add noms/primitives.go and relatedness

This commit is contained in:
Aaron Boodman
2015-06-02 23:34:51 -07:00
parent 8dd790e7e2
commit 228186dfee
15 changed files with 253 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
package gen
import (
"github.com/clipperhouse/typewriter"
)
func init() {
templates = append(templates, &typewriter.Template{
Name: "noms",
Text: `
func (self {{.Name}}) Equals(other Value) bool {
if other, ok := other.({{.Name}}); ok {
return self == other
} else {
return false
}
}
`,
TypeConstraint: typewriter.Constraint{Comparable: true},
})
}
+43
View File
@@ -0,0 +1,43 @@
package gen
import (
"io"
. "github.com/attic-labs/noms/dbg"
"github.com/clipperhouse/typewriter"
)
var (
templates = typewriter.TemplateSlice{}
)
func init() {
Chk.NoError(typewriter.Register(&nomWriter{}))
}
type nomWriter struct{}
func (nw *nomWriter) Name() string {
return "noms"
}
func (nw *nomWriter) Imports(t typewriter.Type) []typewriter.ImportSpec {
return []typewriter.ImportSpec{}
}
func (nw *nomWriter) Write(w io.Writer, typ typewriter.Type) error {
tag, found := typ.FindTag(nw)
if !found {
return nil
}
tmpl, err := templates.ByTag(typ, tag)
if err != nil {
return err
}
if err := tmpl.Execute(w, typ); err != nil {
return err
}
return nil
}
+7
View File
@@ -0,0 +1,7 @@
package main
import (
_ "github.com/attic-labs/noms/codegen"
_ "github.com/clipperhouse/slice"
_ "github.com/clipperhouse/stringer"
)
+13
View File
@@ -0,0 +1,13 @@
// Generated by: main
// TypeWriter: noms
// Directive: +gen on Bool
package types
func (self Bool) Equals(other Value) bool {
if other, ok := other.(Bool); ok {
return self == other
} else {
return false
}
}
+13
View File
@@ -0,0 +1,13 @@
// Generated by: main
// TypeWriter: noms
// Directive: +gen on Float32
package types
func (self Float32) Equals(other Value) bool {
if other, ok := other.(Float32); ok {
return self == other
} else {
return false
}
}
+13
View File
@@ -0,0 +1,13 @@
// Generated by: main
// TypeWriter: noms
// Directive: +gen on Float64
package types
func (self Float64) Equals(other Value) bool {
if other, ok := other.(Float64); ok {
return self == other
} else {
return false
}
}
+13
View File
@@ -0,0 +1,13 @@
// Generated by: main
// TypeWriter: noms
// Directive: +gen on Int16
package types
func (self Int16) Equals(other Value) bool {
if other, ok := other.(Int16); ok {
return self == other
} else {
return false
}
}
+13
View File
@@ -0,0 +1,13 @@
// Generated by: main
// TypeWriter: noms
// Directive: +gen on Int32
package types
func (self Int32) Equals(other Value) bool {
if other, ok := other.(Int32); ok {
return self == other
} else {
return false
}
}
+13
View File
@@ -0,0 +1,13 @@
// Generated by: main
// TypeWriter: noms
// Directive: +gen on Int64
package types
func (self Int64) Equals(other Value) bool {
if other, ok := other.(Int64); ok {
return self == other
} else {
return false
}
}
+28
View File
@@ -0,0 +1,28 @@
package types
// +gen noms
type Bool bool
// +gen noms
type Int16 int16
// +gen noms
type Int32 int32
// +gen noms
type Int64 int64
// +gen noms
type UInt16 uint16
// +gen noms
type UInt32 uint32
// +gen noms
type UInt64 uint64
// +gen noms
type Float32 float32
// +gen noms
type Float64 float64
+31
View File
@@ -0,0 +1,31 @@
package types
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPrimitives(t *testing.T) {
data := []Value{
Bool(true), Bool(false),
Int16(0), Int16(-1),
Int32(0), Int32(-1),
Int64(0), Int64(-1),
UInt16(0), UInt16(1),
UInt32(0), UInt32(1),
UInt64(0), UInt64(1),
Float32(0.0), Float32(0.1),
Float64(0.0), Float64(0.1),
}
for i := range data {
for j := range data {
if i == j {
assert.True(t, data[i].Equals(data[j]), "Expected value to equal self at index %d", i)
} else {
assert.False(t, data[i].Equals(data[j]), "Expected values at indices %d and %d to not equal", i, j)
}
}
}
}
+13
View File
@@ -0,0 +1,13 @@
// Generated by: main
// TypeWriter: noms
// Directive: +gen on UInt16
package types
func (self UInt16) Equals(other Value) bool {
if other, ok := other.(UInt16); ok {
return self == other
} else {
return false
}
}
+13
View File
@@ -0,0 +1,13 @@
// Generated by: main
// TypeWriter: noms
// Directive: +gen on UInt32
package types
func (self UInt32) Equals(other Value) bool {
if other, ok := other.(UInt32); ok {
return self == other
} else {
return false
}
}
+13
View File
@@ -0,0 +1,13 @@
// Generated by: main
// TypeWriter: noms
// Directive: +gen on UInt64
package types
func (self UInt64) Equals(other Value) bool {
if other, ok := other.(UInt64); ok {
return self == other
} else {
return false
}
}
+6
View File
@@ -0,0 +1,6 @@
package types
// Value is implemented by every noms value
type Value interface {
Equals(other Value) bool
}