json: handle field attributes

pull/4732/head
Alexander Medvednikov 2020-05-05 14:41:24 +02:00
parent a3bc32f3e0
commit b3d7b0205a
3 changed files with 11 additions and 8 deletions

View File

@ -16,12 +16,11 @@ fn test_simple() {
assert y.age == 28 assert y.age == 28
} }
struct User { struct User2 {
age int age int
nums []int nums []int
} }
/*
struct User { struct User {
age int age int
nums []int nums []int
@ -29,15 +28,17 @@ struct User {
is_registered bool [json:IsRegistered] is_registered bool [json:IsRegistered]
typ int [json:'type'] typ int [json:'type']
} }
*/
fn test_parse_user() { fn test_parse_user() {
s := '{"age": 10, "nums": [1,2,3], "type": 0, "lastName": "Johnson", "IsRegistered": true}' s := '{"age": 10, "nums": [1,2,3], "type": 1, "lastName": "Johnson", "IsRegistered": true}'
u2 := json.decode(User2, s) or {
exit(1)
}
println(u2)
u := json.decode(User, s) or { u := json.decode(User, s) or {
exit(1) exit(1)
} }
println(u) println(u)
/*
assert u.age == 10 assert u.age == 10
assert u.last_name == 'Johnson' assert u.last_name == 'Johnson'
assert u.is_registered == true assert u.is_registered == true
@ -45,8 +46,7 @@ fn test_parse_user() {
assert u.nums[0] == 1 assert u.nums[0] == 1
assert u.nums[1] == 2 assert u.nums[1] == 2
assert u.nums[2] == 3 assert u.nums[2] == 3
assert u.typ == 0 assert u.typ == 1
*/
} }
/* /*

View File

@ -495,6 +495,7 @@ fn (mut p Parser) attribute() ast.Attr {
} }
mut name := p.check_name() mut name := p.check_name()
if p.tok.kind == .colon { if p.tok.kind == .colon {
name += ':'
p.next() p.next()
if p.tok.kind == .name { if p.tok.kind == .name {
name += p.check_name() name += p.check_name()

View File

@ -123,6 +123,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
if p.tok.kind == .comment { if p.tok.kind == .comment {
comment = p.comment() comment = p.comment()
} }
// TODO merge table and ast Fields?
ast_fields << ast.StructField{ ast_fields << ast.StructField{
name: field_name name: field_name
pos: field_pos pos: field_pos
@ -140,6 +141,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
is_pub: is_field_pub is_pub: is_field_pub
is_mut: is_field_mut is_mut: is_field_mut
is_global: is_field_global is_global: is_field_global
attr: attr.name
} }
// println('struct field $ti.name $field_name') // println('struct field $ti.name $field_name')
} }