本文整理汇总了Golang中github.com/gravitational/trace.Wrap函数的典型用法代码示例。如果您正苦于以下问题:Golang Wrap函数的具体用法?Golang Wrap怎么用?Golang Wrap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Wrap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例11: Set
func (c *CIDR) Set(v string) error {
out, err := ParseCIDR(v)
if err != nil {
return trace.Wrap(err)
}
*c = *out
return nil
}
示例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})
}
示例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)
}
示例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
}
示例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
}