本文整理汇总了Golang中github.com/gravitational/trace.Wrap函数的典型用法代码示例。如果您正苦于以下问题:Golang Wrap函数的具体用法?Golang Wrap怎么用?Golang Wrap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Wrap函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: ProcessObject
// ProcessObject checks if the object is valid from this schema's standpoint
// and returns an object with defaults set up according to schema's spec
func (j *JSONSchema) ProcessObject(in interface{}) (interface{}, error) {
result, err := j.schema.Validate(gojsonschema.NewGoLoader(in))
if err != nil {
return nil, trace.Wrap(err)
}
if !result.Valid() {
return nil, trace.Wrap(trace.Errorf("errors: %v", schemaErrors(result.Errors())))
}
return setDefaults(j.rawSchema, in), nil
}
开发者ID:lucmichalski,项目名称:configure,代码行数:12,代码来源:schema.go
示例2: ParseCommandLine
// ParseCommandLine takes a pointer to a function and attempts
// to initialize it from environment variables.
func ParseCommandLine(v interface{}, args []string) error {
app, err := NewCommandLineApp(v)
if err != nil {
return trace.Wrap(err)
}
if _, err := app.Parse(args); err != nil {
return trace.Wrap(err)
}
return nil
}
开发者ID:lucmichalski,项目名称:configure,代码行数:12,代码来源:cli.go
示例3: New
// New returns JSON schema created from JSON byte string
// returns a valid schema or error if schema is invalid
func New(data []byte) (*JSONSchema, error) {
j := JSONSchema{}
err := json.Unmarshal(data, &j.rawSchema)
if err != nil {
return nil, trace.Wrap(err)
}
loader := gojsonschema.NewGoLoader(j.rawSchema)
j.schema, err = gojsonschema.NewSchema(loader)
if err != nil {
return nil, trace.Wrap(err)
}
return &j, nil
}
开发者ID:gravitational,项目名称:configure,代码行数:16,代码来源:schema.go
示例4: parseEnum
func (p *cparser) parseEnum(s paramSpec) (Param, error) {
var e *enumSpec
if err := json.Unmarshal(s.S, &e); err != nil {
return nil, trace.Wrap(
err, fmt.Sprintf("failed to parse: '%v'", string(s.S)))
}
if len(e.Values) == 0 {
return nil, trace.Errorf("provide at least one value for '%v'", s.Name)
}
values := make([]string, len(e.Values))
seen := make(map[string]bool, len(e.Values))
for i, v := range e.Values {
if v == "" {
return nil, trace.Errorf("value can not be an empty string")
}
if seen[v] {
return nil, trace.Errorf("duplicate value: '%v'", v)
}
values[i] = v
}
ep := &EnumParam{values: values}
ep.paramCommon = s.common()
return ep, nil
}
开发者ID:gravitational,项目名称:configure,代码行数:27,代码来源:schema.go
示例5: TransformXML
// TransformXML parses the XML tree, traverses it and calls TransformFunc
// on each XML token, writing the output to the writer, resulting in a
// transformed XML tree
func TransformXML(decoder *xml.Decoder, encoder *xml.Encoder, fn TransformFunc) error {
parentNodes := &NodeList{}
for {
token, err := decoder.Token()
if err != nil {
if err != io.EOF {
return trace.Wrap(err)
}
break
}
for _, t := range fn(parentNodes, token) {
if err := encoder.EncodeToken(t); err != nil {
return err
}
}
switch e := token.(type) {
case xml.StartElement:
parentNodes.Push(e)
case xml.EndElement:
parentNodes.Pop()
}
}
encoder.Flush()
return nil
}
开发者ID:gravitational,项目名称:configure,代码行数:28,代码来源:xml.go
示例6: parseKeyVal
func (p *cparser) parseKeyVal(s paramSpec) (Param, error) {
var k *kvSpec
if err := json.Unmarshal(s.S, &k); err != nil {
return nil, trace.Wrap(
err, fmt.Sprintf("failed to parse: '%v'", string(s.S)))
}
if len(k.Keys) == 0 {
return nil, trace.Errorf("provide at least one key for '%v'", s.Name)
}
keys := make([]Param, len(k.Keys))
for i, ks := range k.Keys {
k, err := p.parseParam(ks, true)
if err != nil {
return nil, err
}
keys[i] = k
}
if err := checkSameNames(keys); err != nil {
return nil, err
}
kv := &KVParam{keys: keys, separator: k.Separator}
kv.paramCommon = s.common()
return kv, nil
}
开发者ID:gravitational,项目名称:configure,代码行数:28,代码来源:schema.go
示例7: SetEnv
// SetEnv sets the value from environment variable using json encoding
func (kv *KeyValSlice) SetEnv(v string) error {
if err := json.Unmarshal([]byte(v), &kv); err != nil {
return trace.Wrap(
err, "failed to parse environment variable, expected JSON map")
}
return nil
}
开发者ID:gravitational,项目名称:configure,代码行数:8,代码来源:kv.go
示例8: File
func (c *ctx) File(path string) (string, error) {
o, err := ioutil.ReadFile(path)
if err != nil {
return "", trace.Wrap(err, fmt.Sprintf("reading file: %v", path))
}
return string(o), nil
}
开发者ID:gravitational,项目名称:configure,代码行数:7,代码来源:tpl.go
示例9: Set
func (c *cliBoolValue) Set(v string) error {
boolVal, err := strconv.ParseBool(v)
if err != nil {
return trace.Wrap(err)
}
*c.v = boolVal
return nil
}
开发者ID:lucmichalski,项目名称:configure,代码行数:8,代码来源:cli.go
示例10: ParseYAML
// ParseYAML parses yaml-encoded byte string into the struct
// passed to the function.
// EnableTemplating() argument allows to treat configuration file as a template
// for example, it will support {{env "VAR"}} - that will substitute
// environment variable "VAR" and pass it to YAML file parser
func ParseYAML(data []byte, cfg interface{}, funcArgs ...ParseOption) error {
var opts parseOptions
for _, fn := range funcArgs {
fn(&opts)
}
var err error
if opts.templating {
if data, err = renderTemplate(data); err != nil {
return trace.Wrap(err)
}
}
if err := yaml.Unmarshal(data, cfg); err != nil {
return trace.Wrap(err)
}
return nil
}
开发者ID:gravitational,项目名称:configure,代码行数:22,代码来源:yaml.go
示例11: Set
func (c *CIDR) Set(v string) error {
out, err := ParseCIDR(v)
if err != nil {
return trace.Wrap(err)
}
*c = *out
return nil
}
开发者ID:gravitational,项目名称:configure,代码行数:8,代码来源:cidr.go
示例12: ParseVariablesJSON
func ParseVariablesJSON(r io.Reader) (*Config, error) {
var variables []paramSpec
if err := json.NewDecoder(r).Decode(&variables); err != nil {
return nil, trace.Wrap(err)
}
return newParser().parse(configV1{Params: variables})
}
开发者ID:gravitational,项目名称:configure,代码行数:8,代码来源:schema.go
示例13: ParseJSON
func ParseJSON(r io.Reader) (*Config, error) {
var c *configV1
if err := json.NewDecoder(r).Decode(&c); err != nil {
return nil, trace.Wrap(err)
}
return newParser().parse(*c)
}
开发者ID:gravitational,项目名称:configure,代码行数:8,代码来源:schema.go
示例14: checkName
func (p *cparser) checkName(n string) error {
for _, pr := range p.params {
if pr.Name() == n {
return trace.Errorf("parameter '%v' is already defined", n)
}
}
e, err := parser.ParseExpr(n)
if err != nil {
return trace.Wrap(
err, fmt.Sprintf("failed to parse name: '%v'", n))
}
if _, ok := e.(*ast.Ident); !ok {
return trace.Wrap(
err, fmt.Sprintf("name should be a valid identifier: '%v'", n))
}
return nil
}
开发者ID:gravitational,项目名称:configure,代码行数:17,代码来源:schema.go
示例15: NewCommandLineApp
// NewCommandLineApp generates a command line parsing tool based on the struct
// that was passed in as a parameter
func NewCommandLineApp(v interface{}) (*kingpin.Application, error) {
s := reflect.ValueOf(v).Elem()
app := kingpin.New("app", "Auto generated command line application")
if err := setupApp(app, s); err != nil {
return nil, trace.Wrap(err)
}
return app, nil
}
开发者ID:lucmichalski,项目名称:configure,代码行数:10,代码来源:cli.go
示例16: Set
// Set accepts string with arguments in the form "key:val,key2:val2"
func (kv *KeyValSlice) Set(v string) error {
if len(*kv) == 0 {
*kv = make([]map[string]string, 0)
}
var i KeyVal
if err := i.Set(v); err != nil {
return trace.Wrap(err)
}
*kv = append(*kv, i)
return nil
}
开发者ID:gravitational,项目名称:configure,代码行数:12,代码来源:kv.go
示例17: renderTemplate
func renderTemplate(data []byte) ([]byte, error) {
t := template.New("tpl")
c, err := newCtx()
if err != nil {
return nil, trace.Wrap(err)
}
t.Funcs(map[string]interface{}{
"env": c.Env,
"file": c.File,
})
t, err = t.Parse(string(data))
if err != nil {
return nil, trace.Wrap(err)
}
buf := &bytes.Buffer{}
if err := t.Execute(buf, nil); err != nil {
return nil, trace.Wrap(err)
}
return buf.Bytes(), nil
}
开发者ID:gravitational,项目名称:configure,代码行数:20,代码来源:tpl.go
示例18: parseList
func (p *cparser) parseList(s paramSpec) (Param, error) {
var ps *paramSpec
if err := json.Unmarshal(s.S, &ps); err != nil {
return nil, trace.Wrap(err, "failed to parse: '%v'", string(s.S))
}
el, err := p.parseParam(*ps, false)
if err != nil {
return nil, err
}
l := &ListParam{el: el}
l.paramCommon = s.common()
return l, nil
}
开发者ID:gravitational,项目名称:configure,代码行数:13,代码来源:schema.go
示例19: ParseVars
func (c *Config) ParseVars(vars map[string]string) error {
for _, p := range c.Params {
val, ok := vars[p.Name()]
if !ok {
if p.Required() {
return trace.Errorf("missing value for required variable: %v", p.Name())
} else {
val = p.Default()
}
}
if err := p.Set(val); err != nil {
return trace.Wrap(err)
}
}
return nil
}
开发者ID:gravitational,项目名称:configure,代码行数:16,代码来源:schema.go
示例20: ProcessObject
// ProcessObject checks if the object is valid from this schema's standpoint
// and returns an object with defaults set up according to schema's spec
func (j *JSONSchema) ProcessObject(in interface{}) (interface{}, error) {
defaults := setDefaults(j.rawSchema, in)
result, err := j.schema.Validate(gojsonschema.NewGoLoader(defaults))
if err != nil {
return nil, trace.Wrap(err)
}
if !result.Valid() {
errors := result.Errors()
output := make([]string, len(errors))
for i, err := range errors {
output[i] = fmt.Sprintf("%v", err)
}
return nil, trace.Errorf("failed to validate: %v", strings.Join(output, ","))
}
return defaults, nil
}
开发者ID:gravitational,项目名称:configure,代码行数:20,代码来源:schema.go
注:本文中的github.com/gravitational/trace.Wrap函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论