helper

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

go/template helper functions

Index

Constants

This section is empty.

Variables

View Source
var Functions = map[string]interface{}{
	"env": GetEnv,

	"filename": filepath.Base,

	"basename": func(filename string) string {
		return strings.TrimSuffix(
			filepath.Base(filename),
			filepath.Ext(filename),
		)
	},

	"eq":    Eq,
	"in":    In,
	"notIn": NotIn,

	"toString": ToString,
	"showJSON": ShowJSON,

	"contact":     Contact,
	"empty":       Empty,
	"filterEmpty": FilterEmpty,

	"format": Format,

	"trimLinePrefix": TrimLinePrefix,
	"trimLineSuffix": TrimLineSuffix,
	"trimLineLeft":   TrimLineLeft,
	"trimLineRight":  TrimLineRight,
	"withLinePrefix": WithLinePrefix,
	"withLineSuffix": WithLineSuffix,
	"withLineSlash":  WithLineSlash,
}

Functions

func BaseName

func BaseName(name string) string

BaseName returns the last path element of the name, with the last dotted suffix removed. a/b/c.d => c

func CamelCase

func CamelCase(s string) string

CamelCase returns the CamelCased name. If there is an interior underscore followed by a lower case letter, drop the underscore and convert the letter to upper case. There is a remote possibility of this rewrite causing a name collision, but it's so remote we're prepared to pretend it's nonexistent - since the C++ generator lowercases names, it's extremely unlikely to have two fields with different capitalizations. In short, _my_field_name_2 becomes XMyFieldName_2.

func CamelCaseSlice

func CamelCaseSlice(elem []string) string

CamelCaseSlice is like CamelCase, but the argument is a slice of strings to be joined with "_".

func Contact

func Contact(args_ ...interface{}) []interface{}

func EachLine

func EachLine(s string, f eachLineFunc) string

func Empty

func Empty(given interface{}) bool

func Eq

func Eq(a interface{}, b interface{}) (ok bool)

func FileExists

func FileExists(filename string) bool

判断文件是否存在

func FilterEmpty

func FilterEmpty(v ...interface{}) []interface{}

func Format

func Format(_args ...interface{}) (string, error)

格式化为字符串 参数: (args ...interface{}, FormatPipeline )

将 FormatPipeline 放在最后是为了适配 go template 的 pipeline 用法

example: `$FormatPipeline|format $arg1 $arg2` => `$FormatPipeline.Format($arg1,$arg2)`

func GetEnv

func GetEnv(key string, def ...string) string

func GetName

func GetName(v interface{}) string

func In

func In(needle interface{}, haystack ...interface{}) (ok bool)

func InStrings

func InStrings(x string, list ...string) bool

func Join

func Join(seq string, args_ ...interface{}) string

func JsonMarshal

func JsonMarshal(v interface{}) []byte

func JsonMarshalIndent

func JsonMarshalIndent(v interface{}) []byte

func Kind

func Kind(v interface{}, dereference_ ...bool) reflect.Kind

func NotIn

func NotIn(needle interface{}, haystack ...interface{}) bool

func ParseBool

func ParseBool(v interface{}) (bool, error)

func RegisterFuncMap

func RegisterFuncMap(functions map[string]interface{})

func ReverseStrings

func ReverseStrings(s []string) []string

反转数组

func Set

func Set(_args ...interface{}) interface{}

func ShowJSON

func ShowJSON(data interface{}, depth_ ...int) string

显示格式化后的json数据

depth - 换行的深度

func Struct2Map

func Struct2Map(obj interface{}) map[string]interface{}

func Throws

func Throws(err error)

func ToBool

func ToBool(v interface{}, _def ...bool) bool

func ToInt

func ToInt(v interface{}, def int) int

func ToString

func ToString(v interface{}, def ...string) string

func TrimLineLeft

func TrimLineLeft(cutset string, s string) string

func TrimLinePrefix

func TrimLinePrefix(prefix string, s string) string

func TrimLineRight

func TrimLineRight(cutset string, s string) string

func TrimLineSuffix

func TrimLineSuffix(suffix string, s string) string

func WithLinePrefix

func WithLinePrefix(prefix string, s string) string

func WithLineSlash

func WithLineSlash(s string) string

func WithLineSuffix

func WithLineSuffix(suffix string, s string) string

Types

type Args

type Args []interface{}

func (Args) Bool

func (args Args) Bool(index int, def bool) bool

func (Args) Def

func (args Args) Def(index int, def interface{}) interface{}

func (Args) First

func (args Args) First() interface{}

func (Args) Flatten

func (args Args) Flatten() Args

func (Args) Get

func (args Args) Get(index int) (v interface{}, ok bool)

func (Args) Int

func (args Args) Int(index int, def int) int

func (Args) Kind

func (args Args) Kind(index int, dereference bool) reflect.Kind

dereference 是否解引用,如果是 reflect.Ptr 则返回其原始类型,如 *int(ptr) => int

func (Args) Last

func (args Args) Last() interface{}

func (Args) Len

func (args Args) Len() int

func (*Args) Pop

func (args *Args) Pop() interface{}

func (*Args) Push

func (args *Args) Push(other ...interface{}) Args

func (*Args) Shift

func (args *Args) Shift() interface{}

func (Args) String

func (args Args) String(index int, def string) string

type FormatPipeline

type FormatPipeline interface {
	FormatPipeline(args Args) (string, error)
}

type SetPipeline

type SetPipeline interface {
	SetPipeline(field string, value interface{}) interface{}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL