Skip to content

Commit 1ba7210

Browse files
authored
Fix quoting on yaml marshaled uint128 values (#149)
1 parent f3a1a77 commit 1ba7210

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

pkg/tls/tls.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,11 @@ func GenerateAllCerts(outDir string, privateCACert *x509.Certificate, privateCAK
9494
return errors.New("you must provide the CA cert and key if providing a CA, or set both to nil and a new CA will be generated")
9595
} else {
9696
// Must have non-nil values for both, so ensure the cert and key match
97-
if ! CertMatchesPrivateKey(privateCACert, privateCAKey) {
97+
if !CertMatchesPrivateKey(privateCACert, privateCAKey) {
9898
return errors.New("provided private CA Cert and Key do not match")
9999
}
100100
}
101101

102-
103102
for _, node := range publicNodeNames {
104103
_, _, err = GenerateCASignedCert(chiaCACert, chiaCAKey, path.Join(outDir, node, fmt.Sprintf("public_%s", node)))
105104
if err != nil {

pkg/types/uint128.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
"math"
3232
"math/big"
3333
"math/bits"
34+
35+
"gopkg.in/yaml.v3"
3436
)
3537

3638
// Uint128Zero is a zero-valued uint128.
@@ -494,7 +496,12 @@ func (u Uint128) MarshalJSON() ([]byte, error) {
494496

495497
// MarshalYAML marshals the uint128 value to yaml
496498
func (u Uint128) MarshalYAML() (interface{}, error) {
497-
return u.String(), nil
499+
// Return a YAML node with ScalarNode type to ensure the value is treated as a plain number and not quoted
500+
return &yaml.Node{
501+
Kind: yaml.ScalarNode,
502+
Value: u.Big().String(), // Use the string value without quotes
503+
Style: yaml.TaggedStyle, // Prevent quoting
504+
}, nil
498505
}
499506

500507
// FitsInUint64 returns true if the value of the Uint128 will fit in Uint64

pkg/types/uint128_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"math/big"
3434
"testing"
3535

36+
"github.com/stretchr/testify/assert"
3637
"gopkg.in/yaml.v3"
3738

3839
"github.com/chia-network/go-chia-libs/pkg/types"
@@ -543,3 +544,31 @@ func TestUint128_UnmarshalYAML_Null(t *testing.T) {
543544
t.Error("`null` did not unmarshal as zero")
544545
}
545546
}
547+
548+
func TestNewUint128_MarshalJSON(t *testing.T) {
549+
num := types.Uint128From64(12345)
550+
out, err := json.Marshal(num)
551+
assert.NoError(t, err)
552+
assert.Equal(t, "12345", string(out))
553+
554+
bigInt := new(big.Int)
555+
bigInt.SetString("18446744073709551616", 10)
556+
num = types.Uint128FromBig(bigInt)
557+
out, err = json.Marshal(num)
558+
assert.NoError(t, err)
559+
assert.Equal(t, "18446744073709551616", string(out))
560+
}
561+
562+
func TestNewUint128_MarshalYAML(t *testing.T) {
563+
num := types.Uint128From64(12345)
564+
out, err := yaml.Marshal(num)
565+
assert.NoError(t, err)
566+
assert.Equal(t, "12345\n", string(out))
567+
568+
bigInt := new(big.Int)
569+
bigInt.SetString("18446744073709551616", 10)
570+
num = types.Uint128FromBig(bigInt)
571+
out, err = yaml.Marshal(num)
572+
assert.NoError(t, err)
573+
assert.Equal(t, "18446744073709551616\n", string(out))
574+
}

0 commit comments

Comments
 (0)