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

Array indexes are changed #16

Closed
nekufa opened this issue Dec 5, 2016 · 4 comments
Closed

Array indexes are changed #16

nekufa opened this issue Dec 5, 2016 · 4 comments

Comments

@nekufa
Copy link
Member

nekufa commented Dec 5, 2016

I use this code to calculate something in lua grouped by integer key

var_dump($client->evaluate('
    local result = {}
    result[8] = 15
    return result
')->getData());

Running the code will provide this result.
Please note that index was decreased by one.

array(1) {
  [0]=>
  array(8) {
    [0]=>
    NULL
    [1]=>
    NULL
    [2]=>
    NULL
    [3]=>
    NULL
    [4]=>
    NULL
    [5]=>
    NULL
    [6]=>
    NULL
    [7]=>
    int(15)
  }
}

I found workaround - initialize with non-integer key, so the result keeps good:

var_dump($client->evaluate('
    local result = {_ = 0}
    result[8] = 15
    return result
')->getData());

This way i see expected result.

array(1) {
  [0]=>
  array(2) {
    [8]=>
    int(15)
    ["_"]=>
    int(0)
  }
}

Maybe there is a way to keep it working without any workaround?
For example, change indexes only when all keys are integer, goes one by one and last equals table size.

@rybakit
Copy link
Member

rybakit commented Dec 5, 2016

@nekufa The similar issue is discussed here: tarantool/tarantool-php#89

@rybakit
Copy link
Member

rybakit commented May 2, 2017

Maybe there is a way to keep it working without any workaround?
For example, change indexes only when all keys are integer, goes one by one and last equals table size.

I don't think it should be fixed on the client side. It looks like a Tarantool issue to me, the client receives MP_ARRAY of 8 items instead of MP_MAP in a response. I guess Tarantool should check if a lua table is an array and pack it accordingly.

@rybakit
Copy link
Member

rybakit commented May 2, 2017

As discussed on Tarantool chat, to force Tarantool to pack in to map/array structures you need to specify the __serialize hint:

print_r($client->evaluate('
    local result = {}
    result[8] = 15
    return result
')->getData());

print_r($client->evaluate('
    local result = setmetatable({}, { __serialize = "map" })
    result[8] = 15
    return result
')->getData());

Output:

Array
(
    [0] => Array
        (
            [0] =>
            [1] =>
            [2] =>
            [3] =>
            [4] =>
            [5] =>
            [6] =>
            [7] => 15
        )

)
Array
(
    [0] => Array
        (
            [8] => 15
        )

)

@rybakit rybakit closed this as completed May 2, 2017
@nekufa
Copy link
Member Author

nekufa commented May 4, 2017

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants