mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
terraform/provider: keep large JSON numbers exact when normalizing
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fae64ed81e
commit
80aeefaec0
2 files changed
+37
-4
No files matched your search
@@ -503,3 +503,22 @@ func TestIAMErrorHelpers(t *testing.T) {
|
||||
t.Fatalf("expected ServiceFailure to be retryable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONNormalizersPreserveLargeNumbers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
raw := `{"current-snapshot-id":9007199254740993}`
|
||||
normalized, err := normalizeJSONString(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("normalize: %v", err)
|
||||
}
|
||||
if normalized != raw {
|
||||
t.Fatalf("normalized = %s, want %s", normalized, raw)
|
||||
}
|
||||
if policiesSemanticallyEqual(raw, `{"current-snapshot-id":9007199254740992}`) {
|
||||
t.Fatal("expected distinct integers to compare different")
|
||||
}
|
||||
if _, err := normalizeJSONString(`{"a":1} {"b":2}`); err == nil {
|
||||
t.Fatal("expected trailing JSON to be rejected")
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,26 @@ package seaweedfs
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func normalizeJSONString(raw string) (string, error) {
|
||||
func decodeJSONValue(raw string) (any, error) {
|
||||
decoder := json.NewDecoder(strings.NewReader(raw))
|
||||
decoder.UseNumber()
|
||||
var value any
|
||||
if err := json.Unmarshal([]byte(raw), &value); err != nil {
|
||||
if err := decoder.Decode(&value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := decoder.Token(); err != io.EOF {
|
||||
return nil, errors.New("invalid JSON: unexpected data after the top-level value")
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func normalizeJSONString(raw string) (string, error) {
|
||||
value, err := decodeJSONValue(raw)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -31,8 +45,8 @@ func policiesSemanticallyEqual(a string, b string) bool {
|
||||
}
|
||||
|
||||
func normalizePolicyDocument(raw string) (string, error) {
|
||||
var value any
|
||||
if err := json.Unmarshal([]byte(raw), &value); err != nil {
|
||||
value, err := decodeJSONValue(raw)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user