Skip to content

Commit

Permalink
[ASCII-2262] Extend config.UnmarshalKey unit tests around primitives (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-hanna authored Oct 1, 2024
1 parent 7082e7f commit 9ce8a82
Show file tree
Hide file tree
Showing 2 changed files with 666 additions and 39 deletions.
18 changes: 17 additions & 1 deletion pkg/config/structure/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"reflect"
"slices"
"strconv"
"strings"
"unicode"
"unicode/utf8"
Expand Down Expand Up @@ -249,6 +250,8 @@ func (n *leafNodeImpl) ChildrenKeys() ([]string, error) {
func (n *leafNodeImpl) GetBool() (bool, error) {
if n.val.Kind() == reflect.Bool {
return n.val.Bool(), nil
} else if n.val.Kind() == reflect.Int {
return n.val.Int() != 0, nil
} else if n.val.Kind() == reflect.String {
return convertToBool(n.val.String())
}
Expand Down Expand Up @@ -283,7 +286,20 @@ func (n *leafNodeImpl) GetFloat() (float64, error) {

// GetString returns the scalar as a string, or an error otherwise
func (n *leafNodeImpl) GetString() (string, error) {
if n.val.Kind() == reflect.String {
switch n.val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
stringVal := strconv.FormatInt(n.val.Int(), 10)
return stringVal, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
stringVal := strconv.FormatUint(n.val.Uint(), 10)
return stringVal, nil
case reflect.Float32:
stringVal := strconv.FormatFloat(n.val.Float(), 'f', -1, 32)
return stringVal, nil
case reflect.Float64:
stringVal := strconv.FormatFloat(n.val.Float(), 'f', -1, 64)
return stringVal, nil
case reflect.String:
return n.val.String(), nil
}
return "", newConversionError(n.val, "string")
Expand Down
Loading

0 comments on commit 9ce8a82

Please sign in to comment.