MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / stringToByteArrayHook

Function stringToByteArrayHook

pkg/config/hooks.go:235–254  ·  view source on GitHub ↗
(f reflect.Type, t reflect.Type, data any)

Source from the content-addressed store, hash-verified

233}
234
235func stringToByteArrayHook(f reflect.Type, t reflect.Type, data any) (any, error) {
236 if f.Kind() != reflect.String || t.Kind() != reflect.Array || t.Elem().Kind() != reflect.Uint8 {
237 return data, nil
238 }
239
240 str := strings.TrimPrefix(data.(string), "0x")
241 slice, err := hex.DecodeString(str)
242 if err != nil {
243 return nil, fmt.Errorf("could not decode hex: %w", err)
244 }
245 if len(slice) != t.Len() {
246 return nil, fmt.Errorf("invalid length: expected %d, got %d", t.Len(), len(slice))
247 }
248
249 rv := reflect.New(t).Elem()
250 for i, v := range slice {
251 rv.Index(i).SetUint(uint64(v))
252 }
253 return rv.Interface(), nil
254}
255
256func stringToTimeDurationPointerHook(f reflect.Type, t reflect.Type, data any) (any, error) {
257 d := time.Duration(0)

Callers

nothing calls this directly

Calls 4

TrimPrefixMethod · 0.80
ErrorfMethod · 0.65
NewMethod · 0.65
LenMethod · 0.45

Tested by

no test coverage detected