| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package datatest |
| 8 |
|
| 9 |
import "fmt" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
type IntOrStr struct { |
| 14 |
Value int |
| 15 |
Registry *ConstantRegistry |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
func (ios *IntOrStr) FromValue(v any) error { |
| 20 |
switch val := v.(type) { |
| 21 |
case int: |
| 22 |
ios.Value = val |
| 23 |
return nil |
| 24 |
case string: |
| 25 |
registry := ios.Registry |
| 26 |
if registry == nil { |
| 27 |
return fmt.Errorf("no constant registry available for resolving %q", val) |
| 28 |
} |
| 29 |
resolved, ok := registry.Resolve(val) |
| 30 |
if !ok { |
| 31 |
return fmt.Errorf("unknown constant name: %s", val) |
| 32 |
} |
| 33 |
ios.Value = resolved |
| 34 |
return nil |
| 35 |
default: |
| 36 |
return fmt.Errorf("IntOrStr value must be int or string, got %T", v) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
type ByteInput []byte |
| 42 |
|
| 43 |
|
| 44 |
func (bi *ByteInput) FromValue(v any) error { |
| 45 |
|
| 46 |
if strVal, ok := v.(string); ok { |
| 47 |
*bi = []byte(strVal) |
| 48 |
return nil |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
if intVal, ok := v.(int); ok { |
| 53 |
if intVal < 0 || intVal > 255 { |
| 54 |
return fmt.Errorf("byte value out of range [0-255]: %d", intVal) |
| 55 |
} |
| 56 |
*bi = []byte{byte(intVal)} |
| 57 |
return nil |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
intSlice, ok := v.([]any) |
| 62 |
if !ok { |
| 63 |
return fmt.Errorf("input must be a string, int, or sequence of integers, got %T", v) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
bytes := make([]byte, len(intSlice)) |
| 68 |
for i, val := range intSlice { |
| 69 |
intVal, ok := val.(int) |
| 70 |
if !ok { |
| 71 |
return fmt.Errorf("byte array element must be int, got %T", val) |
| 72 |
} |
| 73 |
if intVal < 0 || intVal > 255 { |
| 74 |
return fmt.Errorf("byte value out of range [0-255]: %d", intVal) |
| 75 |
} |
| 76 |
bytes[i] = byte(intVal) |
| 77 |
} |
| 78 |
*bi = bytes |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
type Args []any |
| 85 |
|
| 86 |
|
| 87 |
func (a *Args) FromValue(v any) error { |
| 88 |
|
| 89 |
if arrVal, ok := v.([]any); ok { |
| 90 |
*a = arrVal |
| 91 |
return nil |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
*a = []any{v} |
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
type StringSlice []string |
| 101 |
|
| 102 |
|
| 103 |
func (ss *StringSlice) FromValue(v any) error { |
| 104 |
|
| 105 |
if strVal, ok := v.(string); ok { |
| 106 |
*ss = []string{strVal} |
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
if slice, ok := v.([]any); ok { |
| 112 |
strs := make([]string, len(slice)) |
| 113 |
for i, item := range slice { |
| 114 |
str, ok := item.(string) |
| 115 |
if !ok { |
| 116 |
return fmt.Errorf("StringSlice element %d must be string, got %T", i, item) |
| 117 |
} |
| 118 |
strs[i] = str |
| 119 |
} |
| 120 |
*ss = strs |
| 121 |
return nil |
| 122 |
} |
| 123 |
|
| 124 |
return fmt.Errorf("StringSlice must be string or []string, got %T", v) |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
type IntSlice []int |
| 129 |
|
| 130 |
|
| 131 |
func (is *IntSlice) FromValue(v any) error { |
| 132 |
|
| 133 |
if intVal, ok := v.(int); ok { |
| 134 |
*is = []int{intVal} |
| 135 |
return nil |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
if slice, ok := v.([]any); ok { |
| 140 |
ints := make([]int, len(slice)) |
| 141 |
for i, item := range slice { |
| 142 |
intVal, ok := item.(int) |
| 143 |
if !ok { |
| 144 |
return fmt.Errorf("IntSlice element %d must be int, got %T", i, item) |
| 145 |
} |
| 146 |
ints[i] = intVal |
| 147 |
} |
| 148 |
*is = ints |
| 149 |
return nil |
| 150 |
} |
| 151 |
|
| 152 |
return fmt.Errorf("IntSlice must be int or []int, got %T", v) |
| 153 |
} |