Skip to content

Commit

Permalink
Model: Add has_one has_many reource name checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Focinfi committed Feb 6, 2016
1 parent f0c067b commit 042e265
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 5 additions & 4 deletions column.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Column struct {
// CheckType check type if is in the jsonTypes
func (column Column) CheckMeta() error {
if column.Name == "" {
return ColumnsErrorf("colmun[content=%#v] must has a name", column)
return ColumnsErrorf("colmun[content=%v] must has a name", column)
}

columnLogName := fmt.Sprintf("column[name=\"%s\"]", column.Name)
Expand All @@ -64,7 +64,7 @@ func (column Column) CheckMeta() error {

// type must be in jsonTypes
if !jsonTypes.Has(JsonType(column.Type)) {
return ColumnsErrorf("%s use unsupportted type: %s, all supportted types: %#v", columnLogName, column.Type, jsonTypes.ToSlice())
return ColumnsErrorf("%s use unsupportted type: %s, all supportted types: %v", columnLogName, column.Type, jsonTypes.ToSlice())
}

// check regexp format
Expand All @@ -89,7 +89,7 @@ func (column *Column) CheckRelationships(seedVal interface{}, model *Model) erro
resPluralName := inflection.Plural(resName)
router, ok := model.router.apiFaker.Routers[resPluralName]
if !ok {
return ColumnsErrorf("%s has no resource[resoure_name=\"%s\"] %s in column: %#v", columnLogName, resPluralName, column)
return ColumnsErrorf("%s has no resource[resoure_name=\"%s\"] %s in column: %v", columnLogName, resPluralName, column)
}

for _, li := range router.Model.ToLineItems() {
Expand All @@ -107,9 +107,10 @@ func (column *Column) CheckValue(seedVal interface{}, model *Model) error {
// check type
columnLogName := fmt.Sprintf("column[name=\"%s\"]", column.Name)
goType := JsonType(column.Type).GoType()
jsonType := JsonType(column.Type).Name()
seedType := reflect.TypeOf(seedVal).String()
if seedType != goType {
return ColumnsErrorf("%s has wrong type, expected %s, current is %s", columnLogName, goType, seedType)
return ColumnsErrorf("%s has wrong type, expected %s, current is %s", columnLogName, jsonType, seedType)
}

// check regexp pattern matching
Expand Down
17 changes: 15 additions & 2 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,19 @@ func (model *Model) checkColumnsMeta() error {

// CheckRelationship
func (model *Model) CheckRelationship(seed map[string]interface{}) error {
for _, resoureName := range model.HasOne {
if _, ok := model.router.apiFaker.Routers[inflection.Plural(resoureName)]; !ok {
return HasOneErrorf("use unknown reource %s in file: %s", resoureName, model.router.filePath)
}

}
for _, resoureName := range model.HasMany {
if _, ok := model.router.apiFaker.Routers[inflection.Plural(resoureName)]; !ok {
return HasOneErrorf("use unknown reource \"%s\" in file: %s", resoureName, model.router.filePath)
}

}

for _, column := range model.Columns {
if err := column.CheckRelationships(seed[column.Name], model); err != nil {
return err
Expand All @@ -382,12 +395,12 @@ func (model *Model) checkSeedBasic(seed map[string]interface{}) error {
columns := model.Columns

if len(seed) != len(columns) {
return SeedsErrorf("has wrong number of columns: %#v", seed)
return SeedsErrorf("has wrong number of columns: %v", seed)
}

for _, column := range columns {
if seedVal, ok := seed[column.Name]; !ok {
return SeedsErrorf("has no column %s in seed %#v", column.Name, seed)
return SeedsErrorf("has no column %s in seed %v", column.Name, seed)
} else {
if err := column.CheckValue(seedVal, model); err != nil {
return err
Expand Down

0 comments on commit 042e265

Please sign in to comment.