Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(schema)!: rename IntegerStringKind and DecimalStringKind #21694

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions indexer/postgres/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion indexer/postgres/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 14 additions & 12 deletions schema/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,25 @@ 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,
// and exponential notation with a lowercase 'e' should be used for any numbers
// 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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down
104 changes: 52 additions & 52 deletions schema/kind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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},
Expand Down
6 changes: 3 additions & 3 deletions schema/testing/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions schema/testing/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions schema/testing/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion schema/testing/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions schema/testing/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down
Loading