github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/cli/pflags/api/sample.go (about) 1 package api 2 3 import ( 4 "encoding/json" 5 "errors" 6 7 "github.com/lyft/flytestdlib/storage" 8 ) 9 10 var DefaultTestType = &TestType{ 11 StringValue: "Welcome to defaults", 12 } 13 14 type TestType struct { 15 StringValue string `json:"str" pflag:"\"hello world\",\"life is short\""` 16 BoolValue bool `json:"bl" pflag:"true"` 17 NestedType NestedType `json:"nested"` 18 IntArray []int `json:"ints" pflag:"[]int{12%2C1}"` 19 StringArray []string `json:"strs" pflag:"[]string{\"12\"%2C\"1\"}"` 20 ComplexJSONArray []ComplexJSONType `json:"complexArr"` 21 StringToJSON ComplexJSONType `json:"c" pflag:",I'm a complex type but can be converted from string."` 22 IgnoredMap map[string]string `json:"ignored-map" pflag:"-,"` 23 StorageConfig storage.Config `json:"storage"` 24 IntValue *int `json:"i"` 25 } 26 27 type NestedType struct { 28 IntValue int `json:"i" pflag:",this is an important flag"` 29 } 30 31 type ComplexJSONType struct { 32 StringValue string `json:"str"` 33 IntValue int `json:"i"` 34 } 35 36 func (c *ComplexJSONType) UnmarshalJSON(b []byte) error { 37 if len(b) == 0 { 38 c.StringValue = "" 39 return nil 40 } 41 42 var v interface{} 43 if err := json.Unmarshal(b, &v); err != nil { 44 return err 45 } 46 switch value := v.(type) { 47 case string: 48 if len(value) == 0 { 49 c.StringValue = "" 50 } else { 51 c.StringValue = value 52 } 53 default: 54 return errors.New("invalid duration") 55 } 56 57 return nil 58 }