Use go generate instead of custom thing for generating primitives

This commit is contained in:
Aaron Boodman
2015-07-10 10:39:31 -07:00
parent fd75f33c8c
commit 82a87d548c
25 changed files with 195 additions and 336 deletions
-25
View File
@@ -1,25 +0,0 @@
package gen
import (
"github.com/clipperhouse/typewriter"
)
func init() {
templates = append(templates, &typewriter.Template{
Name: "value",
Text: `
func (self {{.Name}}) Equals(other Value) bool {
if other, ok := other.({{.Name}}); ok {
return self == other
} else {
return false
}
}
func (v {{.Name}}) Ref() ref.Ref {
return getRef(v)
}
`,
TypeConstraint: typewriter.Constraint{Comparable: true},
})
}
-55
View File
@@ -1,55 +0,0 @@
package gen
import (
"io"
. "github.com/attic-labs/noms/dbg"
"github.com/clipperhouse/typewriter"
)
var (
templates = typewriter.TemplateSlice{}
)
func init() {
Chk.NoError(typewriter.Register(&valueWriter{}))
}
type valueWriter struct{}
func (nw *valueWriter) Name() string {
return "value"
}
func (nw *valueWriter) Imports(t typewriter.Type) []typewriter.ImportSpec {
return []typewriter.ImportSpec{
typewriter.ImportSpec{
Path: "github.com/attic-labs/noms/ref",
},
}
}
func (nw *valueWriter) 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
}
w.Write([]byte(`
// DO NOT EDIT
//
// This file was generated by a tool.
// See http://clipperhouse.github.io/gen for details.
`))
if err := tmpl.Execute(w, typ); err != nil {
return err
}
return nil
}
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"flag"
"os"
"github.com/attic-labs/noms/codegen/nomgen"
"github.com/attic-labs/noms/nomgen"
. "github.com/attic-labs/noms/dbg"
"github.com/attic-labs/noms/types"
)
+1 -1
View File
@@ -14,7 +14,7 @@ type RootSet struct {
s types.Set
}
type RootSetIterCallback (func(p Root) (stop bool))
type RootSetIterCallback (func (p Root) (stop bool))
func NewRootSet() RootSet {
return RootSet{types.NewSet()}
@@ -1,4 +1,4 @@
func(s {{.StructName}}) {{.GoFieldName}}() {{.FieldType}} {
func (s {{.StructName}}) {{.GoFieldName}}() {{.FieldType}} {
return {{.FieldType}}FromVal(s.m.Get(types.NewString("{{.FieldName}}")))
}
@@ -1,4 +1,4 @@
func(s {{.StructName}}) {{.GoFieldName}}() {{.FieldType}} {
func (s {{.StructName}}) {{.GoFieldName}}() {{.FieldType}} {
return s.m.Get(types.NewString("{{.FieldName}}")).({{.FieldType}})
}
-7
View File
@@ -1,7 +0,0 @@
package main
import (
_ "github.com/attic-labs/noms/codegen/value"
_ "github.com/clipperhouse/slice"
_ "github.com/clipperhouse/stringer"
)
-26
View File
@@ -1,26 +0,0 @@
// Generated by: main
// TypeWriter: value
// Directive: +gen on Bool
package types
import (
"github.com/attic-labs/noms/ref"
)
// DO NOT EDIT
//
// This file was generated by a tool.
// See http://clipperhouse.github.io/gen for details.
func (self Bool) Equals(other Value) bool {
if other, ok := other.(Bool); ok {
return self == other
} else {
return false
}
}
func (v Bool) Ref() ref.Ref {
return getRef(v)
}
-26
View File
@@ -1,26 +0,0 @@
// Generated by: main
// TypeWriter: value
// Directive: +gen on Float32
package types
import (
"github.com/attic-labs/noms/ref"
)
// DO NOT EDIT
//
// This file was generated by a tool.
// See http://clipperhouse.github.io/gen for details.
func (self Float32) Equals(other Value) bool {
if other, ok := other.(Float32); ok {
return self == other
} else {
return false
}
}
func (v Float32) Ref() ref.Ref {
return getRef(v)
}
-26
View File
@@ -1,26 +0,0 @@
// Generated by: main
// TypeWriter: value
// Directive: +gen on Float64
package types
import (
"github.com/attic-labs/noms/ref"
)
// DO NOT EDIT
//
// This file was generated by a tool.
// See http://clipperhouse.github.io/gen for details.
func (self Float64) Equals(other Value) bool {
if other, ok := other.(Float64); ok {
return self == other
} else {
return false
}
}
func (v Float64) Ref() ref.Ref {
return getRef(v)
}
+3
View File
@@ -0,0 +1,3 @@
package types
//go:generate go run gen/main.go
+9
View File
@@ -0,0 +1,9 @@
// DO NOT EDIT: This file was generated.
// To regenerate, run `go generate` in this package.
package types
import (
"github.com/attic-labs/noms/ref"
)
+50
View File
@@ -0,0 +1,50 @@
package main
import (
"io/ioutil"
"log"
"os"
"path"
"runtime"
"strings"
"text/template"
. "github.com/attic-labs/noms/dbg"
)
var (
headerTempl = readTemplate("header.tmpl")
primitiveTempl = readTemplate("primitive.tmpl")
)
func main() {
types := []string{"Bool", "Int16", "Int32", "Int64", "UInt16", "UInt32", "UInt64", "Float32", "Float64"}
f, err := os.OpenFile("primitives.go", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatal(err)
return
}
headerTempl.Execute(f, nil)
for _, t := range types {
goType := strings.ToLower(t)
primitiveTempl.Execute(f, struct {
NomsType string
GoType string
}{t, goType})
}
}
func readTemplate(name string) *template.Template {
_, thisfile, _, _ := runtime.Caller(1)
f, err := os.Open(path.Join(path.Dir(thisfile), name))
Chk.NoError(err)
defer f.Close()
b, err := ioutil.ReadAll(f)
Chk.NoError(err)
t, err := template.New(name).Parse(string(b))
Chk.NoError(err)
return t
}
+14
View File
@@ -0,0 +1,14 @@
type {{.NomsType}} {{.GoType}}
func (self {{.NomsType}}) Equals(other Value) bool {
if other, ok := other.({{.NomsType}}); ok {
return self == other
} else {
return false
}
}
func (v {{.NomsType}}) Ref() ref.Ref {
return getRef(v)
}
-26
View File
@@ -1,26 +0,0 @@
// Generated by: main
// TypeWriter: value
// Directive: +gen on Int16
package types
import (
"github.com/attic-labs/noms/ref"
)
// DO NOT EDIT
//
// This file was generated by a tool.
// See http://clipperhouse.github.io/gen for details.
func (self Int16) Equals(other Value) bool {
if other, ok := other.(Int16); ok {
return self == other
} else {
return false
}
}
func (v Int16) Ref() ref.Ref {
return getRef(v)
}
-26
View File
@@ -1,26 +0,0 @@
// Generated by: main
// TypeWriter: value
// Directive: +gen on Int32
package types
import (
"github.com/attic-labs/noms/ref"
)
// DO NOT EDIT
//
// This file was generated by a tool.
// See http://clipperhouse.github.io/gen for details.
func (self Int32) Equals(other Value) bool {
if other, ok := other.(Int32); ok {
return self == other
} else {
return false
}
}
func (v Int32) Ref() ref.Ref {
return getRef(v)
}
-26
View File
@@ -1,26 +0,0 @@
// Generated by: main
// TypeWriter: value
// Directive: +gen on Int64
package types
import (
"github.com/attic-labs/noms/ref"
)
// DO NOT EDIT
//
// This file was generated by a tool.
// See http://clipperhouse.github.io/gen for details.
func (self Int64) Equals(other Value) bool {
if other, ok := other.(Int64); ok {
return self == other
} else {
return false
}
}
func (v Int64) Ref() ref.Ref {
return getRef(v)
}
+115 -11
View File
@@ -1,31 +1,135 @@
// DO NOT EDIT: This file was generated.
// To regenerate, run `go generate` in this package.
package types
// The methods for these types are generated by a tool.
// See http://clipperhouse.github.io/gen.
import (
"github.com/attic-labs/noms/ref"
)
// +gen value
type Bool bool
// +gen value
func (self Bool) Equals(other Value) bool {
if other, ok := other.(Bool); ok {
return self == other
} else {
return false
}
}
func (v Bool) Ref() ref.Ref {
return getRef(v)
}
type Int16 int16
// +gen value
func (self Int16) Equals(other Value) bool {
if other, ok := other.(Int16); ok {
return self == other
} else {
return false
}
}
func (v Int16) Ref() ref.Ref {
return getRef(v)
}
type Int32 int32
// +gen value
func (self Int32) Equals(other Value) bool {
if other, ok := other.(Int32); ok {
return self == other
} else {
return false
}
}
func (v Int32) Ref() ref.Ref {
return getRef(v)
}
type Int64 int64
// +gen value
func (self Int64) Equals(other Value) bool {
if other, ok := other.(Int64); ok {
return self == other
} else {
return false
}
}
func (v Int64) Ref() ref.Ref {
return getRef(v)
}
type UInt16 uint16
// +gen value
func (self UInt16) Equals(other Value) bool {
if other, ok := other.(UInt16); ok {
return self == other
} else {
return false
}
}
func (v UInt16) Ref() ref.Ref {
return getRef(v)
}
type UInt32 uint32
// +gen value
func (self UInt32) Equals(other Value) bool {
if other, ok := other.(UInt32); ok {
return self == other
} else {
return false
}
}
func (v UInt32) Ref() ref.Ref {
return getRef(v)
}
type UInt64 uint64
// +gen value
func (self UInt64) Equals(other Value) bool {
if other, ok := other.(UInt64); ok {
return self == other
} else {
return false
}
}
func (v UInt64) Ref() ref.Ref {
return getRef(v)
}
type Float32 float32
// +gen value
func (self Float32) Equals(other Value) bool {
if other, ok := other.(Float32); ok {
return self == other
} else {
return false
}
}
func (v Float32) Ref() ref.Ref {
return getRef(v)
}
type Float64 float64
func (self Float64) Equals(other Value) bool {
if other, ok := other.(Float64); ok {
return self == other
} else {
return false
}
}
func (v Float64) Ref() ref.Ref {
return getRef(v)
}
-26
View File
@@ -1,26 +0,0 @@
// Generated by: main
// TypeWriter: value
// Directive: +gen on UInt16
package types
import (
"github.com/attic-labs/noms/ref"
)
// DO NOT EDIT
//
// This file was generated by a tool.
// See http://clipperhouse.github.io/gen for details.
func (self UInt16) Equals(other Value) bool {
if other, ok := other.(UInt16); ok {
return self == other
} else {
return false
}
}
func (v UInt16) Ref() ref.Ref {
return getRef(v)
}
-26
View File
@@ -1,26 +0,0 @@
// Generated by: main
// TypeWriter: value
// Directive: +gen on UInt32
package types
import (
"github.com/attic-labs/noms/ref"
)
// DO NOT EDIT
//
// This file was generated by a tool.
// See http://clipperhouse.github.io/gen for details.
func (self UInt32) Equals(other Value) bool {
if other, ok := other.(UInt32); ok {
return self == other
} else {
return false
}
}
func (v UInt32) Ref() ref.Ref {
return getRef(v)
}
-26
View File
@@ -1,26 +0,0 @@
// Generated by: main
// TypeWriter: value
// Directive: +gen on UInt64
package types
import (
"github.com/attic-labs/noms/ref"
)
// DO NOT EDIT
//
// This file was generated by a tool.
// See http://clipperhouse.github.io/gen for details.
func (self UInt64) Equals(other Value) bool {
if other, ok := other.(UInt64); ok {
return self == other
} else {
return false
}
}
func (v UInt64) Ref() ref.Ref {
return getRef(v)
}