github.com/mem/u-root@v2.0.1-0.20181004165302-9b18b4636a33+incompatible/cmds/elvish/eval/value.go (about) 1 package eval 2 3 import ( 4 "fmt" 5 6 "github.com/u-root/u-root/cmds/elvish/eval/vals" 7 ) 8 9 // Callable wraps the Call method. 10 type Callable interface { 11 // Call calls the receiver in a Frame with arguments and options. 12 Call(fm *Frame, args []interface{}, opts map[string]interface{}) error 13 } 14 15 var ( 16 // NoArgs is an empty argument list. It can be used as an argument to Call. 17 NoArgs = []interface{}{} 18 // NoOpts is an empty option map. It can be used as an argument to Call. 19 NoOpts = map[string]interface{}{} 20 ) 21 22 // FromJSONInterface converts a interface{} that results from json.Unmarshal to 23 // a Value. 24 func FromJSONInterface(v interface{}) interface{} { 25 if v == nil { 26 // TODO Use a more appropriate type 27 return "" 28 } 29 switch v := v.(type) { 30 case bool, string: 31 return v 32 case float64: 33 // TODO Use a numeric type for float64 34 return fmt.Sprint(v) 35 case []interface{}: 36 vs := make([]interface{}, len(v)) 37 for i, v := range v { 38 vs[i] = FromJSONInterface(v) 39 } 40 return vals.MakeList(vs...) 41 case map[string]interface{}: 42 m := vals.EmptyMap 43 for key, val := range v { 44 m = m.Assoc(key, FromJSONInterface(val)) 45 } 46 return m 47 default: 48 throw(fmt.Errorf("unexpected json type: %T", v)) 49 return nil // not reached 50 } 51 }