replaced DynamicList with a more generic (model) helper to allow creating pointer slice of any type

This commit is contained in:
Gani Georgiev
2023-06-25 20:19:01 +03:00
parent 39accdba58
commit 051b3702b0
4 changed files with 5315 additions and 5248 deletions

View File

@@ -67,6 +67,14 @@ func baseBinds(vm *goja.Runtime) {
}
`)
vm.Set("$arrayOf", func(model any) any {
mt := reflect.TypeOf(model)
st := reflect.SliceOf(mt)
elem := reflect.New(st).Elem()
return elem.Addr().Interface()
})
vm.Set("DynamicModel", func(call goja.ConstructorCall) *goja.Object {
shape, ok := call.Argument(0).Export().(map[string]any)
if !ok || len(shape) == 0 {
@@ -80,19 +88,6 @@ func baseBinds(vm *goja.Runtime) {
return instanceValue
})
vm.Set("DynamicList", func(call goja.ConstructorCall) *goja.Object {
shape, ok := call.Argument(0).Export().(map[string]any)
if !ok || len(shape) == 0 {
panic("missing shape data")
}
instance := newDynamicList(shape)
instanceValue := vm.ToValue(instance).(*goja.Object)
instanceValue.SetPrototype(call.This.Prototype())
return instanceValue
})
vm.Set("Record", func(call goja.ConstructorCall) *goja.Object {
var instance *models.Record
@@ -406,24 +401,6 @@ func filesContent(dirPath string, pattern string) (map[string][]byte, error) {
return result, nil
}
// newDynamicList creates a new dynamic slice of structs with fields based
// on the specified "shape".
//
// Example:
//
// m := newDynamicList(map[string]any{
// "title": "",
// "total": 0,
// })
func newDynamicList(shape map[string]any) any {
m := newDynamicModel(shape)
mt := reflect.TypeOf(m)
st := reflect.SliceOf(mt)
elem := reflect.New(st).Elem()
return elem.Addr().Interface()
}
// newDynamicModel creates a new dynamic struct with fields based
// on the specified "shape".
//