Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Expose some of the internals of Modifier too
Browse files Browse the repository at this point in the history
  • Loading branch information
suhailpatel committed Nov 17, 2020
1 parent 16d76a4 commit b4a0328
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
6 changes: 3 additions & 3 deletions mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ func assignRecords(m map[string]interface{}, record map[string]interface{}) erro
switch v := v.(type) {
case Modifier:
switch v.op {
case modifierMapSetField:
case ModifierMapSetField:
// Go interfaces are internally represented as a type and a value. The record[k] interface{} value could look like one of these:
// [type, value]
// [type, nil ]
Expand Down Expand Up @@ -851,7 +851,7 @@ func assignRecords(m map[string]interface{}, record map[string]interface{}) erro
targetMap.SetMapIndex(key, value)

record[k] = targetMap.Interface()
case modifierMapSetFields:
case ModifierMapSetFields:
// Go interfaces are internally represented as a type and a value. The record[k] interface{} value could look like one of these:
// [type, value]
// [type, nil ]
Expand Down Expand Up @@ -889,7 +889,7 @@ func assignRecords(m map[string]interface{}, record map[string]interface{}) erro
targetMap.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
record[k] = targetMap.Interface()
case modifierCounterIncrement:
case ModifierCounterIncrement:
oldV, _ := record[k].(int64)
delta := int64(v.args[0].(int))

Expand Down
75 changes: 53 additions & 22 deletions modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,96 @@ import (

// Modifiers are used with update statements.

// ModifierOp represents a modifier operand
type ModifierOp int

const (
modifierListPrepend = iota
modifierListAppend
modifierListSetAtIndex
modifierListRemove
modifierMapSetFields
modifierMapSetField
modifierCounterIncrement
// These modifier types represent the field modification operations
// on list/map types such as append/remove/map set to be used with
// UPDATE CQL statements
ModifierListPrepend ModifierOp = iota // prepend to beginning of a list
ModifierListAppend // append to the end of a list
ModifierListSetAtIndex // set a value for a specific list index
ModifierListRemove // remove an item from the list
ModifierMapSetFields // set values from the provided map
ModifierMapSetField // update a value for a specific key
ModifierCounterIncrement // increment a counter
)

type Modifier struct {
op int
op ModifierOp
args []interface{}
}

// Operation returns the operation this modifier represents
func (m Modifier) Operation() ModifierOp {
return m.op
}

// Args provides the arguments for this operation when generating the CQL statement,
// the actual arguments will depend on the Operation that this modifier represents
// - ModifierListPrepend returns 1 element with the value (interface{})
// to be prepended
// - ModifierListAppend returns 1 element with the value (interface{})
// to be appended
// - ModifierListSetAtIndex returns two elements, the index (int) and
// value (interface{}) to be set
// - ModifierListRemove returns 1 element with the value (interface{})
// to be removed
// - ModifierMapSetFields returns 1 element with a map (map[string]interface{})
// with the keys and values to be set
// - MapSetField returns 2 elements, the key (string) and value (interface{})
// to be set in the underlying map
// - ModifierCounterIncrement returns 1 element (int) with how much the value
// should be incremented by (or decremented if the value is negative)
func (m Modifier) Args() []interface{} {
return m.args
}

// ListPrepend prepends a value to the front of the list
func ListPrepend(value interface{}) Modifier {
return Modifier{
op: modifierListPrepend,
op: ModifierListPrepend,
args: []interface{}{value},
}
}

// ListAppend appends a value to the end of the list
func ListAppend(value interface{}) Modifier {
return Modifier{
op: modifierListAppend,
op: ModifierListAppend,
args: []interface{}{value},
}
}

// ListSetAtIndex sets the list element at a given index to a given value
func ListSetAtIndex(index int, value interface{}) Modifier {
return Modifier{
op: modifierListSetAtIndex,
op: ModifierListSetAtIndex,
args: []interface{}{index, value},
}
}

// ListRemove removes all elements from a list having a particular value
func ListRemove(value interface{}) Modifier {
return Modifier{
op: modifierListRemove,
op: ModifierListRemove,
args: []interface{}{value},
}
}

// MapSetFields updates the map with keys and values in the given map
func MapSetFields(fields map[string]interface{}) Modifier {
return Modifier{
op: modifierMapSetFields,
op: ModifierMapSetFields,
args: []interface{}{fields},
}
}

// MapSetField updates the map with the given key and value
func MapSetField(key, value interface{}) Modifier {
return Modifier{
op: modifierMapSetField,
op: ModifierMapSetField,
args: []interface{}{key, value},
}
}
Expand All @@ -74,7 +105,7 @@ func MapSetField(key, value interface{}) Modifier {
// Negative value results in decrementing.
func CounterIncrement(value int) Modifier {
return Modifier{
op: modifierCounterIncrement,
op: ModifierCounterIncrement,
args: []interface{}{value},
}
}
Expand All @@ -83,19 +114,19 @@ func (m Modifier) cql(name string) (string, []interface{}) {
str := ""
vals := []interface{}{}
switch m.op {
case modifierListPrepend:
case ModifierListPrepend:
str = fmt.Sprintf("%s = ? + %s", name, name)
vals = append(vals, []interface{}{m.args[0]})
case modifierListAppend:
case ModifierListAppend:
str = fmt.Sprintf("%s = %s + ?", name, name)
vals = append(vals, []interface{}{m.args[0]})
case modifierListSetAtIndex:
case ModifierListSetAtIndex:
str = fmt.Sprintf("%s[?] = ?", name)
vals = append(vals, m.args[0], m.args[1])
case modifierListRemove:
case ModifierListRemove:
str = fmt.Sprintf("%s = %s - ?", name, name)
vals = append(vals, []interface{}{m.args[0]})
case modifierMapSetFields:
case ModifierMapSetFields:
fields, ok := m.args[0].(map[string]interface{})
if !ok {
panic(fmt.Sprintf("Argument for MapSetFields is not a map: %v", m.args[0]))
Expand All @@ -115,10 +146,10 @@ func (m Modifier) cql(name string) (string, []interface{}) {
i++
}
str = buf.String()
case modifierMapSetField:
case ModifierMapSetField:
str = fmt.Sprintf("%s[?] = ?", name)
vals = append(vals, m.args[0], m.args[1])
case modifierCounterIncrement:
case ModifierCounterIncrement:
val := m.args[0].(int)
if val > 0 {
str = fmt.Sprintf("%s = %s + ?", name, name)
Expand Down

0 comments on commit b4a0328

Please sign in to comment.