Skip to content

Commit

Permalink
working dsl, non-working templates, godoc done
Browse files Browse the repository at this point in the history
  • Loading branch information
bketelsen committed Feb 5, 2016
1 parent 3d37e8d commit 009c439
Show file tree
Hide file tree
Showing 25 changed files with 530 additions and 1,138 deletions.
32 changes: 3 additions & 29 deletions definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ type RelationalModelDefinition struct {
HasMany map[string]*RelationalModelDefinition
HasOne map[string]*RelationalModelDefinition
ManyToMany map[string]*ManyToManyDefinition
Maps map[string]*Mapping
Alias string // gorm:tablename
Cached bool
CacheDuration int
Expand All @@ -63,33 +62,10 @@ type RelationalModelDefinition struct {
many2many []string
}

type Mapping struct {
dslengine.Definition
*design.AttributeDefinition
DefinitionDSL func()
MappingName string
Description string
Remote *design.UserTypeDefinition
Parent *RelationalModelDefinition
Mappings map[string]*MapDefinition // keyed by gorma field
}

func NewMapping() *Mapping {
baseAttr := &design.AttributeDefinition{
Description: "mapping",
}
m := &Mapping{
Mappings: make(map[string]*MapDefinition),
AttributeDefinition: baseAttr,
}
return m
}

// MapDefinition represents something
type MapDefinition struct {
RemoteType *design.UserTypeDefinition
RemoteField string
ParentField string
GormaType FieldType // Override computed field type
}

// RelationalFieldDefinition represents
Expand All @@ -99,7 +75,7 @@ type RelationalFieldDefinition struct {
DefinitionDSL func()
Parent *RelationalModelDefinition
a *design.AttributeDefinition
Name string
FieldName string
Datatype FieldType
SQLTag string
DatabaseFieldName string
Expand All @@ -113,6 +89,7 @@ type RelationalFieldDefinition struct {
HasOne string
HasMany string
Many2Many string
Mappings map[string]*MapDefinition
}

// ManyToManyDefinition stores information about a ManyToMany
Expand All @@ -134,9 +111,6 @@ type StoreIterator func(m *RelationalStoreDefinition) error
// RelationalStore
type ModelIterator func(m *RelationalModelDefinition) error

// SourceMapIterator
type MapIterator func(m *Mapping) error

// FieldIterator is a function that iterates over Fields
// in a RelationalModel
type FieldIterator func(m *RelationalFieldDefinition) error
52 changes: 43 additions & 9 deletions dsl/relationalfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"bitbucket.org/pkg/inflect"

"github.com/goadesign/goa/design"
"github.com/goadesign/goa/dslengine"
"github.com/goadesign/goa/goagen/codegen"
"github.com/goadesign/gorma"
Expand All @@ -26,19 +27,18 @@ func Field(name string, args ...interface{}) {
checkInit()
name = codegen.Goify(name, true)
name = SanitizeFieldName(name)
fieldType, dsl := parseModelArgs(args...)
fieldType, dsl := parseFieldArgs(args...)
if s, ok := relationalModelDefinition(true); ok {
if s.RelationalFields == nil {
s.RelationalFields = make(map[string]*gorma.RelationalFieldDefinition)
}
field, ok := s.RelationalFields[name]
if !ok {
field = &gorma.RelationalFieldDefinition{
Name: name,
DefinitionDSL: dsl,
Parent: s,
Datatype: fieldType,
}
field = gorma.NewRelationalFieldDefinition()
field.FieldName = name
field.DefinitionDSL = dsl
field.Parent = s
field.Datatype = fieldType
} else {
// the field was auto-added by the model parser
// so we need to update whatever we can from this new definition
Expand All @@ -61,13 +61,38 @@ func Field(name string, args ...interface{}) {
field.Nullable = true
field.Description = "nullable timestamp (soft delete)"
}

field.DatabaseFieldName = SanitizeDBFieldName(name)

s.RelationalFields[name] = field
}
}

// MapsFrom establishes a mapping relationship between a source
// Type field and this model. The source type must be a UserTypeDefinition "Type"
// in goa. These are typically Payloads.
func MapsFrom(utd *design.UserTypeDefinition, field string) {
if f, ok := relationalFieldDefinition(true); ok {
checkInit()
md := gorma.NewMapDefinition()
md.RemoteField = field
md.RemoteType = utd
f.Mappings[utd.TypeName] = md

}
}

// MapsTo establishes a relationship between a field in model and
// a MediaType in goa.
func MapsTo(mtd *design.MediaTypeDefinition, field string) {
if f, ok := relationalFieldDefinition(true); ok {
checkInit()
md := gorma.NewMapDefinition()
md.RemoteField = field
md.RemoteType = mtd.UserTypeDefinition
f.Mappings[mtd.UserTypeDefinition.TypeName] = md
}
}

func fixID(s string) string {
if s == "i_d" {
return "id"
Expand All @@ -83,6 +108,15 @@ func Nullable() {
}
}

// PrimaryKey establishes a field as a Primary Key by
// seting the struct tags necessary to create the PK in gorm.
func PrimaryKey() {
if f, ok := relationalFieldDefinition(true); ok {
checkInit()
f.PrimaryKey = true
}
}

// SanitizeFieldName is exported for testing purposes
func SanitizeFieldName(name string) string {
name = codegen.Goify(name, true)
Expand All @@ -107,7 +141,7 @@ func SanitizeDBFieldName(name string) string {
}
return name
}
func parseModelArgs(args ...interface{}) (gorma.FieldType, func()) {
func parseFieldArgs(args ...interface{}) (gorma.FieldType, func()) {
var (
fieldType gorma.FieldType
dslp func()
Expand Down
14 changes: 5 additions & 9 deletions dsl/relationalfield_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dsl_test
import (
"github.com/goadesign/gorma"
gdsl "github.com/goadesign/gorma/dsl"
"github.com/kr/pretty"

. "github.com/goadesign/goa/design"
. "github.com/goadesign/goa/design/apidsl"
Expand Down Expand Up @@ -39,9 +38,7 @@ var _ = Describe("RelationalField", func() {
gdsl.StorageGroup(sgname, func() {
gdsl.Store(storename, gorma.MySQL, func() {
gdsl.Model(modelname, func() {
gdsl.BuiltFrom(RandomPayload, func() {
gdsl.Map("first_name", "first_name", gorma.String)
})
gdsl.BuildsFrom(RandomPayload)
gdsl.Field(name, ft, dsl)
gdsl.Field("id", gorma.PKInteger, dsl) // use lowercase "id" to test sanitizer
gdsl.Field("MiddleName", gorma.String)
Expand All @@ -65,8 +62,7 @@ var _ = Describe("RelationalField", func() {
sg := gorma.GormaDesign
rs := sg.RelationalStores[storename]
rm := rs.RelationalModels[modelname]
pretty.Println(rm)
Ω(rm.RelationalFields[name].Name).Should(Equal(name))
Ω(rm.RelationalFields[name].FieldName).Should(Equal(name))
})
})

Expand Down Expand Up @@ -121,7 +117,7 @@ var _ = Describe("RelationalField", func() {
sg := gorma.GormaDesign
rs := sg.RelationalStores[storename]
rm := rs.RelationalModels[modelname]
Ω(rm.RelationalFields["ID"].Name).Should(Equal("ID"))
Ω(rm.RelationalFields["ID"].FieldName).Should(Equal("ID"))
})
It("sets the field type", func() {
sg := gorma.GormaDesign
Expand All @@ -139,15 +135,15 @@ var _ = Describe("RelationalField", func() {
sg := gorma.GormaDesign
rs := sg.RelationalStores[storename]
rm := rs.RelationalModels[modelname]
Ω(rm.RelationalFields["CreatedAt"].Name).Should(Equal("CreatedAt"))
Ω(rm.RelationalFields["CreatedAt"].FieldName).Should(Equal("CreatedAt"))
Ω(rm.RelationalFields["CreatedAt"].Datatype).Should(Equal(gorma.Timestamp))
Ω(rm.RelationalFields["CreatedAt"].Nullable).Should(Equal(false))
})
It("sets has a deleted at field", func() {
sg := gorma.GormaDesign
rs := sg.RelationalStores[storename]
rm := rs.RelationalModels[modelname]
Ω(rm.RelationalFields["DeletedAt"].Name).Should(Equal("DeletedAt"))
Ω(rm.RelationalFields["DeletedAt"].FieldName).Should(Equal("DeletedAt"))
Ω(rm.RelationalFields["DeletedAt"].Datatype).Should(Equal(gorma.NullableTimestamp))
Ω(rm.RelationalFields["DeletedAt"].Nullable).Should(Equal(true))
})
Expand Down
Loading

0 comments on commit 009c439

Please sign in to comment.