Skip to content

Commit

Permalink
Added some auto-cleverness to SetFlex
Browse files Browse the repository at this point in the history
  • Loading branch information
lkarlslund committed Dec 22, 2021
1 parent 8975ef0 commit 50e128f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
8 changes: 8 additions & 0 deletions modules/engine/attributevalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ func (nv NoValues) Len() int {
return 0
}

func AttributeValueSliceFromStrings(values []string) AttributeValueSlice {
var result AttributeValueSlice
for _, av := range values {
result = append(result, AttributeValueString(av))
}
return result
}

type AttributeValueSlice []AttributeValue

func (avs AttributeValueSlice) Slice() []AttributeValue {
Expand Down
46 changes: 46 additions & 0 deletions modules/engine/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,49 @@ func (o *Object) SetFlex(flexinit ...interface{}) {
continue
}
switch v := i.(type) {
case *[]string:
if v == nil {
continue
}
if ignoreblanks && len(*v) == 0 {
continue
}
for _, s := range *v {
data = append(data, AttributeValueString(s))
}
case *string:
if v == nil {
continue
}
if ignoreblanks && len(*v) == 0 {
continue
}
data = append(data, AttributeValueString(*v))
case string:
if ignoreblanks && len(v) == 0 {
continue
}
data = append(data, AttributeValueString(v))
case *time.Time:
if v == nil {
continue
}
if ignoreblanks && v.IsZero() {
continue
}
data = append(data, AttributeValueTime(*v))
case time.Time:
if ignoreblanks && v.IsZero() {
continue
}
data = append(data, AttributeValueTime(v))
case *bool:
if v == nil {
continue
}
data = append(data, AttributeValueBool(*v))
case bool:
data = append(data, AttributeValueBool(v))
case Attribute:
if attribute != 0 && (!ignoreblanks || len(data) > 0) {
o.set(attribute, data)
Expand All @@ -592,6 +635,9 @@ func (o *Object) SetFlex(flexinit ...interface{}) {
if v == nil {
panic("This is impossble")
}
if dt, ok := v.Raw().(time.Time); ok && dt.IsZero() {
continue
}
if v.String() == "" && ignoreblanks {
continue
}
Expand Down

0 comments on commit 50e128f

Please sign in to comment.