1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/values/helpers.go

385 lines
6.7 KiB
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package values
import (
"context"
2018-10-28 07:45:26 +02:00
"encoding/binary"
2018-09-26 01:04:07 +02:00
"encoding/json"
2018-10-28 07:45:26 +02:00
"hash/fnv"
2018-09-29 03:04:16 +02:00
"reflect"
2018-10-28 07:45:26 +02:00
"sort"
"strconv"
2018-09-18 22:42:38 +02:00
"time"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
2018-09-18 22:42:38 +02:00
)
func GetIn(ctx context.Context, from core.Value, byPath []core.Value) (core.Value, error) {
2018-09-18 22:42:38 +02:00
if byPath == nil || len(byPath) == 0 {
return None, nil
}
var result = from
for i, segment := range byPath {
2018-09-18 22:42:38 +02:00
if result == None || result == nil {
break
}
segType := segment.Type()
2018-09-18 22:42:38 +02:00
switch segVal := result.(type) {
case *Object:
if segType != types.String {
return nil, core.TypeError(segType, types.String)
2018-09-18 22:42:38 +02:00
}
result, _ = segVal.Get(segment.(String))
2018-09-18 22:42:38 +02:00
break
case *Array:
if segType != types.Int {
return nil, core.TypeError(segType, types.Int)
2018-09-18 22:42:38 +02:00
}
result = segVal.Get(segment.(Int))
2018-09-18 22:42:38 +02:00
break
case core.Getter:
return segVal.GetIn(ctx, byPath[i:])
2018-09-18 22:42:38 +02:00
default:
return None, core.TypeError(
from.Type(),
types.Array,
types.Object,
core.NewType("Getter"),
2018-09-18 22:42:38 +02:00
)
}
}
return result, nil
}
func SetIn(ctx context.Context, to core.Value, byPath []core.Value, value core.Value) error {
2018-09-18 22:42:38 +02:00
if byPath == nil || len(byPath) == 0 {
return nil
}
var parent core.Value
var current = to
target := len(byPath) - 1
for idx, segment := range byPath {
parent = current
isTarget := target == idx
segmentType := segment.Type()
switch parVal := parent.(type) {
case *Object:
if segmentType != types.String {
return core.TypeError(segmentType, types.String)
2018-09-18 22:42:38 +02:00
}
if isTarget == false {
current, _ = parVal.Get(segment.(String))
2018-09-18 22:42:38 +02:00
} else {
parVal.Set(segment.(String), value)
2018-09-18 22:42:38 +02:00
}
break
case *Array:
if segmentType != types.Int {
return core.TypeError(segmentType, types.Int)
2018-09-18 22:42:38 +02:00
}
if isTarget == false {
current = parVal.Get(segment.(Int))
2018-09-18 22:42:38 +02:00
} else {
if err := parVal.Set(segment.(Int), value); err != nil {
return err
}
2018-09-18 22:42:38 +02:00
}
break
case core.Setter:
return parVal.SetIn(ctx, byPath[idx:], value)
2018-09-18 22:42:38 +02:00
default:
// redefine parent
isArray := segmentType.Equals(types.Int)
2018-09-18 22:42:38 +02:00
// it's not an index
if isArray == false {
obj := NewObject()
parent = obj
if segmentType != types.String {
return core.TypeError(segmentType, types.String)
2018-09-18 22:42:38 +02:00
}
if isTarget {
obj.Set(segment.(String), value)
}
} else {
arr := NewArray(10)
parent = arr
if isTarget {
if err := arr.Set(segment.(Int), value); err != nil {
return err
}
2018-09-18 22:42:38 +02:00
}
}
// set new parent
if err := SetIn(ctx, to, byPath[0:idx-1], parent); err != nil {
return err
}
2018-09-18 22:42:38 +02:00
if isTarget == false {
current = None
}
break
2018-09-18 22:42:38 +02:00
}
}
return nil
}
func Parse(input interface{}) core.Value {
switch value := input.(type) {
2018-09-18 22:42:38 +02:00
case bool:
return NewBoolean(value)
2018-09-18 22:42:38 +02:00
case string:
return NewString(value)
2018-09-18 22:42:38 +02:00
case int:
return NewInt(value)
2018-09-18 22:42:38 +02:00
case float64:
return NewFloat(value)
2018-09-18 22:42:38 +02:00
case float32:
return NewFloat(float64(value))
2018-09-18 22:42:38 +02:00
case time.Time:
return NewDateTime(value)
2018-09-18 22:42:38 +02:00
case []interface{}:
arr := NewArray(len(value))
2018-09-18 22:42:38 +02:00
for _, el := range value {
2018-09-18 22:42:38 +02:00
arr.Push(Parse(el))
}
return arr
case map[string]interface{}:
obj := NewObject()
for key, el := range value {
2018-09-18 22:42:38 +02:00
obj.Set(NewString(key), Parse(el))
}
return obj
case []byte:
return NewBinary(value)
2018-10-04 16:38:42 +02:00
case nil:
return None
2018-09-29 03:04:16 +02:00
default:
v := reflect.ValueOf(value)
t := reflect.TypeOf(value)
2018-09-29 03:04:16 +02:00
kind := t.Kind()
if kind == reflect.Slice || kind == reflect.Array {
size := v.Len()
arr := NewArray(size)
2018-10-05 23:01:54 +02:00
for i := 0; i < size; i++ {
curVal := v.Index(i)
arr.Push(Parse(curVal.Interface()))
2018-09-29 03:04:16 +02:00
}
return arr
}
if kind == reflect.Map {
keys := v.MapKeys()
obj := NewObject()
for _, k := range keys {
key := Parse(k.Interface())
curVal := v.MapIndex(k)
2018-09-29 03:04:16 +02:00
obj.Set(NewString(key.String()), Parse(curVal.Interface()))
2018-09-29 03:04:16 +02:00
}
return obj
}
if kind == reflect.Struct {
obj := NewObject()
size := t.NumField()
2018-10-05 23:01:54 +02:00
for i := 0; i < size; i++ {
2018-09-29 03:04:16 +02:00
field := t.Field(i)
fieldValue := v.Field(i)
2018-09-18 22:42:38 +02:00
obj.Set(NewString(field.Name), Parse(fieldValue.Interface()))
2018-09-29 03:04:16 +02:00
}
return obj
}
return None
}
2018-09-18 22:42:38 +02:00
}
2018-09-23 02:28:33 +02:00
2018-09-26 01:04:07 +02:00
func Unmarshal(value json.RawMessage) (core.Value, error) {
var o interface{}
err := json.Unmarshal(value, &o)
if err != nil {
return None, err
}
return Parse(o), nil
}
2018-09-23 02:28:33 +02:00
func ToBoolean(input core.Value) core.Value {
switch input.Type() {
case types.Boolean:
2018-09-23 02:28:33 +02:00
return input
case types.None:
2018-09-23 02:28:33 +02:00
return False
case types.String:
2018-09-23 02:28:33 +02:00
return NewBoolean(input.String() != "")
case types.Int:
2018-09-23 02:28:33 +02:00
return NewBoolean(input.(Int) != 0)
case types.Float:
2018-09-23 02:28:33 +02:00
return NewBoolean(input.(Float) != 0)
default:
return True
}
}
func ToFloat(input core.Value) (Float, error) {
switch val := input.(type) {
case Float:
return val, nil
case Int:
return Float(val), nil
case String:
i, err := strconv.ParseFloat(string(val), 64)
if err != nil {
return ZeroFloat, err
}
return Float(i), nil
default:
return ZeroFloat, core.TypeError(input.Type(), types.Int, types.Float, types.String)
}
}
func ToInt(input core.Value) (Int, error) {
switch val := input.(type) {
case Int:
return val, nil
case Float:
return Int(val), nil
case String:
i, err := strconv.ParseInt(string(val), 10, 64)
if err != nil {
return ZeroInt, err
}
return Int(i), nil
default:
return ZeroInt, core.TypeError(input.Type(), types.Int, types.Float, types.String)
}
}
func ToArray(ctx context.Context, input core.Value) (core.Value, error) {
switch value := input.(type) {
case Boolean,
Int,
Float,
String,
DateTime:
return NewArrayWith(value), nil
case *Array:
return value.Copy(), nil
case *Object:
arr := NewArray(int(value.Length()))
value.ForEach(func(value core.Value, key string) bool {
arr.Push(value)
return true
})
return arr, nil
case core.Iterable:
iterator, err := value.Iterate(ctx)
if err != nil {
return None, err
}
arr := NewArray(10)
for {
val, _, err := iterator.Next(ctx)
if err != nil {
return None, err
}
if val == None {
break
}
arr.Push(val)
}
return arr, nil
default:
return NewArray(0), nil
}
}
2018-10-28 07:45:26 +02:00
func MapHash(input map[string]core.Value) uint64 {
h := fnv.New64a()
keys := make([]string, 0, len(input))
for key := range input {
keys = append(keys, key)
}
// order does not really matter
// but it will give us a consistent hash sum
sort.Strings(keys)
endIndex := len(keys) - 1
h.Write([]byte("{"))
for idx, key := range keys {
h.Write([]byte(key))
h.Write([]byte(":"))
el := input[key]
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, el.Hash())
h.Write(bytes)
if idx != endIndex {
h.Write([]byte(","))
}
}
h.Write([]byte("}"))
return h.Sum64()
}