Skip to content

Commit c6f3114

Browse files
authored
Merge pull request #197 from kaleido-io/getBigInt
add GetBigInt() function
2 parents fea0ae0 + d167e23 commit c6f3114

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

pkg/config/config.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2023 Kaleido, Inc.
1+
// Copyright © 2025 Kaleido, Inc.
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//
@@ -22,6 +22,7 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"math"
25+
"math/big"
2526
"reflect"
2627
"sort"
2728
"strings"
@@ -113,6 +114,7 @@ type Section interface {
113114
GetObject(key string) fftypes.JSONObject
114115
GetObjectArray(key string) fftypes.JSONObjectArray
115116
Get(key string) interface{}
117+
GetBigInt(key string) *big.Int
116118
}
117119

118120
// ArraySection represents an array of options at a particular layer in the config.
@@ -521,6 +523,30 @@ func (c *configSection) GetByteSize(key string) int64 {
521523
return fftypes.ParseToByteSize(viper.GetString(c.prefixKey(key)))
522524
}
523525

526+
// GetBigInt gets a configuration big integer.
527+
// Uses base 0 so that the string can be in decimal, hexadecimal (0x...), octal (0o...), or binary (0b...) formats.
528+
// See math/big.Int.SetString docs for details.
529+
func GetBigInt(key RootKey) *big.Int {
530+
return root.GetBigInt(string(key))
531+
}
532+
func (c *configSection) GetBigInt(key string) *big.Int {
533+
keysMutex.Lock()
534+
defer keysMutex.Unlock()
535+
bigIntStr := viper.GetString(c.prefixKey(key))
536+
valueStr := strings.TrimSpace(bigIntStr)
537+
if valueStr == "" {
538+
return nil
539+
}
540+
541+
value := &big.Int{}
542+
_, ok := value.SetString(valueStr, 0)
543+
if !ok {
544+
log.L(context.Background()).Warnf("Unable to parse string %s into big.Int", valueStr)
545+
return nil
546+
}
547+
return value
548+
}
549+
524550
// GetUint gets a configuration uint
525551
func GetUint(key RootKey) uint {
526552
return root.GetUint(string(key))

pkg/config/config_test.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2023 Kaleido, Inc.
1+
// Copyright © 2025 Kaleido, Inc.
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//
@@ -19,6 +19,7 @@ package config
1919
import (
2020
"context"
2121
"fmt"
22+
"math/big"
2223
"os"
2324
"path"
2425
"strings"
@@ -522,3 +523,75 @@ func TestSetEnvPrefix(t *testing.T) {
522523

523524
assert.Equal(t, "two", cfg.GetString("conf"))
524525
}
526+
527+
func TestGetBigInt(t *testing.T) {
528+
defer RootConfigReset()
529+
530+
// Test valid decimal
531+
key1 := AddRootKey("bigint.key1")
532+
Set(key1, "12345678901234567890")
533+
result1 := GetBigInt(key1)
534+
require.NotNil(t, result1)
535+
expected1 := big.NewInt(0)
536+
expected1.SetString("12345678901234567890", 10)
537+
assert.Equal(t, expected1, result1)
538+
539+
// Test valid hex
540+
key2 := AddRootKey("bigint.key2")
541+
Set(key2, "0xFF")
542+
assert.Equal(t, big.NewInt(255), GetBigInt(key2))
543+
544+
// Test valid octal
545+
key3 := AddRootKey("bigint.key3")
546+
Set(key3, "0777")
547+
assert.Equal(t, big.NewInt(511), GetBigInt(key3))
548+
549+
// Test very large value
550+
key4 := AddRootKey("bigint.key4")
551+
Set(key4, "999999999999999999999999999999")
552+
result4 := GetBigInt(key4)
553+
require.NotNil(t, result4)
554+
expected4 := big.NewInt(0)
555+
expected4.SetString("999999999999999999999999999999", 10)
556+
assert.Equal(t, expected4, result4)
557+
558+
// Test trimming whitespace
559+
key5 := AddRootKey("bigint.key5")
560+
Set(key5, " 999 ")
561+
assert.Equal(t, big.NewInt(999), GetBigInt(key5))
562+
563+
// Test zero value
564+
key6 := AddRootKey("bigint.key6")
565+
Set(key6, "0")
566+
assert.Equal(t, big.NewInt(0), GetBigInt(key6))
567+
568+
// Test negative value
569+
key7 := AddRootKey("bigint.key7")
570+
Set(key7, "-123456789")
571+
assert.Equal(t, big.NewInt(-123456789), GetBigInt(key7))
572+
573+
// Test empty string returns nil
574+
key8 := AddRootKey("bigint.key8")
575+
Set(key8, "")
576+
assert.Nil(t, GetBigInt(key8))
577+
578+
// Test whitespace only returns nil
579+
key9 := AddRootKey("bigint.key9")
580+
Set(key9, " ")
581+
assert.Nil(t, GetBigInt(key9))
582+
583+
// Test invalid string returns nil
584+
key10 := AddRootKey("bigint.key10")
585+
Set(key10, "not a number")
586+
assert.Nil(t, GetBigInt(key10))
587+
588+
// Test with config section
589+
section := RootSection("bigintsection")
590+
section.AddKnownKey("value")
591+
section.Set("value", "98765432109876543210")
592+
resultSection := section.GetBigInt("value")
593+
require.NotNil(t, resultSection)
594+
expectedSection := big.NewInt(0)
595+
expectedSection.SetString("98765432109876543210", 10)
596+
assert.Equal(t, expectedSection, resultSection)
597+
}

0 commit comments

Comments
 (0)