feat: attribute typing #40

Merged
0xmuralik merged 24 commits from murali/attribute-typing into main 2022-11-15 06:21:14 +00:00
2 changed files with 22 additions and 1 deletions
Showing only changes of commit e75b5eb807 - Show all commits

View File

@ -66,6 +66,7 @@ func (suite *KeeperTestSuite) TestGrpcGetRecordLists() {
{ {
Key: "type", Key: "type",
Value: &nameservicetypes.QueryListRecordsRequest_ValueInput{ Value: &nameservicetypes.QueryListRecordsRequest_ValueInput{
Type: "string",
String_: "WebsiteRegistrationRecord", String_: "WebsiteRegistrationRecord",
}, },
}, },

View File

@ -135,7 +135,8 @@ func (k Keeper) ListRecords(ctx sdk.Context) []types.Record {
func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*types.QueryListRecordsRequest_KeyValueInput, all bool) ([]types.Record, error) { func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*types.QueryListRecordsRequest_KeyValueInput, all bool) ([]types.Record, error) {
resultRecordIds := []string{} resultRecordIds := []string{}
for i, attr := range attributes { for i, attr := range attributes {
attributeIndex := GetAttributesIndexKey(attr.Key, attr.Value) val := getAttributeValue(attr.Value)
attributeIndex := GetAttributesIndexKey(attr.Key, val)
recordIds, err := k.GetAttributeMapping(ctx, attributeIndex) recordIds, err := k.GetAttributeMapping(ctx, attributeIndex)
if err != nil { if err != nil {
return nil, err return nil, err
@ -161,6 +162,25 @@ func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*types.Query
return records, nil return records, nil
} }
func getAttributeValue(input *types.QueryListRecordsRequest_ValueInput) interface{} {
if input.Type == "int" {
return input.GetInt()
}
if input.Type == "float" {
return input.GetFloat()
}
if input.Type == "string" {
return input.GetString_()
}
if input.Type == "boolean" {
return input.GetBoolean()
}
if input.Type == "reference" {
return input.GetReference().GetId()
}
return nil
}
func getIntersection(a []string, b []string) []string { func getIntersection(a []string, b []string) []string {
result := []string{} result := []string{}
if len(a) < len(b) { if len(a) < len(b) {