-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.go
More file actions
37 lines (32 loc) · 992 Bytes
/
bytes.go
File metadata and controls
37 lines (32 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package puzzle
import (
"encoding/base32"
"encoding/base64"
"encoding/hex"
"fmt"
)
var BytesConverter = newConverter(bytesFromString)
func bytesFromString(entry *Entry[[]byte], stringValue string) error {
var value []byte
var err error
switch entry.Metadata.Format {
case "base64":
value, err = base64.StdEncoding.DecodeString(stringValue)
if err != nil {
return &InvalidValueError{Key: entry.Key, Value: stringValue, Err: fmt.Errorf("error while decoding as base64: %v", err)}
}
case "base32":
value, err = base32.StdEncoding.DecodeString(stringValue)
if err != nil {
return &InvalidValueError{Key: entry.Key, Value: stringValue, Err: fmt.Errorf("error while decoding as base32: %v", err)}
}
default: // hex
value, err = hex.DecodeString(stringValue)
if err != nil {
return &InvalidValueError{Key: entry.Key, Value: stringValue, Err: fmt.Errorf("error while decoding as hex: %v", err)}
}
}
*entry.ValueP = value
entry.Value = value
return nil
}