Int declares an attribute of int-typed in Dictionary d.
(name string, value interface{}, doc string, checker func(int) error)
| 57 | |
| 58 | // Int declares an attribute of int-typed in Dictionary d. |
| 59 | func (d Dictionary) Int(name string, value interface{}, doc string, checker func(int) error) Dictionary { |
| 60 | interfaceChecker := func(value interface{}, name string) error { |
| 61 | if intValue, ok := value.(int); ok { |
| 62 | if checker != nil { |
| 63 | if err := checker(intValue); err != nil { |
| 64 | return fmt.Errorf("attribute %s error: %s", name, err) |
| 65 | } |
| 66 | } |
| 67 | return nil |
| 68 | } |
| 69 | return fmt.Errorf("attribute %s must be of type int, but got %T", name, value) |
| 70 | } |
| 71 | |
| 72 | if value != nil { |
| 73 | err := interfaceChecker(value, name) |
| 74 | if err != nil { |
| 75 | log.Panicf("default value of attribute %s is invalid, error is: %s", name, err) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | d[name] = &description{ |
| 80 | typ: intType, |
| 81 | defaultValue: value, |
| 82 | doc: doc, |
| 83 | checker: interfaceChecker, |
| 84 | } |
| 85 | return d |
| 86 | } |
| 87 | |
| 88 | // Float declares an attribute of float32-typed in Dictionary d. |
| 89 | func (d Dictionary) Float(name string, value interface{}, doc string, checker func(float32) error) Dictionary { |