File: option_private.go

package info (click to toggle)
golang-github-svent-go-flags 1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 224 kB
  • ctags: 186
  • sloc: sh: 13; makefile: 7
file content (86 lines) | stat: -rw-r--r-- 1,488 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package flags

import (
	"reflect"
)

func (option *Option) canArgument() bool {
	return !option.isBool()
}

func (option *Option) clear() {
	tp := option.Value.Type()

	switch tp.Kind() {
	case reflect.Func:
		// Skip
	case reflect.Map:
		// Empty the map
		option.Value.Set(reflect.MakeMap(tp))
	default:
		zeroval := reflect.Zero(tp)
		option.Value.Set(zeroval)
	}
}

func (option *Option) isBool() bool {
	tp := option.Value.Type()

	switch tp.Kind() {
	case reflect.Bool:
		return true
	case reflect.Slice:
		return (tp.Elem().Kind() == reflect.Bool)
	case reflect.Func:
		return tp.NumIn() == 0
	}

	return false
}

func (option *Option) isFunc() bool {
	return option.Value.Type().Kind() == reflect.Func
}

func (option *Option) iniName() string {
	if len(option.iniUsedName) != 0 {
		return option.iniUsedName
	}

	name := option.tag.Get("ini-name")

	if len(name) != 0 {
		return name
	}

	return option.Field.Name
}

func (option *Option) call(value *string) error {
	var retval []reflect.Value

	if value == nil {
		retval = option.Value.Call(nil)
	} else {
		tp := option.Value.Type().In(0)

		val := reflect.New(tp)
		val = reflect.Indirect(val)

		if err := convert(*value, val, option.tag); err != nil {
			return err
		}

		retval = option.Value.Call([]reflect.Value{val})
	}

	if len(retval) == 1 && retval[0].Type() == reflect.TypeOf((*error)(nil)).Elem() {
		if retval[0].Interface() == nil {
			return nil
		}

		return retval[0].Interface().(error)
	}

	return nil
}