Skip to content

Parsing Call function of client #80

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

Closed
DylanVerstraete opened this issue May 7, 2019 · 2 comments
Closed

Parsing Call function of client #80

DylanVerstraete opened this issue May 7, 2019 · 2 comments
Labels
question Further information is requested

Comments

@DylanVerstraete
Copy link

I'm having issues parsing the result of the Call function of this client.

In the Tarantool console i'm getting this response from a simple select:

unix/:/var/run/tarantool/tarantool.sock> select * from info
---
- metadata:
  - name: CONSENSUSCHANGEID
    type: string
  - name: HEIGHT
    type: integer
  rows:
  - ['initial', 0]

While in the client after creating a function with Lua, so I can later reuse this logic.
I'm getting this as response from the client.Call function.

res.Data
<[]interface {}> (length: 1, cap: 1)
[0]:<interface {}>
data:<[]interface {}> (length: 1, cap: 1)
[0]:<interface {}>
data:<map[interface {}]interface {}> (length: 2)
<interface {}>:<interface {}>
data:<[]interface {}> (length: 2, cap: 2)
[0]:<interface {}>
data:<map[interface {}]interface {}> (length: 2)
<interface {}>:<interface {}>
data:"CONSENSUSCHANGEID"
[1]:<interface {}>
data:<map[interface {}]interface {}> (length: 2)
<interface {}>:<interface {}>
data:"HEIGHT"

It's giving me an array of interfaces of array of interfaces and so on.. to eventually find only the metadata column names in there. Row data is not present.

After printing them with fmt.Print its giving me following correct output:
[[map[metadata:[map[name:CONSENSUSCHANGEID type:string] map[name:HEIGHT type:integer]] rows:[[initial 0]]]]]

So this means the data of the rows is somewhere in there and magically parsed by fmt.Print. Can someone please elaborate how one could parse this into a useable data form?

@ivankprod
Copy link

You can parse the response like this:

respData := resp.Data

if len(respData) > 1 {
	respError, _ := respData[1].([]interface{})[0].(string)
	if respError != "" {
		return nil, fmt.Errorf("SQL error: %s", respError)
	}
}

respParsed, ok := respData[0].([]interface{})[0].(map[interface{}]interface{})["rows"].([]interface{})
if !ok {
	return nil, fmt.Errorf("error during parsing SQL response")
}

then range over the respParsed with:

for _, v := range respParsed {
	data := v.([]interface{})
	// your logic
}

@Totktonada
Copy link
Member

NB: We plan to implement a native SQL support within #62.

The question looks answered, so I'll close it. Feel free to reopen if you still experience a problem around parsing of an SQL response.

@Totktonada Totktonada added the question Further information is requested label Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants