From 47561a93f2e815ef7e07db6d21b9a4cc5309b4bb Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Fri, 13 Sep 2024 21:45:15 -0400 Subject: [PATCH] refactor(schema)!: rename IntegerStringKind and DecimalStringKind (#21694) Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com> --- indexer/postgres/column.go | 4 +- indexer/postgres/select.go | 2 +- schema/kind.go | 26 ++++----- schema/kind_test.go | 104 ++++++++++++++++++------------------ schema/testing/diff.go | 6 +-- schema/testing/diff_test.go | 18 +++---- schema/testing/field.go | 4 +- schema/testing/fmt.go | 2 +- schema/testing/fmt_test.go | 4 +- 9 files changed, 86 insertions(+), 84 deletions(-) diff --git a/indexer/postgres/column.go b/indexer/postgres/column.go index 01975e112177..0abbce874c95 100644 --- a/indexer/postgres/column.go +++ b/indexer/postgres/column.go @@ -88,9 +88,9 @@ func simpleColumnType(kind schema.Kind) string { return "BIGINT" case schema.Uint64Kind: return "NUMERIC" - case schema.IntegerStringKind: + case schema.IntegerKind: return "NUMERIC" - case schema.DecimalStringKind: + case schema.DecimalKind: return "NUMERIC" case schema.Float32Kind: return "REAL" diff --git a/indexer/postgres/select.go b/indexer/postgres/select.go index 46ef12d3f15c..03d560a5b76c 100644 --- a/indexer/postgres/select.go +++ b/indexer/postgres/select.go @@ -242,7 +242,7 @@ func (tm *objectIndexer) readCol(field schema.Field, value interface{}) (interfa str := nullStr.String switch field.Kind { - case schema.StringKind, schema.EnumKind, schema.IntegerStringKind, schema.DecimalStringKind: + case schema.StringKind, schema.EnumKind, schema.IntegerKind, schema.DecimalKind: return str, nil case schema.Uint8Kind: value, err := strconv.ParseUint(str, 10, 8) diff --git a/schema/kind.go b/schema/kind.go index 96f9a934842e..5eedee68c244 100644 --- a/schema/kind.go +++ b/schema/kind.go @@ -75,15 +75,17 @@ const ( // Canonically encoded values should include no leading zeros. Uint64Kind - // IntegerStringKind represents an arbitrary precision integer number. - // Go Encoding: string which matches the IntegerFormat regex + // IntegerKind represents an arbitrary precision integer number. + // Support for expressing the maximum bit precision of values will be added in the future. + // Go Encoding: string which matches the IntegerFormat regex (unstable, subject to change). // JSON Encoding: base10 integer string // Canonically encoded values should include no leading zeros. // Equality comparison with integers should be done using numerical equality rather // than string equality. - IntegerStringKind + IntegerKind - // DecimalStringKind represents an arbitrary precision decimal or integer number. + // DecimalKind represents an arbitrary precision decimal or integer number. + // Support for optionally limiting the precision may be added in the future. // Go Encoding: string which matches the DecimalFormat regex // JSON Encoding: base10 decimal string // Canonically encoded values should include no leading zeros or trailing zeros, @@ -91,7 +93,7 @@ const ( // with an absolute value less than or equal to 1e-6 or greater than or equal to 1e6. // Equality comparison with decimals should be done using numerical equality rather // than string equality. - DecimalStringKind + DecimalKind // BoolKind represents a boolean true or false value. // Go Encoding: bool @@ -194,9 +196,9 @@ func (t Kind) String() string { return "int64" case Uint64Kind: return "uint64" - case DecimalStringKind: + case DecimalKind: return "decimal" - case IntegerStringKind: + case IntegerKind: return "integer" case BoolKind: return "bool" @@ -276,13 +278,13 @@ func (t Kind) ValidateValueType(value interface{}) error { if !ok { return fmt.Errorf("expected uint64, got %T", value) } - case IntegerStringKind: + case IntegerKind: _, ok := value.(string) if !ok { return fmt.Errorf("expected string, got %T", value) } - case DecimalStringKind: + case DecimalKind: _, ok := value.(string) if !ok { return fmt.Errorf("expected string, got %T", value) @@ -355,11 +357,11 @@ func (t Kind) ValidateValue(value interface{}) error { return fmt.Errorf("expected string without null characters, got %s", value) } } - case IntegerStringKind: + case IntegerKind: if !integerRegex.Match([]byte(value.(string))) { return fmt.Errorf("expected base10 integer, got %s", value) } - case DecimalStringKind: + case DecimalKind: if !decimalRegex.Match([]byte(value.(string))) { return fmt.Errorf("expected decimal number, got %s", value) } @@ -391,7 +393,7 @@ var ( ) // KindForGoValue finds the simplest kind that can represent the given go value. It will not, however, -// return kinds such as IntegerStringKind, DecimalStringKind, AddressKind, or EnumKind which all can be +// return kinds such as IntegerKind, DecimalKind, AddressKind, or EnumKind which all can be // represented as strings. func KindForGoValue(value interface{}) Kind { switch value.(type) { diff --git a/schema/kind_test.go b/schema/kind_test.go index ec5766655943..262bb615153f 100644 --- a/schema/kind_test.go +++ b/schema/kind_test.go @@ -53,12 +53,12 @@ func TestKind_ValidateValueType(t *testing.T) { {kind: Int64Kind, value: int32(1), valid: false}, {kind: Uint64Kind, value: uint64(1), valid: true}, {kind: Uint64Kind, value: uint32(1), valid: false}, - {kind: IntegerStringKind, value: "1", valid: true}, - {kind: IntegerStringKind, value: int32(1), valid: false}, - {kind: DecimalStringKind, value: "1.0", valid: true}, - {kind: DecimalStringKind, value: "1", valid: true}, - {kind: DecimalStringKind, value: "1.1e4", valid: true}, - {kind: DecimalStringKind, value: int32(1), valid: false}, + {kind: IntegerKind, value: "1", valid: true}, + {kind: IntegerKind, value: int32(1), valid: false}, + {kind: DecimalKind, value: "1.0", valid: true}, + {kind: DecimalKind, value: "1", valid: true}, + {kind: DecimalKind, value: "1.1e4", valid: true}, + {kind: DecimalKind, value: int32(1), valid: false}, {kind: AddressKind, value: []byte("hello"), valid: true}, {kind: AddressKind, value: 1, valid: false}, {kind: BoolKind, value: true, valid: true}, @@ -115,54 +115,54 @@ func TestKind_ValidateValue(t *testing.T) { // strings with null characters are invalid {StringKind, string([]byte{1, 2, 0, 3}), false}, // check integer, decimal and json more thoroughly - {IntegerStringKind, "1", true}, - {IntegerStringKind, "0", true}, - {IntegerStringKind, "10", true}, - {IntegerStringKind, "-100", true}, - {IntegerStringKind, "1.0", false}, - {IntegerStringKind, "00", true}, // leading zeros are allowed - {IntegerStringKind, "001", true}, - {IntegerStringKind, "-01", true}, + {IntegerKind, "1", true}, + {IntegerKind, "0", true}, + {IntegerKind, "10", true}, + {IntegerKind, "-100", true}, + {IntegerKind, "1.0", false}, + {IntegerKind, "00", true}, // leading zeros are allowed + {IntegerKind, "001", true}, + {IntegerKind, "-01", true}, // 100 digits - {IntegerStringKind, "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", true}, + {IntegerKind, "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", true}, // more than 100 digits - {IntegerStringKind, "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", false}, - {IntegerStringKind, "", false}, - {IntegerStringKind, "abc", false}, - {IntegerStringKind, "abc100", false}, - {DecimalStringKind, "1.0", true}, - {DecimalStringKind, "0.0", true}, - {DecimalStringKind, "-100.075", true}, - {DecimalStringKind, "1002346.000", true}, - {DecimalStringKind, "0", true}, - {DecimalStringKind, "10", true}, - {DecimalStringKind, "-100", true}, - {DecimalStringKind, "1", true}, - {DecimalStringKind, "1.0e4", true}, - {DecimalStringKind, "1.0e-4", true}, - {DecimalStringKind, "1.0e+4", true}, - {DecimalStringKind, "1.0e", false}, - {DecimalStringKind, "1.0e4.0", false}, - {DecimalStringKind, "1.0e-4.0", false}, - {DecimalStringKind, "1.0e+4.0", false}, - {DecimalStringKind, "-1.0e-4", true}, - {DecimalStringKind, "-1.0e+4", true}, - {DecimalStringKind, "-1.0E4", true}, - {DecimalStringKind, "1E-9", true}, - {DecimalStringKind, "1E-99", true}, - {DecimalStringKind, "1E+9", true}, - {DecimalStringKind, "1E+99", true}, + {IntegerKind, "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", false}, + {IntegerKind, "", false}, + {IntegerKind, "abc", false}, + {IntegerKind, "abc100", false}, + {DecimalKind, "1.0", true}, + {DecimalKind, "0.0", true}, + {DecimalKind, "-100.075", true}, + {DecimalKind, "1002346.000", true}, + {DecimalKind, "0", true}, + {DecimalKind, "10", true}, + {DecimalKind, "-100", true}, + {DecimalKind, "1", true}, + {DecimalKind, "1.0e4", true}, + {DecimalKind, "1.0e-4", true}, + {DecimalKind, "1.0e+4", true}, + {DecimalKind, "1.0e", false}, + {DecimalKind, "1.0e4.0", false}, + {DecimalKind, "1.0e-4.0", false}, + {DecimalKind, "1.0e+4.0", false}, + {DecimalKind, "-1.0e-4", true}, + {DecimalKind, "-1.0e+4", true}, + {DecimalKind, "-1.0E4", true}, + {DecimalKind, "1E-9", true}, + {DecimalKind, "1E-99", true}, + {DecimalKind, "1E+9", true}, + {DecimalKind, "1E+99", true}, // 50 digits before and after the decimal point - {DecimalStringKind, "10000000000000000000000000000000000000000000000000.10000000000000000000000000000000000000000000000001", true}, + {DecimalKind, "10000000000000000000000000000000000000000000000000.10000000000000000000000000000000000000000000000001", true}, // too many digits before the decimal point - {DecimalStringKind, "10000000000000000000000000000000000000000000000000000000000000000000000000", false}, + {DecimalKind, "10000000000000000000000000000000000000000000000000000000000000000000000000", false}, // too many digits after the decimal point - {DecimalStringKind, "1.0000000000000000000000000000000000000000000000000000000000000000000000001", false}, + {DecimalKind, "1.0000000000000000000000000000000000000000000000000000000000000000000000001", false}, // exponent too big - {DecimalStringKind, "1E-999", false}, - {DecimalStringKind, "", false}, - {DecimalStringKind, "abc", false}, - {DecimalStringKind, "abc", false}, + {DecimalKind, "1E-999", false}, + {DecimalKind, "", false}, + {DecimalKind, "abc", false}, + {DecimalKind, "abc", false}, {JSONKind, json.RawMessage(`{"a":10}`), true}, {JSONKind, json.RawMessage("10"), true}, {JSONKind, json.RawMessage("10.0"), true}, @@ -204,8 +204,8 @@ func TestKind_String(t *testing.T) { {Uint32Kind, "uint32"}, {Int64Kind, "int64"}, {Uint64Kind, "uint64"}, - {IntegerStringKind, "integer"}, - {DecimalStringKind, "decimal"}, + {IntegerKind, "integer"}, + {DecimalKind, "decimal"}, {BoolKind, "bool"}, {TimeKind, "time"}, {DurationKind, "duration"}, @@ -280,8 +280,8 @@ func TestKindJSON(t *testing.T) { {Uint32Kind, `"uint32"`, false}, {Int64Kind, `"int64"`, false}, {Uint64Kind, `"uint64"`, false}, - {IntegerStringKind, `"integer"`, false}, - {DecimalStringKind, `"decimal"`, false}, + {IntegerKind, `"integer"`, false}, + {DecimalKind, `"decimal"`, false}, {BoolKind, `"bool"`, false}, {TimeKind, `"time"`, false}, {DurationKind, `"duration"`, false}, diff --git a/schema/testing/diff.go b/schema/testing/diff.go index 9cf320f4cf28..f8b53ca3ea06 100644 --- a/schema/testing/diff.go +++ b/schema/testing/diff.go @@ -80,7 +80,7 @@ func DiffFieldValues(field schema.Field, expected, actual any) string { // CompareKindValues compares the expected and actual values for the provided kind and returns true if they are equal, // false if they are not, and an error if the types are not valid for the kind. -// For IntegerStringKind and DecimalStringKind values, comparisons are made based on equality of the underlying numeric +// For IntegerKind and DecimalKind values, comparisons are made based on equality of the underlying numeric // values rather than their string encoding. func CompareKindValues(kind schema.Kind, expected, actual any) (bool, error) { if kind.ValidateValueType(expected) != nil { @@ -96,7 +96,7 @@ func CompareKindValues(kind schema.Kind, expected, actual any) (bool, error) { if !bytes.Equal(expected.([]byte), actual.([]byte)) { return false, nil } - case schema.IntegerStringKind: + case schema.IntegerKind: expectedInt := big.NewInt(0) expectedInt, ok := expectedInt.SetString(expected.(string), 10) if !ok { @@ -112,7 +112,7 @@ func CompareKindValues(kind schema.Kind, expected, actual any) (bool, error) { if expectedInt.Cmp(actualInt) != 0 { return false, nil } - case schema.DecimalStringKind: + case schema.DecimalKind: expectedDec, _, err := apd.NewFromString(expected.(string)) if err != nil { return false, fmt.Errorf("could not decode %v as a decimal: %w", expected, err) diff --git a/schema/testing/diff_test.go b/schema/testing/diff_test.go index 70bed0d1763e..e99ad3c7a98d 100644 --- a/schema/testing/diff_test.go +++ b/schema/testing/diff_test.go @@ -45,55 +45,55 @@ func TestCompareKindValues(t *testing.T) { equal: false, }, { - kind: schema.IntegerStringKind, + kind: schema.IntegerKind, expected: "a123", actual: "123", expectError: true, }, { - kind: schema.IntegerStringKind, + kind: schema.IntegerKind, expected: "123", actual: "123b", expectError: true, }, { - kind: schema.IntegerStringKind, + kind: schema.IntegerKind, expected: "123", actual: "1234", equal: false, }, { - kind: schema.IntegerStringKind, + kind: schema.IntegerKind, expected: "000123", actual: "123", equal: true, }, { - kind: schema.DecimalStringKind, + kind: schema.DecimalKind, expected: "abc", actual: "100.001", expectError: true, }, { - kind: schema.DecimalStringKind, + kind: schema.DecimalKind, expected: "1", actual: "b", expectError: true, }, { - kind: schema.DecimalStringKind, + kind: schema.DecimalKind, expected: "1.00001", actual: "100.001", equal: false, }, { - kind: schema.DecimalStringKind, + kind: schema.DecimalKind, expected: "1.00001e2", actual: "100.001", equal: true, }, { - kind: schema.DecimalStringKind, + kind: schema.DecimalKind, expected: "00000100.00100000", actual: "100.001", equal: true, diff --git a/schema/testing/field.go b/schema/testing/field.go index a090c38395f8..b0ad53a8820f 100644 --- a/schema/testing/field.go +++ b/schema/testing/field.go @@ -93,9 +93,9 @@ func baseFieldValue(field schema.Field, typeSet schema.TypeSet) *rapid.Generator return rapid.Float32().AsAny() case schema.Float64Kind: return rapid.Float64().AsAny() - case schema.IntegerStringKind: + case schema.IntegerKind: return rapid.StringMatching(schema.IntegerFormat).AsAny() - case schema.DecimalStringKind: + case schema.DecimalKind: return rapid.StringMatching(schema.DecimalFormat).AsAny() case schema.BoolKind: return rapid.Bool().AsAny() diff --git a/schema/testing/fmt.go b/schema/testing/fmt.go index e486adfeb8b7..a7c2852b34ea 100644 --- a/schema/testing/fmt.go +++ b/schema/testing/fmt.go @@ -38,7 +38,7 @@ func fmtValue(kind schema.Kind, value any) string { switch kind { case schema.BytesKind, schema.AddressKind: return fmt.Sprintf("0x%x", value) - case schema.DecimalStringKind, schema.IntegerStringKind: + case schema.DecimalKind, schema.IntegerKind: // we need to normalize decimal & integer strings to remove leading & trailing zeros d, _, err := apd.NewFromString(value.(string)) if err != nil { diff --git a/schema/testing/fmt_test.go b/schema/testing/fmt_test.go index 02cef05d5f1b..49d539a9db6a 100644 --- a/schema/testing/fmt_test.go +++ b/schema/testing/fmt_test.go @@ -36,8 +36,8 @@ func TestObjectKeyString(t *testing.T) { KeyFields: []schema.Field{ {Name: "Bz", Kind: schema.BytesKind}, {Name: "Addr", Kind: schema.AddressKind}, - {Name: "Dec", Kind: schema.DecimalStringKind}, - {Name: "Int", Kind: schema.IntegerStringKind}, + {Name: "Dec", Kind: schema.DecimalKind}, + {Name: "Int", Kind: schema.IntegerKind}, }, }, key: []interface{}{