本文整理汇总了Golang中github.com/ipfs/go-ipfs/util.ErrCast函数的典型用法代码示例。如果您正苦于以下问题:Golang ErrCast函数的具体用法?Golang ErrCast怎么用?Golang ErrCast使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ErrCast函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Parse
// Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object.
func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *cmds.Command, []string, error) {
path, opts, stringVals, cmd, err := parseOpts(input, root)
if err != nil {
return nil, nil, path, err
}
optDefs, err := root.GetOptions(path)
if err != nil {
return nil, cmd, path, err
}
req, err := cmds.NewRequest(path, opts, nil, nil, cmd, optDefs)
if err != nil {
return nil, cmd, path, err
}
// if -r is provided, and it is associated with the package builtin
// recursive path option, allow recursive file paths
recursiveOpt := req.Option(cmds.RecShort)
recursive := false
if recursiveOpt != nil && recursiveOpt.Definition() == cmds.OptionRecursivePath {
recursive, _, err = recursiveOpt.Bool()
if err != nil {
return req, nil, nil, u.ErrCast()
}
}
// if '--hidden' is provided, enumerate hidden paths
hiddenOpt := req.Option("hidden")
hidden := false
if hiddenOpt != nil {
hidden, _, err = hiddenOpt.Bool()
if err != nil {
return req, nil, nil, u.ErrCast()
}
}
stringArgs, fileArgs, err := parseArgs(stringVals, stdin, cmd.Arguments, recursive, hidden, root)
if err != nil {
return req, cmd, path, err
}
req.SetArguments(stringArgs)
if len(fileArgs) > 0 {
file := files.NewSliceFile("", "", fileArgs)
req.SetFiles(file)
}
err = cmd.CheckArguments(req)
if err != nil {
return req, cmd, path, err
}
return req, cmd, path, nil
}
开发者ID:noffle,项目名称:go-ipfs,代码行数:57,代码来源:parse.go
示例2: objectMarshaler
func objectMarshaler(res cmds.Response) (io.Reader, error) {
o, ok := res.Output().(*Object)
if !ok {
return nil, u.ErrCast()
}
return strings.NewReader(o.Hash + "\n"), nil
}
开发者ID:thomas-gardner,项目名称:go-ipfs,代码行数:8,代码来源:patch.go
示例3: Bool
// value accessor methods, gets the value as a certain type
func (ov OptionValue) Bool() (value bool, found bool, err error) {
if !ov.found {
return false, false, nil
}
val, ok := ov.value.(bool)
if !ok {
err = util.ErrCast()
}
return val, ov.found, err
}
开发者ID:avbalu,项目名称:go-ipfs,代码行数:11,代码来源:option.go
示例4: String
func (ov OptionValue) String() (value string, found bool, err error) {
if !ov.found {
return "", false, nil
}
val, ok := ov.value.(string)
if !ok {
err = util.ErrCast()
}
return val, ov.found, err
}
开发者ID:avbalu,项目名称:go-ipfs,代码行数:10,代码来源:option.go
示例5: Float
func (ov OptionValue) Float() (value float64, found bool, err error) {
if !ov.found {
return 0, false, nil
}
val, ok := ov.value.(float64)
if !ok {
err = util.ErrCast()
}
return val, ov.found, err
}
开发者ID:avbalu,项目名称:go-ipfs,代码行数:10,代码来源:option.go
示例6: Uint
func (ov OptionValue) Uint() (value uint, found bool, err error) {
if !ov.found {
return 0, false, nil
}
val, ok := ov.value.(uint)
if !ok {
err = util.ErrCast()
}
return val, ov.found, err
}
开发者ID:avbalu,项目名称:go-ipfs,代码行数:10,代码来源:option.go
示例7: bootstrapMarshaler
func bootstrapMarshaler(res cmds.Response) (io.Reader, error) {
v, ok := res.Output().(*BootstrapOutput)
if !ok {
return nil, u.ErrCast()
}
buf := new(bytes.Buffer)
err := bootstrapWritePeers(buf, "", v.Peers)
return buf, err
}
开发者ID:avbalu,项目名称:go-ipfs,代码行数:10,代码来源:bootstrap.go
示例8: ConvertOptions
func (r *request) ConvertOptions() error {
for k, v := range r.options {
opt, ok := r.optionDefs[k]
if !ok {
continue
}
kind := reflect.TypeOf(v).Kind()
if kind != opt.Type() {
if kind == String {
convert := converters[opt.Type()]
str, ok := v.(string)
if !ok {
return u.ErrCast()
}
val, err := convert(str)
if err != nil {
value := fmt.Sprintf("value '%v'", v)
if len(str) == 0 {
value = "empty value"
}
return fmt.Errorf("Could not convert %s to type '%s' (for option '-%s')",
value, opt.Type().String(), k)
}
r.options[k] = val
} else {
return fmt.Errorf("Option '%s' should be type '%s', but got type '%s'",
k, opt.Type().String(), kind.String())
}
} else {
r.options[k] = v
}
for _, name := range opt.Names() {
if _, ok := r.options[name]; name != k && ok {
return fmt.Errorf("Duplicate command options were provided ('%s' and '%s')",
k, name)
}
}
}
return nil
}
开发者ID:avbalu,项目名称:go-ipfs,代码行数:44,代码来源:request.go
示例9:
go func() {
defer close(outChan)
if err := addAllAndPin(req.Files()); err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
}()
},
PostRun: func(req cmds.Request, res cmds.Response) {
if res.Error() != nil {
return
}
outChan, ok := res.Output().(<-chan interface{})
if !ok {
res.SetError(u.ErrCast(), cmds.ErrNormal)
return
}
res.SetOutput(nil)
quiet, _, err := req.Option("quiet").Bool()
if err != nil {
res.SetError(u.ErrCast(), cmds.ErrNormal)
return
}
size := int64(0)
s, found := req.Values()["size"]
if found {
size = s.(int64)
}
开发者ID:andradeandrey,项目名称:go-ipfs,代码行数:31,代码来源:add.go
示例10:
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
node, err := core.Resolve(req.Context(), n, p)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
key, err := node.Key()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&ResolvedPath{path.FromKey(key)})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
output, ok := res.Output().(*ResolvedPath)
if !ok {
return nil, u.ErrCast()
}
return strings.NewReader(output.Path.String() + "\n"), nil
},
},
Type: ResolvedPath{},
}
开发者ID:thomas-gardner,项目名称:go-ipfs,代码行数:30,代码来源:resolve.go
示例11:
},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
if !nd.OnlineMode() {
res.SetError(errNotOnline, cmds.ErrClient)
return
}
bs, ok := nd.Exchange.(*bitswap.Bitswap)
if !ok {
res.SetError(u.ErrCast(), cmds.ErrNormal)
return
}
var ks []key.Key
for _, arg := range req.Arguments() {
dec := key.B58KeyDecode(arg)
if dec == "" {
res.SetError(fmt.Errorf("incorrectly formatted key: %s", arg), cmds.ErrNormal)
return
}
ks = append(ks, dec)
}
bs.CancelWants(ks)
开发者ID:riversparks,项目名称:go-ipfs,代码行数:31,代码来源:bitswap.go
示例12:
cmds.BoolOption("recursive", "r", "Resolve until the result is not a DNS link."),
},
Run: func(req cmds.Request, res cmds.Response) {
recursive, _, _ := req.Option("recursive").Bool()
name := req.Arguments()[0]
resolver := namesys.NewDNSResolver()
depth := 1
if recursive {
depth = namesys.DefaultDepthLimit
}
output, err := resolver.ResolveN(req.Context(), name, depth)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&ResolvedPath{output})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
output, ok := res.Output().(*ResolvedPath)
if !ok {
return nil, util.ErrCast()
}
return strings.NewReader(output.Path.String() + "\n"), nil
},
},
Type: ResolvedPath{},
}
开发者ID:Patagonicus,项目名称:go-ipfs,代码行数:30,代码来源:dns.go
注:本文中的github.com/ipfs/go-ipfs/util.ErrCast函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论