Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a check to be sure we do not have the same tag in our model #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 23 additions & 5 deletions cqlr.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ func (b *Binding) bind(q *gocql.QueryInfo) ([]interface{}, error) {
return values, nil
}

func (b *Binding) addStrategy(tag string, value reflect.Value) error {
if _, ok := b.strategy[tag]; ok {
return ErrMultipleFieldsWithTheSameTag
}

b.strategy[tag] = value
return nil
}

func (b *Binding) compile(v reflect.Value, cols []gocql.ColumnInfo) error {

indirect := reflect.Indirect(v)
Expand All @@ -161,7 +170,9 @@ func (b *Binding) compile(v reflect.Value, cols []gocql.ColumnInfo) error {
f := s.Field(i)
tag := f.Tag.Get("cql")
if tag != "" {
b.strategy[tag] = indirect.Field(i)
if err := b.addStrategy(tag, indirect.Field(i)); err != nil {
return err
}
} else {
b.fieldMap[strings.ToLower(f.Name)] = f.Index
}
Expand All @@ -171,7 +182,9 @@ func (b *Binding) compile(v reflect.Value, cols []gocql.ColumnInfo) error {
for _, col := range cols {
staticField, ok := b.fun(col)
if ok {
b.strategy[col.Name] = indirect.FieldByIndex(staticField.Index)
if err := b.addStrategy(col.Name, indirect.FieldByIndex(staticField.Index)); err != nil {
return err
}
}
}
}
Expand All @@ -181,7 +194,9 @@ func (b *Binding) compile(v reflect.Value, cols []gocql.ColumnInfo) error {
fieldName, ok := b.typeMap[col.Name]
if ok {
f := indirect.FieldByName(fieldName)
b.strategy[col.Name] = f
if err := b.addStrategy(col.Name, f); err != nil {
return err
}
}
}
}
Expand All @@ -197,7 +212,9 @@ func (b *Binding) compile(v reflect.Value, cols []gocql.ColumnInfo) error {
if ok {
f := indirect.FieldByIndex(index)
if f.IsValid() {
b.strategy[col.Name] = f
if err := b.addStrategy(col.Name, f); err != nil {
return err
}
}
}
}
Expand All @@ -215,5 +232,6 @@ func (b *Binding) compile(v reflect.Value, cols []gocql.ColumnInfo) error {
}

var (
ErrMissingStrategy = errors.New("insufficient column mapping")
ErrMissingStrategy = errors.New("insufficient column mapping")
ErrMultipleFieldsWithTheSameTag = errors.New("multiple fields with the same tag")
)
18 changes: 18 additions & 0 deletions cqlr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,24 @@ func TestNoCaseColumns(t *testing.T) {

}

func TestDoubleTag(t *testing.T) {
type DoubleTag struct {
Id gocql.UUID `json:"id" cql:"key"`
Text string `json:"id" cql:"key"`
}
s := setup(t, "key_value")
defer s.Close()

q := s.Query(`SELECT key, value FROM key_value LIMIT 1`)
b := BindQuery(q)

var read DoubleTag

b.Scan(&read)
err := b.Close()
assert.Equal(t, ErrMultipleFieldsWithTheSameTag, err)
}

func TestUUID(t *testing.T) {
type KeyValue struct {
Id gocql.UUID `json:"id" cql:"key"`
Expand Down