Debian Codegraph @

internal/testutil/datatest/converters.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Type converters for test data unmarshaling.
5 // Provides custom conversion logic for test data types like IntOrStr.
6
7 package datatest
8
9 import "fmt"
10
11 // IntOrStr can be converted from either an int or a string constant name.
12 // It uses a ConstantRegistry to resolve string names to integer values.
13 type IntOrStr struct {
14 Value int
15 Registry *ConstantRegistry // Required for string name resolution; if nil, returns error
16 }
17
18 // FromValue implements the custom converter interface used by UnmarshalStruct.
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 // ByteInput can be converted from either a string or a sequence of hex bytes.
41 type ByteInput []byte
42
43 // FromValue implements the custom converter interface used by UnmarshalStruct.
44 func (bi *ByteInput) FromValue(v any) error {
45 // Try string first
46 if strVal, ok := v.(string); ok {
47 *bi = []byte(strVal)
48 return nil
49 }
50
51 // Try single int (convert to single-byte array)
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 // Otherwise, it should be a sequence of integer bytes
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 // Convert integers to bytes
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 // Args can be converted from either a single value or an array of values.
83 // This is useful for method arguments that can be either scalar or array.
84 type Args []any
85
86 // FromValue implements the custom converter interface used by UnmarshalStruct.
87 func (a *Args) FromValue(v any) error {
88 // Try array first
89 if arrVal, ok := v.([]any); ok {
90 *a = arrVal
91 return nil
92 }
93
94 // Otherwise, it's a single scalar value - wrap it in a slice
95 *a = []any{v}
96 return nil
97 }
98
99 // StringSlice can be converted from either a single string or a slice of strings.
100 type StringSlice []string
101
102 // FromValue implements the custom converter interface used by UnmarshalStruct.
103 func (ss *StringSlice) FromValue(v any) error {
104 // Try string first
105 if strVal, ok := v.(string); ok {
106 *ss = []string{strVal}
107 return nil
108 }
109
110 // Try slice of interface{}
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 // IntSlice can be converted from either a single int or a slice of ints.
128 type IntSlice []int
129
130 // FromValue implements the custom converter interface used by UnmarshalStruct.
131 func (is *IntSlice) FromValue(v any) error {
132 // Try int first
133 if intVal, ok := v.(int); ok {
134 *is = []int{intVal}
135 return nil
136 }
137
138 // Try slice of interface{}
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 }