gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/stretchr/objx/value.go (about) 1 package objx 2 3 import ( 4 "fmt" 5 "strconv" 6 ) 7 8 // Value provides methods for extracting interface{} data in various 9 // types. 10 type Value struct { 11 // data contains the raw data being managed by this Value 12 data interface{} 13 } 14 15 // Data returns the raw data contained by this Value 16 func (v *Value) Data() interface{} { 17 return v.data 18 } 19 20 // String returns the value always as a string 21 func (v *Value) String() string { 22 switch { 23 case v.IsNil(): 24 return "" 25 case v.IsStr(): 26 return v.Str() 27 case v.IsBool(): 28 return strconv.FormatBool(v.Bool()) 29 case v.IsFloat32(): 30 return strconv.FormatFloat(float64(v.Float32()), 'f', -1, 32) 31 case v.IsFloat64(): 32 return strconv.FormatFloat(v.Float64(), 'f', -1, 64) 33 case v.IsInt(): 34 return strconv.FormatInt(int64(v.Int()), 10) 35 case v.IsInt8(): 36 return strconv.FormatInt(int64(v.Int8()), 10) 37 case v.IsInt16(): 38 return strconv.FormatInt(int64(v.Int16()), 10) 39 case v.IsInt32(): 40 return strconv.FormatInt(int64(v.Int32()), 10) 41 case v.IsInt64(): 42 return strconv.FormatInt(v.Int64(), 10) 43 case v.IsUint(): 44 return strconv.FormatUint(uint64(v.Uint()), 10) 45 case v.IsUint8(): 46 return strconv.FormatUint(uint64(v.Uint8()), 10) 47 case v.IsUint16(): 48 return strconv.FormatUint(uint64(v.Uint16()), 10) 49 case v.IsUint32(): 50 return strconv.FormatUint(uint64(v.Uint32()), 10) 51 case v.IsUint64(): 52 return strconv.FormatUint(v.Uint64(), 10) 53 } 54 return fmt.Sprintf("%#v", v.Data()) 55 } 56 57 // StringSlice returns the value always as a []string 58 func (v *Value) StringSlice(optionalDefault ...[]string) []string { 59 switch { 60 case v.IsStrSlice(): 61 return v.MustStrSlice() 62 case v.IsBoolSlice(): 63 slice := v.MustBoolSlice() 64 vals := make([]string, len(slice)) 65 for i, iv := range slice { 66 vals[i] = strconv.FormatBool(iv) 67 } 68 return vals 69 case v.IsFloat32Slice(): 70 slice := v.MustFloat32Slice() 71 vals := make([]string, len(slice)) 72 for i, iv := range slice { 73 vals[i] = strconv.FormatFloat(float64(iv), 'f', -1, 32) 74 } 75 return vals 76 case v.IsFloat64Slice(): 77 slice := v.MustFloat64Slice() 78 vals := make([]string, len(slice)) 79 for i, iv := range slice { 80 vals[i] = strconv.FormatFloat(iv, 'f', -1, 64) 81 } 82 return vals 83 case v.IsIntSlice(): 84 slice := v.MustIntSlice() 85 vals := make([]string, len(slice)) 86 for i, iv := range slice { 87 vals[i] = strconv.FormatInt(int64(iv), 10) 88 } 89 return vals 90 case v.IsInt8Slice(): 91 slice := v.MustInt8Slice() 92 vals := make([]string, len(slice)) 93 for i, iv := range slice { 94 vals[i] = strconv.FormatInt(int64(iv), 10) 95 } 96 return vals 97 case v.IsInt16Slice(): 98 slice := v.MustInt16Slice() 99 vals := make([]string, len(slice)) 100 for i, iv := range slice { 101 vals[i] = strconv.FormatInt(int64(iv), 10) 102 } 103 return vals 104 case v.IsInt32Slice(): 105 slice := v.MustInt32Slice() 106 vals := make([]string, len(slice)) 107 for i, iv := range slice { 108 vals[i] = strconv.FormatInt(int64(iv), 10) 109 } 110 return vals 111 case v.IsInt64Slice(): 112 slice := v.MustInt64Slice() 113 vals := make([]string, len(slice)) 114 for i, iv := range slice { 115 vals[i] = strconv.FormatInt(iv, 10) 116 } 117 return vals 118 case v.IsUintSlice(): 119 slice := v.MustUintSlice() 120 vals := make([]string, len(slice)) 121 for i, iv := range slice { 122 vals[i] = strconv.FormatUint(uint64(iv), 10) 123 } 124 return vals 125 case v.IsUint8Slice(): 126 slice := v.MustUint8Slice() 127 vals := make([]string, len(slice)) 128 for i, iv := range slice { 129 vals[i] = strconv.FormatUint(uint64(iv), 10) 130 } 131 return vals 132 case v.IsUint16Slice(): 133 slice := v.MustUint16Slice() 134 vals := make([]string, len(slice)) 135 for i, iv := range slice { 136 vals[i] = strconv.FormatUint(uint64(iv), 10) 137 } 138 return vals 139 case v.IsUint32Slice(): 140 slice := v.MustUint32Slice() 141 vals := make([]string, len(slice)) 142 for i, iv := range slice { 143 vals[i] = strconv.FormatUint(uint64(iv), 10) 144 } 145 return vals 146 case v.IsUint64Slice(): 147 slice := v.MustUint64Slice() 148 vals := make([]string, len(slice)) 149 for i, iv := range slice { 150 vals[i] = strconv.FormatUint(iv, 10) 151 } 152 return vals 153 } 154 if len(optionalDefault) == 1 { 155 return optionalDefault[0] 156 } 157 158 return []string{} 159 }