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
}