refactor(schema)!: move bech32 address prefixes to a higher level (#21026)
This commit is contained in:
parent
d80eba9bf9
commit
2c236af94d
@ -13,10 +13,6 @@ type Field struct {
|
||||
// Nullable indicates whether null values are accepted for the field. Key fields CANNOT be nullable.
|
||||
Nullable bool
|
||||
|
||||
// AddressPrefix is the address prefix of the field's kind, currently only used for Bech32AddressKind.
|
||||
// TODO: in a future update, stricter criteria and validation for address prefixes should be added.
|
||||
AddressPrefix string
|
||||
|
||||
// EnumDefinition is the definition of the enum type and is only valid when Kind is EnumKind.
|
||||
// The same enum types can be reused in the same module schema, but they always must contain
|
||||
// the same values for the same enum name. This possibly introduces some duplication of
|
||||
@ -36,13 +32,6 @@ func (c Field) Validate() error {
|
||||
return fmt.Errorf("invalid field kind for %q: %v", c.Name, err) //nolint:errorlint // false positive due to using go1.12
|
||||
}
|
||||
|
||||
// address prefix only valid with Bech32AddressKind
|
||||
if c.Kind == Bech32AddressKind && c.AddressPrefix == "" {
|
||||
return fmt.Errorf("missing address prefix for field %q", c.Name)
|
||||
} else if c.Kind != Bech32AddressKind && c.AddressPrefix != "" {
|
||||
return fmt.Errorf("address prefix is only valid for field %q with type Bech32AddressKind", c.Name)
|
||||
}
|
||||
|
||||
// enum definition only valid with EnumKind
|
||||
if c.Kind == EnumKind {
|
||||
if err := c.EnumDefinition.Validate(); err != nil {
|
||||
|
||||
@ -35,23 +35,6 @@ func TestField_Validate(t *testing.T) {
|
||||
},
|
||||
errContains: "invalid field kind",
|
||||
},
|
||||
{
|
||||
name: "missing address prefix",
|
||||
field: Field{
|
||||
Name: "field1",
|
||||
Kind: Bech32AddressKind,
|
||||
},
|
||||
errContains: "missing address prefix",
|
||||
},
|
||||
{
|
||||
name: "address prefix with non-Bech32AddressKind",
|
||||
field: Field{
|
||||
Name: "field1",
|
||||
Kind: StringKind,
|
||||
AddressPrefix: "prefix",
|
||||
},
|
||||
errContains: "address prefix is only valid for field \"field1\" with type Bech32AddressKind",
|
||||
},
|
||||
{
|
||||
name: "invalid enum definition",
|
||||
field: Field{
|
||||
|
||||
@ -72,10 +72,10 @@ const (
|
||||
// Float64Kind is a float64 type and values of this type must be of the go type float64.
|
||||
Float64Kind
|
||||
|
||||
// Bech32AddressKind is a bech32 address type and values of this type must be of the go type []byte.
|
||||
// Fields of this type are expected to set the AddressPrefix field in the field definition to the
|
||||
// bech32 address prefix so that indexers can properly convert them to strings.
|
||||
Bech32AddressKind
|
||||
// AddressKind represents an account address and must be of type []byte. Addresses usually have a
|
||||
// human-readable rendering, such as bech32, and tooling should provide a way for apps to define a
|
||||
// string encoder for friendly user-facing display.
|
||||
AddressKind
|
||||
|
||||
// EnumKind is an enum type and values of this type must be of the go type string.
|
||||
// Fields of this type are expected to set the EnumDefinition field in the field definition to the enum
|
||||
@ -150,7 +150,7 @@ func (t Kind) String() string {
|
||||
return "float32"
|
||||
case Float64Kind:
|
||||
return "float64"
|
||||
case Bech32AddressKind:
|
||||
case AddressKind:
|
||||
return "bech32address"
|
||||
case EnumKind:
|
||||
return "enum"
|
||||
@ -254,7 +254,7 @@ func (t Kind) ValidateValueType(value interface{}) error {
|
||||
if !ok {
|
||||
return fmt.Errorf("expected float64, got %T", value)
|
||||
}
|
||||
case Bech32AddressKind:
|
||||
case AddressKind:
|
||||
_, ok := value.([]byte)
|
||||
if !ok {
|
||||
return fmt.Errorf("expected []byte, got %T", value)
|
||||
@ -321,7 +321,7 @@ var (
|
||||
)
|
||||
|
||||
// KindForGoValue finds the simplest kind that can represent the given go value. It will not, however,
|
||||
// return kinds such as IntegerStringKind, DecimalStringKind, Bech32AddressKind, or EnumKind which all can be
|
||||
// return kinds such as IntegerStringKind, DecimalStringKind, AddressKind, or EnumKind which all can be
|
||||
// represented as strings.
|
||||
func KindForGoValue(value interface{}) Kind {
|
||||
switch value.(type) {
|
||||
|
||||
@ -59,8 +59,8 @@ func TestKind_ValidateValueType(t *testing.T) {
|
||||
{kind: DecimalStringKind, value: "1", valid: true},
|
||||
{kind: DecimalStringKind, value: "1.1e4", valid: true},
|
||||
{kind: DecimalStringKind, value: int32(1), valid: false},
|
||||
{kind: Bech32AddressKind, value: []byte("hello"), valid: true},
|
||||
{kind: Bech32AddressKind, value: 1, valid: false},
|
||||
{kind: AddressKind, value: []byte("hello"), valid: true},
|
||||
{kind: AddressKind, value: 1, valid: false},
|
||||
{kind: BoolKind, value: true, valid: true},
|
||||
{kind: BoolKind, value: false, valid: true},
|
||||
{kind: BoolKind, value: 1, valid: false},
|
||||
@ -213,7 +213,7 @@ func TestKind_String(t *testing.T) {
|
||||
{Float64Kind, "float64"},
|
||||
{JSONKind, "json"},
|
||||
{EnumKind, "enum"},
|
||||
{Bech32AddressKind, "bech32address"},
|
||||
{AddressKind, "bech32address"},
|
||||
{InvalidKind, "invalid(0)"},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user