1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix indexexpr with complex index expr (#17693)

This commit is contained in:
Felipe Pena 2023-03-17 17:44:14 -03:00 committed by GitHub
parent 45c0a21f46
commit 68955bb26c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -449,7 +449,10 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
g.write('->val')
}
g.write(', &(${key_type_str}[]){')
old_is_assign_lhs := g.is_assign_lhs
g.is_assign_lhs = false
g.expr(node.index)
g.is_assign_lhs = old_is_assign_lhs
g.write('}, &(${elem_type_str}[]){ ${zero} }))')
} else {
zero := g.type_default(info.value_type)

View File

@ -0,0 +1,25 @@
import x.json2
struct SomeStruct {
title string
}
fn test_result_with_index() {
resp := '{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipitsuscipit recusandae consequuntur expedita et cumreprehenderit molestiae ut ut quas totamnostrum rerum est autem sunt rem eveniet architecto"
}'
raw_data := json2.raw_decode(resp)!
data := raw_data as map[string]json2.Any
mut ss := map[int]SomeStruct{}
s := SomeStruct{
title: data['title']!.str()
}
ss[data['id']!.int()] = s
t := ss[data['id']!.int()]
assert t.title == 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit'
}