本文整理汇总了Golang中github.com/robertkrimen/otto.Value.Export方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Export方法的具体用法?Golang Value.Export怎么用?Golang Value.Export使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/robertkrimen/otto.Value
的用法示例。
在下文中一共展示了Value.Export方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: convertValToMap
func convertValToMap(v *otto.Value) map[string]interface{} {
if v.IsDefined() && v.IsObject() {
options, _ := v.Export()
return options.(map[string]interface{})
}
return make(map[string]interface{})
}
示例2: findNode
// find the node from the based ont the hash passed in
// the hash needs to at least have a {name: }property
func (js *JavascriptBuilder) findNode(token string, in otto.Value) (n Node, err error) {
var (
givenOptions map[string]interface{}
ok bool
name string
)
e, err := in.Export()
if err != nil {
return n, err
}
// accept both a json hash and a string as an argument.
// if the arg is a hash, then we should extract the name,
// and pull the node from the yaml, and then merge the given options
// over top of the options presented in the config node.
//
// if the arg is a string, then use that string as the name
// and pull the config node
switch arg := e.(type) {
case map[string]interface{}:
givenOptions = arg
if name, ok = givenOptions["name"].(string); ok {
// merge the two maps
tmpMap := make(map[string]interface{})
for k, v := range js.config.Nodes[name] {
tmpMap[k] = v
}
for k, v := range givenOptions {
tmpMap[k] = v
}
givenOptions = tmpMap
} else { // we don't have a name, so lets generate one.
u, err := uuid.NewV4()
if err != nil {
return n, fmt.Errorf("%s error. unable to create uuid (%s)", token, err.Error())
}
name = u.String()
givenOptions["name"] = name
}
case string:
name = arg
givenOptions, ok = js.config.Nodes[name]
if !ok {
return n, fmt.Errorf("%s error. unable to find node '%s'", token, name)
}
}
if token == "transform" {
// this is a little bit of magic so that transformers (which are added by the transform fn get the right kind)
givenOptions["type"] = "transformer"
}
kind, ok := givenOptions["type"].(string)
if !ok {
return n, fmt.Errorf("%s: hash requires a type field, but no type given", token)
}
return NewNode(name, kind, givenOptions)
}
示例3: GetBool
//GetBool gets bool from otto value
func GetBool(value otto.Value) (bool, error) {
rawBool, _ := value.Export()
result, ok := rawBool.(bool)
if !ok {
return false, fmt.Errorf(wrongArgumentType, rawBool, "bool")
}
return result, nil
}
示例4: GetTransaction
//GetTransaction gets Transaction from otto value
func GetTransaction(value otto.Value) (transaction.Transaction, error) {
rawTransaction, _ := value.Export()
result, ok := rawTransaction.(transaction.Transaction)
if !ok {
return nil, fmt.Errorf(wrongArgumentType, rawTransaction, "Transaction")
}
return result, nil
}
示例5: GetAuthorization
//GetAuthorization gets Transaction from otto value
func GetAuthorization(value otto.Value) (schema.Authorization, error) {
rawAuthorization, _ := value.Export()
result, ok := rawAuthorization.(schema.Authorization)
if !ok {
return nil, fmt.Errorf(wrongArgumentType, rawAuthorization, "Authorization")
}
return result, nil
}
示例6: GetString
//GetString gets string from otto value
func GetString(value otto.Value) (string, error) {
rawString, _ := value.Export()
result, ok := rawString.(string)
if !ok {
return "", fmt.Errorf(wrongArgumentType, rawString, "string")
}
return result, nil
}
示例7: GetStringList
//GetStringList gets []string from otto value
func GetStringList(value otto.Value) ([]string, error) {
rawSlice, _ := value.Export()
rawResult, ok := rawSlice.([]string)
if !ok {
return make([]string, 0), fmt.Errorf(wrongArgumentType, rawSlice, "array of strings")
}
return rawResult, nil
}
示例8: Filter
func (f *JSFilter) Filter(ev *message.Event) (*message.Event, error) {
var dropped bool
f.vm.Set("$", map[string]interface{}{
"env": f.conf.Env,
"event": map[string]interface{}{
"tag": ev.Tag,
"time": ev.Time,
"record": ev.Record,
},
"drop": func(call otto.FunctionCall) otto.Value {
dropped = true
return otto.Value{}
},
"emit": func(call otto.FunctionCall) (ret otto.Value) {
var record otto.Value
var t time.Time
tag := call.Argument(0).String()
if !call.Argument(2).IsDefined() {
record = call.Argument(1)
t = time.Now()
} else {
record = call.Argument(2)
v, err := call.Argument(1).Export()
if err != nil {
f.env.Log.Warningf("Failed to get time: %v", err)
return
}
var ok bool
t, ok = v.(time.Time)
if !ok {
f.env.Log.Warningf("Failed to get time: unsupported type %T", v)
return
}
}
rec, err := record.Export()
if err != nil {
f.env.Log.Warningf("Failed to get record: %v", err)
return
}
typedRec, ok := rec.(map[string]interface{})
if !ok {
f.env.Log.Warningf("Failed to get record: Unsupported type %T", rec)
return
}
f.env.Emit(message.NewEventWithTime(tag, t, typedRec))
return
},
})
_, err := f.vm.Run(f.script)
if err != nil {
return nil, err
} else if dropped {
return nil, nil
}
return ev, nil
}
示例9: GetMap
//GetMap gets map[string]interface{} from otto value
func GetMap(value otto.Value) (map[string]interface{}, error) {
rawMap, _ := value.Export()
result, ok := rawMap.(map[string]interface{})
if !ok {
return map[string]interface{}{}, fmt.Errorf(wrongArgumentType, rawMap, "Object")
}
for key, value := range result {
result[key] = ConvertOttoToGo(value)
}
return result, nil
}
示例10: ottoValueToStringArray
// Converts a JS string or array into a Go string array.
func ottoValueToStringArray(value otto.Value) []string {
nativeValue, _ := value.Export()
result := base.ValueToStringArray(nativeValue)
if result == nil && !value.IsNull() && !value.IsUndefined() {
base.Warn("SyncRunner: Non-string, non-array passed to JS callback: %s", value)
}
return result
}
示例11: GetList
//GetList gets []interface{} from otto value
func GetList(value otto.Value) ([]interface{}, error) {
rawSlice, err := value.Export()
result := make([]interface{}, 0)
if rawSlice == nil || err != nil {
return result, err
}
typeOfSlice := reflect.TypeOf(rawSlice)
if typeOfSlice.Kind() != reflect.Array && typeOfSlice.Kind() != reflect.Slice {
return result, fmt.Errorf(wrongArgumentType, value, "array")
}
list := reflect.ValueOf(rawSlice)
for i := 0; i < list.Len(); i++ {
result = append(result, ConvertOttoToGo(list.Index(i).Interface()))
}
return result, err
}
示例12: ottoValueToStringArray
func ottoValueToStringArray(value otto.Value) []string {
{
value, _ := value.Export()
switch value := value.(type) {
case string:
return []string{value}
case []interface{}:
result := make([]string, 0, len(value))
for _, item := range value {
if value, ok := item.(string); ok {
result = append(result, value)
}
}
return result
}
}
return nil
}
示例13: objToHeader
func objToHeader(data *otto.Value, header http.Header) {
dataMap, _ := data.Export()
for key, val := range dataMap.(map[string]interface{}) {
switch val.(type) {
case string:
{
header.Add(key, val.(string))
break
}
case []interface{}:
{
for _, headerVal := range val.([]interface{}) {
header.Add(key, headerVal.(string))
}
break
}
}
}
}
示例14: buildIteratorFromValue
func buildIteratorFromValue(val otto.Value, ts graph.TripleStore) graph.Iterator {
if val.IsNull() || val.IsUndefined() {
return ts.GetNodesAllIterator()
}
if val.IsPrimitive() {
thing, _ := val.Export()
switch v := thing.(type) {
case string:
it := ts.MakeFixed()
it.AddValue(ts.GetIdFor(v))
return it
default:
glog.Errorln("Trying to build unknown primitive value.")
}
}
switch val.Class() {
case "Object":
return buildIteratorTree(val.Object(), ts)
case "Array":
// Had better be an array of strings
strings := makeListOfStringsFromArrayValue(val.Object())
it := ts.MakeFixed()
for _, x := range strings {
it.AddValue(ts.GetIdFor(x))
}
return it
case "Number":
fallthrough
case "Boolean":
fallthrough
case "Date":
fallthrough
case "String":
it := ts.MakeFixed()
str, _ := val.ToString()
it.AddValue(ts.GetIdFor(str))
return it
default:
glog.Errorln("Trying to handle unsupported Javascript value.")
return graph.NewNullIterator()
}
}
示例15: GetStringList
//GetStringList gets []string from otto value
func GetStringList(value otto.Value) ([]string, error) {
var ok bool
var rawSlice []interface{}
var stringSlice []string
rawData, _ := value.Export()
rawSlice, ok = rawData.([]interface{})
if ok && len(rawSlice) == 0 {
return []string{}, nil
}
stringSlice, ok = rawData.([]string)
if !ok {
return make([]string, 0), fmt.Errorf(wrongArgumentType, rawData, "array of strings")
}
return stringSlice, nil
}