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

feat: PRT - Add support for arrays in the parsing function ParseCanonical #1709

Closed
wants to merge 2 commits into from
Closed
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
66 changes: 52 additions & 14 deletions protocol/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,20 @@ func parseByArg(rpcInput RPCInput, input []string, dataSource int) ([]interface{
// }
//
// should output an interface array with "wanted result" in first index 0
//
// also supports array of objects, for example, on the given input ["a","b","0", "c"] and the canonical object
//
// {
// "a": {
// "b": [
// {
// "c": "wanted result"
// }
// ]
// }
// }
//
// should output an interface array with "wanted result" in first index 0
func parseCanonical(rpcInput RPCInput, input []string, dataSource int) ([]interface{}, error) {
unmarshalledData, err := getDataToParse(rpcInput, dataSource)
if err != nil {
Expand All @@ -575,21 +589,45 @@ func parseCanonical(rpcInput RPCInput, input []string, dataSource int) ([]interf
blockContainer := unmarshalledDataTyped[param_index]
for _, key := range input[1:] {
// type assertion for blockcontainer
if blockContainer, ok := blockContainer.(map[string]interface{}); !ok {
return nil, utils.LavaFormatWarning("invalid parser input format, blockContainer is not map[string]interface{}", ValueNotSetError,
utils.LogAttr("params", rpcInput.GetParams()),
utils.LogAttr("method", rpcInput.GetMethod()),
utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)),
utils.LogAttr("key", key),
utils.LogAttr("unmarshaledDataTyped", unmarshalledDataTyped),
)
}
switch blockContainerTyped := blockContainer.(type) {
case map[string]interface{}:
// assertion for key
if container, ok := blockContainerTyped[key]; ok {
blockContainer = container
} else {
return nil, utils.LavaFormatWarning("invalid parser input format, blockContainer does not have the field searched inside", ValueNotSetError,
utils.LogAttr("params", rpcInput.GetParams()),
utils.LogAttr("method", rpcInput.GetMethod()),
utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)),
utils.LogAttr("key", key),
utils.LogAttr("unmarshaledDataTyped", unmarshalledDataTyped),
)
}
case []interface{}:
param_index, err := strconv.ParseUint(key, 10, 32)
if err != nil {
return nil, utils.LavaFormatWarning("invalid parser input format, blockContainer is a slice, but given key is not an index", nil,
utils.LogAttr("params", rpcInput.GetParams()),
utils.LogAttr("method", rpcInput.GetMethod()),
utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)),
utils.LogAttr("key", key),
utils.LogAttr("unmarshaledDataTyped", unmarshalledDataTyped),
)
}
if uint64(len(blockContainerTyped)) <= param_index {
return nil, utils.LavaFormatWarning("invalid parser input format, blockContainer is a slice, but given index is larger than the slice length", ValueNotSetError,
utils.LogAttr("params", rpcInput.GetParams()),
utils.LogAttr("method", rpcInput.GetMethod()),
utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)),
utils.LogAttr("index", param_index),
utils.LogAttr("length", len(blockContainerTyped)),
utils.LogAttr("unmarshaledDataTyped", unmarshalledDataTyped),
)
}

// assertion for key
if container, ok := blockContainer.(map[string]interface{})[key]; ok {
blockContainer = container
} else {
return nil, utils.LavaFormatWarning("invalid parser input format, blockContainer does not have the field searched inside", ValueNotSetError,
blockContainer = blockContainerTyped[param_index]
default:
return nil, utils.LavaFormatWarning("invalid parser input format, blockContainer is not map[string]interface{} nor []interface{}", ValueNotSetError,
utils.LogAttr("params", rpcInput.GetParams()),
utils.LogAttr("method", rpcInput.GetMethod()),
utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)),
Expand Down
Loading