Skip to content

Commit

Permalink
Use NewError2 in type.go.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Aug 2, 2017
1 parent 529a837 commit 16a7245
Showing 1 changed file with 39 additions and 60 deletions.
99 changes: 39 additions & 60 deletions v2/type.go
Expand Up @@ -45,21 +45,18 @@ type ColumnField struct {
func checkTableName(s string) error {
switch s {
case "":
return NewError(TypeError, map[string]interface{}{
"name": s,
"error": "A table name must not be empty.",
return NewError2(TypeError, "A table name must not be empty.", map[string]interface{}{
"name": s,
})
case "Bool", "Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64",
"Float", "ShortText", "Text", "LongText", "Time", "WGS84GeoPoint", "TokyoGeoPoint":
return NewError(TypeError, map[string]interface{}{
"name": s,
"error": "The name specifies a built-in type and not available as a table name.",
return NewError2(TypeError, "The name specifies a built-in type and not available as a table name.", map[string]interface{}{
"name": s,
})
}
if s[0] == '_' {
return NewError(TypeError, map[string]interface{}{
"name": s,
"error": "A table name must not start with '_'.",
return NewError2(TypeError, "A table name must not start with '_'.", map[string]interface{}{
"name": s,
})
}
for _, c := range s {
Expand All @@ -69,9 +66,8 @@ func checkTableName(s string) error {
case c >= 'a' && c <= 'z':
case c == '_':
default:
return NewError(TypeError, map[string]interface{}{
"name": s,
"error": "A table name must consist of [0-9A-Za-z_].",
return NewError2(TypeError, "A table name must consist of [0-9A-Za-z_].", map[string]interface{}{
"name": s,
})
}
}
Expand All @@ -81,10 +77,9 @@ func checkTableName(s string) error {
// parseIDOptions parses options of _id.
func (cf *ColumnField) parseIDOptions(options []string) error {
if len(options) > 1 {
return NewError(TypeError, map[string]interface{}{
return NewError2(TypeError, "The tag must not contain more than one option.", map[string]interface{}{
"name": cf.Name,
"options": options,
"error": "The tag must not contain more than one option.",
})
}
if len(options) > 0 {
Expand All @@ -95,9 +90,8 @@ func (cf *ColumnField) parseIDOptions(options []string) error {
cf.Type = "UInt32"
case "UInt32":
default:
return NewError(TypeError, map[string]interface{}{
"type": cf.Type,
"error": "The type is not supported as _id.",
return NewError2(TypeError, "The type is not supported as _id.", map[string]interface{}{
"type": cf.Type,
})
}
return nil
Expand Down Expand Up @@ -141,18 +135,16 @@ func (cf *ColumnField) checkKeyType() error {
}
}
if cf.Type == "" {
return NewError(TypeError, map[string]interface{}{
"type": reflect.TypeOf(cf.Field.Type).Name(),
"error": "The type is not supported as _key.",
return NewError2(TypeError, "The type is not supported as _key.", map[string]interface{}{
"type": reflect.TypeOf(cf.Field.Type).Name(),
})
}
case "Bool", "Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64",
"Float", "ShortText", "Time", "WGS84GeoPoint", "TokyoGeoPoint":
default:
if err := checkTableName(cf.Type); err != nil {
return NewError(TypeError, map[string]interface{}{
"type": cf.Type,
"error": "The type is not supported as _key.",
return NewError2(TypeError, "The type is not supported as _key.", map[string]interface{}{
"type": cf.Type,
})
}
}
Expand Down Expand Up @@ -237,17 +229,15 @@ func (cf *ColumnField) checkValue() error {
}
}
if cf.Type == "" {
return NewError(TypeError, map[string]interface{}{
"type": reflect.TypeOf(cf.Field.Type).Name(),
"error": "The type is not supported as _value.",
return NewError2(TypeError, "The type is not supported as _value.", map[string]interface{}{
"type": reflect.TypeOf(cf.Field.Type).Name(),
})
}
case "Bool", "Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64",
"Float", "Time", "WGS84GeoPoint", "TokyoGeoPoint":
default:
return NewError(TypeError, map[string]interface{}{
"type": cf.Type,
"error": "The type is not supported as _value.",
return NewError2(TypeError, "The type is not supported as _value.", map[string]interface{}{
"type": cf.Type,
})
}
return nil
Expand All @@ -256,10 +246,9 @@ func (cf *ColumnField) checkValue() error {
// parseValueOptions parses options of _value.
func (cf *ColumnField) parseValueOptions(options []string) error {
if len(options) > 1 {
return NewError(TypeError, map[string]interface{}{
return NewError2(TypeError, "The tag must not contain more than one option.", map[string]interface{}{
"name": cf.Name,
"options": options,
"error": "The tag must not contain more than one option.",
})
}
if len(options) > 0 {
Expand All @@ -275,10 +264,9 @@ func (cf *ColumnField) parseValueOptions(options []string) error {
// parseScoreOptions parses options of _score.
func (cf *ColumnField) parseScoreOptions(options []string) error {
if len(options) > 1 {
return NewError(TypeError, map[string]interface{}{
return NewError2(TypeError, "The tag must not contain more than one option.", map[string]interface{}{
"name": cf.Name,
"options": options,
"error": "The tag must not contain more than one option.",
})
}
if len(options) > 0 {
Expand All @@ -291,9 +279,8 @@ func (cf *ColumnField) parseScoreOptions(options []string) error {
cf.Type = "Float"
case "Int32", "Float":
default:
return NewError(TypeError, map[string]interface{}{
"type": cf.Type,
"error": "The type is not supported as _score.",
return NewError2(TypeError, "The type is not supported as _score.", map[string]interface{}{
"type": cf.Type,
})
}
return nil
Expand Down Expand Up @@ -347,9 +334,8 @@ Loop:
}
}
if cf.Type == "" {
return NewError(TypeError, map[string]interface{}{
"type": reflect.TypeOf(cf.Field.Type).Name(),
"error": "The type is not supported as a column.",
return NewError2(TypeError, "The type is not supported as a column.", map[string]interface{}{
"type": reflect.TypeOf(cf.Field.Type).Name(),
})
}
cf.Type = strings.Repeat("[]", dim) + cf.Type
Expand All @@ -372,9 +358,8 @@ func (cf *ColumnField) checkColumnType() error {
"Float", "ShortText", "Text", "LongText", "Time", "WGS84GeoPoint", "TokyoGeoPoint":
default:
if err := checkTableName(typ); err != nil {
return NewError(TypeError, map[string]interface{}{
"type": cf.Type,
"error": "The type is not supported as a column.",
return NewError2(TypeError, "The type is not supported as a column.", map[string]interface{}{
"type": cf.Type,
})
}
}
Expand All @@ -386,15 +371,13 @@ func (cf *ColumnField) checkColumnType() error {
func (cf *ColumnField) checkColumnName() error {
s := cf.Name
if s == "" {
return NewError(TypeError, map[string]interface{}{
"name": s,
"error": "A column name must not be empty.",
return NewError2(TypeError, "A column name must not be empty.", map[string]interface{}{
"name": s,
})
}
if s[0] == '_' {
return NewError(TypeError, map[string]interface{}{
"name": s,
"error": "A column name must not start with '_'.",
return NewError2(TypeError, "A column name must not start with '_'.", map[string]interface{}{
"name": s,
})
}
loadable := true
Expand Down Expand Up @@ -433,10 +416,9 @@ func (cf *ColumnField) checkColumn() error {
// parseColumnOptions parses options of a column.
func (cf *ColumnField) parseColumnOptions(options []string) error {
if len(options) > 2 {
return NewError(TypeError, map[string]interface{}{
return NewError2(TypeError, "The tag must not contain more than 2 options.", map[string]interface{}{
"name": cf.Name,
"options": options,
"error": "The tag must not contain more than 2 options.",
})
}
if len(options) > 0 {
Expand Down Expand Up @@ -468,10 +450,9 @@ func (cf *ColumnField) parseOptions(options []string) error {
func newColumnField(field *reflect.StructField, index int) (*ColumnField, error) {
tag := field.Tag.Get(columnFieldTagKey)
if tag == "" {
return nil, NewError(TypeError, map[string]interface{}{
"name": field.Name,
"tag": field.Tag,
"error": "The struct field must have a non-empty " + columnFieldTagKey + " tag.",
return nil, NewError2(TypeError, "The struct field must have a non-empty "+columnFieldTagKey+" tag.", map[string]interface{}{
"name": field.Name,
"tag": field.Tag,
})
}
values := strings.Split(tag, columnFieldTagDelim)
Expand Down Expand Up @@ -508,9 +489,8 @@ Loop:
case reflect.Struct:
break Loop
default:
return nil, NewError(TypeError, map[string]interface{}{
"type": reflect.TypeOf(v).Name(),
"error": "The type is not supported as a row struct.",
return nil, NewError2(TypeError, "The type is not supported as a row struct.", map[string]interface{}{
"type": reflect.TypeOf(v).Name(),
})
}
}
Expand Down Expand Up @@ -539,9 +519,8 @@ Loop:
cfs = append(cfs, cf)
}
if _, ok := cfsByName[cf.Name]; ok {
return nil, NewError(TypeError, map[string]interface{}{
"name": cf.Name,
"error": "The name appears more than once.",
return nil, NewError2(TypeError, "The name appears more than once.", map[string]interface{}{
"name": cf.Name,
})
}
cfsByName[cf.Name] = cf
Expand Down

0 comments on commit 16a7245

Please sign in to comment.