Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions schema/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,7 @@ func (t *Table) ValidateDuplicateColumns() error {

func (t *Table) ValidateColumnNames() error {
for _, c := range t.Columns {
ok := reValidColumnName.MatchString(c.Name)
if !ok {
if !ValidColumnName(c.Name) {
return fmt.Errorf("column name %q on table %q is not valid: column names must contain only lower-case letters, numbers and underscores, and must start with a lower-case letter or underscore", c.Name, t.Name)
}
}
Expand Down Expand Up @@ -430,3 +429,7 @@ func (t *Table) Copy(parent *Table) *Table {
}
return &c
}

func ValidColumnName(name string) bool {
return reValidColumnName.MatchString(name)
}
4 changes: 3 additions & 1 deletion transformers/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,9 @@ func DefaultNameTransformer(field reflect.StructField) (string, error) {
if jsonTag == "-" {
return "", nil
}
name = jsonTag
if schema.ValidColumnName(jsonTag) {
name = jsonTag
}
}
return defaultCaser.ToSnake(name), nil
}
21 changes: 21 additions & 0 deletions transformers/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ type (
Name string `json:"name"`
Version int `json:"version"`
}

testFunnyStruct struct {
AFunnyLookingField string `json:"OS-EXT:a-funny-looking-field"`
}
)

var (
Expand Down Expand Up @@ -243,6 +247,16 @@ var (
},
},
}

expectedFunnyTable = schema.Table{
Name: "test_funny_struct",
Columns: schema.ColumnList{
{
Name: "a_funny_looking_field",
Type: schema.TypeString,
},
},
}
)

func TestTableFromGoStruct(t *testing.T) {
Expand Down Expand Up @@ -356,6 +370,13 @@ func TestTableFromGoStruct(t *testing.T) {
want: expectedTableWithPKs,
wantErr: true,
},
{
name: "Should properly transform structs with funny looking fields",
args: args{
testStruct: testFunnyStruct{},
},
want: expectedFunnyTable,
},
}

for _, tt := range tests {
Expand Down