当前位置: 首页>>代码示例>>Golang>>正文


Golang action.Format类代码示例

本文整理汇总了Golang中github.com/crackcomm/go-actions/action.Format的典型用法代码示例。如果您正苦于以下问题:Golang Format类的具体用法?Golang Format怎么用?Golang Format使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Format类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Action

// Action - Creates and returns action.
func (cmd *Command) Action(ctx action.Map) *action.Action {
	value := action.Format{Value: cmd.IAction}
	// If value is a string it's action name
	if name, ok := value.String(); ok {
		return &action.Action{
			Name: name,
			Ctx:  ctx,
		}
	}
	// If value is a map it's action
	if m, ok := value.Map(); ok {
		// Get action name
		name, _ := m.Get("name").String()
		// Get action ctx
		if c, ok := m.Get("ctx").Map(); ok {
			c.Merge(ctx)
			ctx = c
		}
		return &action.Action{
			Name: name,
			Ctx:  ctx,
		}
	}
	return nil
}
开发者ID:crackcomm,项目名称:actions-cli,代码行数:26,代码来源:command.go

示例2: IsSelector

// IsSelector - Checks if value is a selector.
func IsSelector(value action.Format) bool {
	// If value is a string it's a selector
	if _, ok := value.String(); ok {
		return true
	}

	// Maybe it's a map ?
	m, ok := value.Map()
	if !ok {
		return false
	}

	// If we formatted this map save it
	value.Value = m

	// If map contains $path it's a selector
	if !m.Get("$path").IsNil() {
		return true
	}

	// If map contains $extract it's a selector
	if !m.Get("$extract").IsNil() {
		return true
	}

	return false
}
开发者ID:crackcomm,项目名称:go-core,代码行数:28,代码来源:selectors.go

示例3: encodeFunc

// encodeFunc - Encodes value to a JSON byte array.
func encodeFunc(val action.Format) interface{} {
	// Try to format to a map and marshal (otherwise UnsupportedTypeError could appear on interface-interface map)
	if m, ok := val.Map(); ok {
		body, _ := json.Marshal(m)
		return body
	}
	// Otherwise marshal interface
	body, _ := json.Marshal(val.Value)
	return body
}
开发者ID:crackcomm,项目名称:go-core,代码行数:11,代码来源:json.go

示例4: decodeFunc

// decodeFunc - Decodes JSON strings, readers and byte arrays.
func decodeFunc(val action.Format) interface{} {
	// Value can be a Reader
	if reader, ok := val.Reader(); ok {
		defer reader.Close()
		var res interface{}
		if err := json.NewDecoder(reader).Decode(&res); err == nil {
			return res
		}
		return reader
	}

	return val.Value
}
开发者ID:crackcomm,项目名称:go-core,代码行数:14,代码来源:json.go

示例5: ToSelector

// ToSelector - Transforms a value into a Selector.
// Accepts string and map values.
func ToSelector(value action.Format) selector.Extractor {
	// Try to get string value
	if path, ok := value.String(); ok {
		return &selector.Selector{
			Extractor: selector.TextExtractor,
			Path:      path,
		}
	}

	// Maybe it's a map...
	if m, ok := value.Map(); ok {
		return mapToSelector(m)
	}

	return nil
}
开发者ID:crackcomm,项目名称:go-core,代码行数:18,代码来源:selectors.go

示例6: toString

// toString - Formats interface to string and cuts to `MaxVariableLength`.
func toString(v interface{}) (res string) {
	value := action.Format{v}

	if m, ok := value.Map(); ok {
		body, _ := m.JSON()
		res = string(body)
	} else if b, ok := value.Bytes(); ok {
		res = string(b)
	} else {
		res = fmt.Sprintf("%v", value.Interface())
	}

	// Cut value at 100 characters
	if len(res) >= MaxVariableLength {
		res = res[:MaxVariableLength] + "..."
	}

	// Break by new line and trim every line
	var lines []string
	for _, line := range strings.Split(res, "\n") {
		lines = append(lines, strings.TrimSpace(line))
	}

	// Join to one line
	res = strings.Join(lines, "")

	return
}
开发者ID:crackcomm,项目名称:action-test,代码行数:29,代码来源:variables.go

示例7: trimFunc

// trimFunc - Runs strings.TrimSpace on strings, lists and maps.
func trimFunc(in action.Format) interface{} {
	// If it's a string just trim
	if value, ok := in.String(); ok {
		return strings.TrimSpace(value)
	}

	// If it's a list - deep trim all values
	if value, ok := in.List(); ok {
		for num, val := range value {
			if result := trimFunc(action.Format{Value: val}); result != nil {
				value[num] = result
			}
		}
		return value
	}

	// If it's a map - deep trim all values
	if value, ok := in.Map(); ok {
		value.Transform(value.Keys(), trimFunc)
		return value
	}

	// Not expected type - don't touch
	return in.Value
}
开发者ID:crackcomm,项目名称:go-core,代码行数:26,代码来源:trim.go

示例8: joinURL

// joinURL - joins query to uri
func joinURL(uri string, query action.Format) (u *url.URL, err error) {
	u, err = url.Parse(uri)
	if err != nil {
		return
	}

	if query.IsNil() {
		return
	}

	var urlQuery url.Values
	if u.RawQuery == "" {
		urlQuery = make(url.Values)
	} else {
		urlQuery, err = url.ParseQuery(u.RawQuery)
		if err != nil {
			return
		}
	}

	// If request query is map
	if q, ok := query.Map(); ok {
		urlQuery = mapToQuery(urlQuery, q)
	} else if q, ok := query.String(); ok { // or a string
		urlQuery, err = strToQuery(urlQuery, q)
		if err != nil {
			return
		}
	}

	u.RawQuery = urlQuery.Encode()

	return
}
开发者ID:crackcomm,项目名称:go-core,代码行数:35,代码来源:url.go

示例9: printValue

func printValue(value interface{}, n int) {
	switch value.(type) {
	case action.Map:
		print("\n")
		printMap(value.(action.Map), n+1)
	case []interface{}:
		print("\n")
		for _, v := range value.([]interface{}) {
			prints(n+1, "-")
			printValue(v, n+1)
		}
	case []string:
		print("\n")
		for _, v := range value.([]string) {
			prints(n+1, "- %v\n", v)
		}
	case string:
		lines := strings.Split(value.(string), "\n")
		if len(lines) <= 1 {
			print(value.(string))
			print("\n")
		} else {
			print("\n")
			for _, ln := range lines {
				prints(n+1, "%s\n", ln)
			}
		}
	default:
		f := action.Format{value}
		if v, ok := f.String(); ok {
			prints(0, "%#v\n", v)
		} else {
			prints(0, "%#v\n", value)
		}
	}
}
开发者ID:crackcomm,项目名称:cmds,代码行数:36,代码来源:main.go


注:本文中的github.com/crackcomm/go-actions/action.Format类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。