1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/stdlib/objects/merge.go
Tim Voronov 1af8b37a0f
New type system (#232)
* New type system

* Fixed dot notation for HTML elements
2019-02-13 12:31:18 -05:00

67 lines
1.4 KiB
Go

package objects
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
// Merge merge the given objects into a single object.
// @params objs (Array Of Object OR Objects) - objects to merge.
// @returns (Object) - Object created by merging.
func Merge(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, core.MaxArgs)
if err != nil {
return values.None, err
}
objs := values.NewArrayWith(args...)
if len(args) == 1 && args[0].Type().Equals(types.Array) {
objs = args[0].(*values.Array)
}
err = validateArrayOf(types.Object, objs)
if err != nil {
return values.None, err
}
return mergeArray(objs), nil
}
func mergeArray(arr *values.Array) *values.Object {
merged, obj := values.NewObject(), values.NewObject()
arr.ForEach(func(arrValue core.Value, arrIdx int) bool {
obj = arrValue.(*values.Object)
obj.ForEach(func(objValue core.Value, objKey string) bool {
cloneable, ok := objValue.(core.Cloneable)
if ok {
objValue = cloneable.Clone()
}
merged.Set(values.NewString(objKey), objValue)
return true
})
return true
})
return merged
}
func validateArrayOf(typ core.Type, arr *values.Array) (err error) {
for idx := values.NewInt(0); idx < arr.Length(); idx++ {
if err != nil {
break
}
err = core.ValidateType(arr.Get(idx), typ)
}
return
}