✨ Make IntOrString support TOML as well
This commit is contained in:
parent
f4497aef7e
commit
e8401db519
3 changed files with 77 additions and 17 deletions
51
types/int_string.go
Normal file
51
types/int_string.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package types
|
||||
|
||||
import "fmt"
|
||||
|
||||
type IntOrString struct {
|
||||
intVal int
|
||||
strVal string
|
||||
isInt bool
|
||||
}
|
||||
|
||||
func (is *IntOrString) Set(val any) error {
|
||||
switch v := val.(type) {
|
||||
case int:
|
||||
case int8:
|
||||
case int16:
|
||||
case int32:
|
||||
case int64:
|
||||
case uint:
|
||||
case uint8:
|
||||
case uint16:
|
||||
case uint32:
|
||||
is.isInt = true
|
||||
is.intVal = int(v)
|
||||
return nil
|
||||
case string:
|
||||
is.isInt = false
|
||||
is.strVal = v
|
||||
return nil
|
||||
case []byte:
|
||||
is.isInt = false
|
||||
is.strVal = string(v)
|
||||
return nil
|
||||
}
|
||||
|
||||
//nolint:err113 // Temp skip
|
||||
return fmt.Errorf("invalid type: %T", val)
|
||||
}
|
||||
|
||||
func (is IntOrString) IsZero() bool {
|
||||
if is.isInt {
|
||||
return is.intVal == 0
|
||||
}
|
||||
return is.strVal == ""
|
||||
}
|
||||
|
||||
func (is IntOrString) Value() any {
|
||||
if is.isInt {
|
||||
return is.intVal
|
||||
}
|
||||
return is.strVal
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue