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

feat: Add support for lwwr scalar arrays (full replace on update) #115

Merged
merged 3 commits into from
Jan 23, 2022
Merged
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
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
AndrewSisley marked this conversation as resolved.
Show resolved Hide resolved
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