Skip to content

Commit cdf1f9b

Browse files
committed
Merge pull request #46 from pendo-io/parse-prim-val-minor-cleanup
Minor cleanups/fixes for the new Parse functions
2 parents 6c8c6a8 + 05fef7b commit cdf1f9b

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

parser.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -449,25 +449,20 @@ func GetBoolean(data []byte, keys ...string) (val bool, err error) {
449449

450450
// ParseBoolean parses a Boolean ValueType into a Go bool (not particularly useful, but here for completeness)
451451
func ParseBoolean(b []byte) (bool, error) {
452-
switch b[0] {
453-
case 't':
452+
switch {
453+
case bytes.Equal(b, trueLiteral):
454454
return true, nil
455-
case 'f':
455+
case bytes.Equal(b, falseLiteral):
456456
return false, nil
457457
default:
458458
return false, MalformedValueError
459459
}
460460
}
461461

462-
// ParseString parses a String ValueType into a Go []byte (the main parsing work is unescaping the JSON string)
463-
func parseStringAsBytes(b []byte) ([]byte, error) {
464-
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings (hopefully; the Go compiler might just always kick stackbuf[:] into the heap)
465-
return Unescape(b, stackbuf[:])
466-
}
467-
468462
// ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string)
469463
func ParseString(b []byte) (string, error) {
470-
if bU, err := parseStringAsBytes(b); err != nil {
464+
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
465+
if bU, err := Unescape(b, stackbuf[:]); err != nil {
471466
return "", nil
472467
} else {
473468
return string(bU), nil
@@ -483,11 +478,11 @@ func ParseFloat(b []byte) (float64, error) {
483478
}
484479
}
485480

486-
// ParseNumber parses a Number ValueType into a Go float64
481+
// ParseInt parses a Number ValueType into a Go int64
487482
func ParseInt(b []byte) (int64, error) {
488-
if v, err := parseInt(b); !err {
483+
if v, ok := parseInt(b); !ok {
489484
return 0, MalformedValueError
490485
} else {
491486
return v, nil
492487
}
493-
}
488+
}

0 commit comments

Comments
 (0)