mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 08:50:46 +00:00
Move out repository models; Hidrate collection on create
This commit is contained in:
32
internal/struct_hidrators/collection_defaults.go
Normal file
32
internal/struct_hidrators/collection_defaults.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package structhidrators
|
||||
|
||||
import (
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
var defaultCollection repositorymodels.Collection = repositorymodels.Collection{
|
||||
IndexingPolicy: repositorymodels.CollectionIndexingPolicy{
|
||||
IndexingMode: "consistent",
|
||||
Automatic: true,
|
||||
IncludedPaths: []repositorymodels.CollectionIndexingPolicyPath{
|
||||
{Path: "/*"},
|
||||
},
|
||||
ExcludedPaths: []repositorymodels.CollectionIndexingPolicyPath{
|
||||
{Path: "/\"_etag\"/?"},
|
||||
},
|
||||
},
|
||||
PartitionKey: repositorymodels.CollectionPartitionKey{
|
||||
Paths: []string{"/_partitionKey"},
|
||||
Kind: "Hash",
|
||||
Version: 2,
|
||||
},
|
||||
Rid: "nFFFFFFFFFF=",
|
||||
Ts: 0,
|
||||
Self: "",
|
||||
Etag: "\"00000000-0000-0000-0000-000000000000\"",
|
||||
Docs: "docs/",
|
||||
Sprocs: "sprocs/",
|
||||
Triggers: "triggers/",
|
||||
Udfs: "udfs/",
|
||||
Conflicts: "conflicts/",
|
||||
}
|
||||
60
internal/struct_hidrators/hidrate.go
Normal file
60
internal/struct_hidrators/hidrate.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package structhidrators
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func Hidrate(input interface{}) interface{} {
|
||||
if reflect.TypeOf(input) == reflect.TypeOf(repositorymodels.Collection{}) {
|
||||
return hidrate(input, defaultCollection)
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
func hidrate(input interface{}, defaults interface{}) interface{} {
|
||||
inputVal := reflect.ValueOf(input)
|
||||
defaultsVal := reflect.ValueOf(defaults)
|
||||
|
||||
if inputVal.Kind() != reflect.Struct || defaultsVal.Kind() != reflect.Struct {
|
||||
panic("Both input and defaults must be structs")
|
||||
}
|
||||
|
||||
output := reflect.New(inputVal.Type()).Elem()
|
||||
for i := 0; i < inputVal.NumField(); i++ {
|
||||
inputField := inputVal.Field(i)
|
||||
defaultField := defaultsVal.Field(i)
|
||||
|
||||
if inputField.Kind() == reflect.Struct && defaultField.Kind() == reflect.Struct {
|
||||
filledNested := hidrate(inputField.Interface(), defaultField.Interface())
|
||||
output.Field(i).Set(reflect.ValueOf(filledNested))
|
||||
} else {
|
||||
if isEmptyValue(inputField) {
|
||||
output.Field(i).Set(defaultField)
|
||||
} else {
|
||||
output.Field(i).Set(inputField)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output.Interface()
|
||||
}
|
||||
|
||||
func isEmptyValue(v reflect.Value) bool {
|
||||
switch v.Kind() {
|
||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
||||
return v.Len() == 0
|
||||
case reflect.Bool:
|
||||
return !v.Bool()
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return v.Int() == 0
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return v.Uint() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return v.Float() == 0
|
||||
case reflect.Interface, reflect.Ptr:
|
||||
return v.IsNil()
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user