Skip to content

Commit

Permalink
internal/structfilter: cleanup splitColumnOperator
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 8, 2019
1 parent b481462 commit 9070859
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions internal/structfilter/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ func newField(sf reflect.StructField) *Field {
panic(err)
}

filterName := filterName(f.name)
if f.IsSlice {
f.Column, f.opCode, f.OpValue = splitSliceColumnOperator(f.name)
f.Column, f.opCode, f.OpValue = splitSliceColumnOperator(filterName)
f.Scan = arrayScanner(sf.Type)
f.Append = types.ArrayAppender(sf.Type)
} else {
f.Column, f.opCode, f.OpValue = splitColumnOperator(f.name, "_")
f.Column, f.opCode, f.OpValue = splitColumnOperator(filterName)
f.Scan = scanner(sf.Type)
f.Append = types.Appender(sf.Type)
}
Expand All @@ -109,9 +110,10 @@ func (f *Field) Omit(value reflect.Value) bool {
return !f.required && f.noWhere || f.isZero(value)
}

func splitColumnOperator(s, sep string) (string, opCode, string) {
s = internal.Underscore(s)
ind := strings.LastIndex(s, sep)
func splitColumnOperator(s string) (string, opCode, string) {
const sep = "__"

ind := strings.Index(s, sep)
if ind == -1 {
return s, opCodeEq, opEq
}
Expand Down Expand Up @@ -142,14 +144,15 @@ func splitColumnOperator(s, sep string) (string, opCode, string) {
}

func splitSliceColumnOperator(s string) (string, opCode, string) {
s = internal.Underscore(s)
ind := strings.LastIndexByte(s, '_')
const sep = "__"

ind := strings.Index(s, sep)
if ind == -1 {
return s, opCodeEq, opAny
}

col := s[:ind]
op := s[ind+1:]
op := s[ind+len(sep):]

switch op {
case "eq", "":
Expand All @@ -160,3 +163,9 @@ func splitSliceColumnOperator(s string) (string, opCode, string) {
return s, opCodeEq, opAny
}
}

func filterName(s string) string {
s = internal.Underscore(s)
s = strings.ReplaceAll(s, "_", "__")
return s
}
2 changes: 1 addition & 1 deletion internal/structfilter/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewStruct(typ reflect.Type) *Struct {
}

func (s *Struct) Field(name string) *Field {
col, opCode, _ := splitColumnOperator(name, "__")
col, opCode, _ := splitColumnOperator(name)
for _, f := range s.Fields {
if f.Column == col && f.opCode == opCode {
return f
Expand Down

0 comments on commit 9070859

Please sign in to comment.