github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/cli/altsrc/map_input_source.go (about) 1 package altsrc 2 3 import ( 4 "fmt" 5 "reflect" 6 "time" 7 8 "github.com/insionng/yougam/libraries/cli" 9 ) 10 11 // MapInputSource implements InputSourceContext to return 12 // data from the map that is loaded. 13 type MapInputSource struct { 14 valueMap map[string]interface{} 15 } 16 17 // Int returns an int from the map if it exists otherwise returns 0 18 func (fsm *MapInputSource) Int(name string) (int, error) { 19 otherGenericValue, exists := fsm.valueMap[name] 20 if exists { 21 otherValue, isType := otherGenericValue.(int) 22 if !isType { 23 return 0, incorrectTypeForFlagError(name, "int", otherGenericValue) 24 } 25 26 return otherValue, nil 27 } 28 29 return 0, nil 30 } 31 32 // Duration returns a duration from the map if it exists otherwise returns 0 33 func (fsm *MapInputSource) Duration(name string) (time.Duration, error) { 34 otherGenericValue, exists := fsm.valueMap[name] 35 if exists { 36 otherValue, isType := otherGenericValue.(time.Duration) 37 if !isType { 38 return 0, incorrectTypeForFlagError(name, "duration", otherGenericValue) 39 } 40 return otherValue, nil 41 } 42 43 return 0, nil 44 } 45 46 // Float64 returns an float64 from the map if it exists otherwise returns 0 47 func (fsm *MapInputSource) Float64(name string) (float64, error) { 48 otherGenericValue, exists := fsm.valueMap[name] 49 if exists { 50 otherValue, isType := otherGenericValue.(float64) 51 if !isType { 52 return 0, incorrectTypeForFlagError(name, "float64", otherGenericValue) 53 } 54 return otherValue, nil 55 } 56 57 return 0, nil 58 } 59 60 // String returns a string from the map if it exists otherwise returns an empty string 61 func (fsm *MapInputSource) String(name string) (string, error) { 62 otherGenericValue, exists := fsm.valueMap[name] 63 if exists { 64 otherValue, isType := otherGenericValue.(string) 65 if !isType { 66 return "", incorrectTypeForFlagError(name, "string", otherGenericValue) 67 } 68 return otherValue, nil 69 } 70 71 return "", nil 72 } 73 74 // StringSlice returns an []string from the map if it exists otherwise returns nil 75 func (fsm *MapInputSource) StringSlice(name string) ([]string, error) { 76 otherGenericValue, exists := fsm.valueMap[name] 77 if exists { 78 otherValue, isType := otherGenericValue.([]string) 79 if !isType { 80 return nil, incorrectTypeForFlagError(name, "[]string", otherGenericValue) 81 } 82 return otherValue, nil 83 } 84 85 return nil, nil 86 } 87 88 // IntSlice returns an []int from the map if it exists otherwise returns nil 89 func (fsm *MapInputSource) IntSlice(name string) ([]int, error) { 90 otherGenericValue, exists := fsm.valueMap[name] 91 if exists { 92 otherValue, isType := otherGenericValue.([]int) 93 if !isType { 94 return nil, incorrectTypeForFlagError(name, "[]int", otherGenericValue) 95 } 96 return otherValue, nil 97 } 98 99 return nil, nil 100 } 101 102 // Generic returns an cli.Generic from the map if it exists otherwise returns nil 103 func (fsm *MapInputSource) Generic(name string) (cli.Generic, error) { 104 otherGenericValue, exists := fsm.valueMap[name] 105 if exists { 106 otherValue, isType := otherGenericValue.(cli.Generic) 107 if !isType { 108 return nil, incorrectTypeForFlagError(name, "cli.Generic", otherGenericValue) 109 } 110 return otherValue, nil 111 } 112 113 return nil, nil 114 } 115 116 // Bool returns an bool from the map otherwise returns false 117 func (fsm *MapInputSource) Bool(name string) (bool, error) { 118 otherGenericValue, exists := fsm.valueMap[name] 119 if exists { 120 otherValue, isType := otherGenericValue.(bool) 121 if !isType { 122 return false, incorrectTypeForFlagError(name, "bool", otherGenericValue) 123 } 124 return otherValue, nil 125 } 126 127 return false, nil 128 } 129 130 // BoolT returns an bool from the map otherwise returns true 131 func (fsm *MapInputSource) BoolT(name string) (bool, error) { 132 otherGenericValue, exists := fsm.valueMap[name] 133 if exists { 134 otherValue, isType := otherGenericValue.(bool) 135 if !isType { 136 return true, incorrectTypeForFlagError(name, "bool", otherGenericValue) 137 } 138 return otherValue, nil 139 } 140 141 return true, nil 142 } 143 144 func incorrectTypeForFlagError(name, expectedTypeName string, value interface{}) error { 145 valueType := reflect.TypeOf(value) 146 valueTypeName := "" 147 if valueType != nil { 148 valueTypeName = valueType.Name() 149 } 150 151 return fmt.Errorf("Mismatched type for flag '%s'. Expected '%s' but actual is '%s'", name, expectedTypeName, valueTypeName) 152 }