Skip to content

Commit

Permalink
Incorporate suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul2393 committed Dec 14, 2023
1 parent 0c5fe88 commit 5724768
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
23 changes: 7 additions & 16 deletions spanner/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2169,10 +2169,10 @@ func TestIntegration_BasicTypes(t *testing.T) {
}...)
}

for idx := 0; idx < 2; idx++ {
if idx == 1 {
UseNumberWithJSONDecoderEncoder()
defer testSetJSONProviderNumberConfig(false)
for _, withNumberConfigOption := range []bool{false, true} {
if withNumberConfigOption {
UseNumberWithJSONDecoderEncoder(withNumberConfigOption)
defer UseNumberWithJSONDecoderEncoder(!withNumberConfigOption)
}
// Write rows into table first using DML.
statements := make([]Statement, 0)
Expand Down Expand Up @@ -2224,15 +2224,15 @@ func TestIntegration_BasicTypes(t *testing.T) {
}
verifyDirectPathRemoteAddress(t)
want := test.wantWithDefaultConfig
if idx == 1 {
if withNumberConfigOption {
want = test.wantWithNumber
}
if want == nil {
want = test.val
}
gotp := reflect.New(reflect.TypeOf(want))
if err := row.Column(0, gotp.Interface()); err != nil {
t.Errorf("%d-%d: col:%v val:%#v, %v", idx, i, test.col, test.val, err)
t.Errorf("%v-%d: col:%v val:%#v, %v", withNumberConfigOption, i, test.col, test.val, err)
continue
}
got := reflect.Indirect(gotp).Interface()
Expand All @@ -2245,7 +2245,7 @@ func TestIntegration_BasicTypes(t *testing.T) {

// Check non-NaN cases.
if !testEqual(got, want) {
t.Errorf("%d-%d: col:%v val:%#v, got %#v, want %#v", idx, i, test.col, test.val, got, want)
t.Errorf("%v-%d: col:%v val:%#v, got %#v, want %#v", withNumberConfigOption, i, test.col, test.val, got, want)
continue
}
}
Expand Down Expand Up @@ -5538,12 +5538,3 @@ func checkCommonTagsGFELatency(t *testing.T, m map[tag.Key]string) {
t.Fatalf("Incorrect library version: %v", m[tagKeyLibVersion])
}
}

// helper method to enable json provider with useNumber flag, only for testing.
func testSetJSONProviderNumberConfig(useNumber bool) {
jsonProvider = jsoniter.Config{
EscapeHTML: true,
SortMapKeys: true, // Sort map keys to ensure deterministic output, to be consistent with encoding.
UseNumber: useNumber,
}.Froze()
}
23 changes: 10 additions & 13 deletions spanner/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"reflect"
"strconv"
"strings"
"sync"
"time"

"cloud.google.com/go/civil"
Expand Down Expand Up @@ -116,22 +115,20 @@ var (
jsonNullBytes = []byte("null")

jsonProvider = jsoniter.ConfigCompatibleWithStandardLibrary

once sync.Once
)

// UseNumberWithJSONDecoderEncoder specifies whether Cloud Spanner JSON numbers are decoded
// as Number (preserving precision) or float64 (risking loss).
// Defaults to float64, call this method for lossless precision.
// NOTE: This change affects all clients created by this library, both existing and future ones.
func UseNumberWithJSONDecoderEncoder() {
once.Do(func() {
jsonProvider = jsoniter.Config{
EscapeHTML: true,
SortMapKeys: true, // Sort map keys to ensure deterministic output, to be consistent with encoding.
UseNumber: true,
}.Froze()
})
// Defaults to the same behavior as the standard Go library, which means decoding to float64.
// Call this method to enable lossless precision.
// NOTE 1: Calling this method affects the behavior of all clients created by this library, both existing and future instances.
// NOTE 2: This method sets a global variable that is used by the client to encode/decode JSON numbers. Access to the global variable is not synchronized. You should only call this method when there are no goroutines encoding/decoding Cloud Spanner JSON values. It is recommended to only call this method during the initialization of your application, and preferably before you create any Cloud Spanner clients, and/or in tests when there are no queries being executed.
func UseNumberWithJSONDecoderEncoder(useNumber bool) {
jsonProvider = jsoniter.Config{
EscapeHTML: true,
SortMapKeys: true, // Sort map keys to ensure deterministic output, to be consistent with encoding.
UseNumber: useNumber,
}.Froze()
}

// Encoder is the interface implemented by a custom type that can be encoded to
Expand Down

0 comments on commit 5724768

Please sign in to comment.