Skip to content

Commit

Permalink
feat: Add support for lwwr scalar arrays (full replace on update) (so…
Browse files Browse the repository at this point in the history
…urcenetwork#115)

* Handle explicit nil values

* Remove unwanted print lines

* Add support for inline scalar arrays
  • Loading branch information
AndrewSisley authored Jan 23, 2022
1 parent bd01c5e commit 04b175b
Show file tree
Hide file tree
Showing 11 changed files with 1,180 additions and 9 deletions.
4 changes: 4 additions & 0 deletions db/base/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,16 @@ const (
FieldKind_None FieldKind = iota
FieldKind_DocKey
FieldKind_BOOL
FieldKind_BOOL_ARRAY
FieldKind_INT
FieldKind_INT_ARRAY
FieldKind_FLOAT
FieldKind_FLOAT_ARRAY
FieldKind_DECIMNAL
FieldKind_DATE
FieldKind_TIMESTAMP
FieldKind_STRING
FieldKind_STRING_ARRAY
FieldKind_BYTES
FieldKind_OBJECT // Embedded object within the type
FieldKind_OBJECT_ARRAY // Array of embedded objects
Expand Down
73 changes: 70 additions & 3 deletions db/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,30 +436,97 @@ func validateFieldSchema(val interface{}, field base.FieldDescription) (interfac
switch field.Kind {
case base.FieldKind_DocKey, base.FieldKind_STRING:
cval, ok = val.(string)
case base.FieldKind_STRING_ARRAY:
if val == nil {
ok = true
cval = nil
break
}
untypedCollection := val.([]interface{})
stringArray := make([]string, len(untypedCollection))
for i, value := range untypedCollection {
if value == nil {
stringArray[i] = ""
continue
}
stringArray[i], ok = value.(string)
if !ok {
return nil, fmt.Errorf("Failed to cast value: %v of type: %T to string", value, value)
}
}
ok = true
cval = stringArray
case base.FieldKind_BOOL:
cval, ok = val.(bool)
case base.FieldKind_BOOL_ARRAY:
if val == nil {
ok = true
cval = nil
break
}
untypedCollection := val.([]interface{})
boolArray := make([]bool, len(untypedCollection))
for i, value := range untypedCollection {
boolArray[i], ok = value.(bool)
if !ok {
return nil, fmt.Errorf("Failed to cast value: %v of type: %T to bool", value, value)
}
}
ok = true
cval = boolArray
case base.FieldKind_FLOAT, base.FieldKind_DECIMNAL:
cval, ok = val.(float64)
case base.FieldKind_FLOAT_ARRAY:
if val == nil {
ok = true
cval = nil
break
}
untypedCollection := val.([]interface{})
floatArray := make([]float64, len(untypedCollection))
for i, value := range untypedCollection {
floatArray[i], ok = value.(float64)
if !ok {
return nil, fmt.Errorf("Failed to cast value: %v of type: %T to float64", value, value)
}
}
ok = true
cval = floatArray

case base.FieldKind_DATE:
var sval string
sval, ok = val.(string)
cval, err = time.Parse(time.RFC3339, sval)
case base.FieldKind_INT:
fmt.Printf("encountered val type: %v\n", val)
var fval float64
fval, ok = val.(float64)
if !ok {
fmt.Println("error1")
return nil, ErrInvalidMergeValueType
}
cval = int64(fval)
case base.FieldKind_INT_ARRAY:
if val == nil {
ok = true
cval = nil
break
}
untypedCollection := val.([]interface{})
intArray := make([]int64, len(untypedCollection))
for i, value := range untypedCollection {
valueAsFloat, castOk := value.(float64)
if !castOk {
return nil, fmt.Errorf("Failed to cast value: %v of type: %T to float64", value, value)
}
intArray[i] = int64(valueAsFloat)
}
ok = true
cval = intArray
case base.FieldKind_OBJECT, base.FieldKind_OBJECT_ARRAY,
base.FieldKind_FOREIGN_OBJECT, base.FieldKind_FOREIGN_OBJECT_ARRAY:
err = errors.New("Merge doesn't support sub types yet")
}

if !ok {
fmt.Println("error2")
return nil, ErrInvalidMergeValueType
}
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion db/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ func (df *DocumentFetcher) processKV(kv *core.KeyValue) error {
// secondary index is provided, we need to extract the indexed/implicit fields
// from the KV pair.
df.doc.Properties[fieldDesc] = &document.EncProperty{
Raw: kv.Value,
Desc: fieldDesc,
Raw: kv.Value,
}
// @todo: Extract Index implicit/stored keys
return nil
Expand Down
Loading

0 comments on commit 04b175b

Please sign in to comment.