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

json.v example

This commit is contained in:
Alexander Medvednikov 2019-06-23 12:57:58 +02:00
parent 6e50421f99
commit 64a5dc3f23
2 changed files with 38 additions and 15 deletions

View File

@ -688,9 +688,6 @@ fn (p mut Parser) get_type() string {
// println('fn return typ=$f.typ') // println('fn return typ=$f.typ')
} }
else { else {
if debug {
println('new line!!')
}
f.typ = 'void' f.typ = 'void'
} }
// Register anon fn type // Register anon fn type
@ -700,7 +697,6 @@ fn (p mut Parser) get_type() string {
func: f func: f
} }
p.table.register_type2(fn_typ) p.table.register_type2(fn_typ)
// println('!!!registerd typ_str="' + f.typ_str() + '" f.typ=$f.typ')
return f.typ_str() return f.typ_str()
} }
// arrays ([]int) // arrays ([]int)
@ -1196,14 +1192,11 @@ fn (p mut Parser) name_expr() string {
if p.is_play && !p.builtin_pkg { if p.is_play && !p.builtin_pkg {
p.error('dereferencing is temporarily disabled on the playground, will be fixed soon') p.error('dereferencing is temporarily disabled on the playground, will be fixed soon')
} }
// IVE BEEN LOOKING FOR YOU FOR 20 MINUTES
// p.gen('*/*!!!!*/')
} }
mut name := p.lit mut name := p.lit
p.fgen(name) p.fgen(name)
// known_type := p.table.known_type(name) // known_type := p.table.known_type(name)
orig_name := name orig_name := name
// println('\n\n!!name_expr() name=$name cur_fn = $p.cur_fn.name')
is_c := name == 'C' && p.peek() == DOT is_c := name == 'C' && p.peek() == DOT
mut is_c_struct_init := is_c && ptr// a := &C.mycstruct{} mut is_c_struct_init := is_c && ptr// a := &C.mycstruct{}
if is_c { if is_c {
@ -1281,7 +1274,6 @@ fn (p mut Parser) name_expr() string {
} }
// Color.green // Color.green
else if p.peek() == DOT { else if p.peek() == DOT {
// println('$name enum init!!')
enum_type := p.table.find_type(name) enum_type := p.table.find_type(name)
if !enum_type.is_enum { if !enum_type.is_enum {
p.error('`$name` is not an enum') p.error('`$name` is not an enum')
@ -1497,9 +1489,6 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string {
// field // field
if has_field { if has_field {
field := p.table.find_field(typ, field_name) field := p.table.find_field(typ, field_name)
if field.name == 'age' {
println('!! $field.name $field.is_mut')
}
// Is the next token `=`, `+=` etc? (Are we modifying the field?) // Is the next token `=`, `+=` etc? (Are we modifying the field?)
next := p.peek() next := p.peek()
modifying := next.is_assign() || next == INC || next == DEC modifying := next.is_assign() || next == INC || next == DEC
@ -1546,7 +1535,6 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string {
return typ.name.right(6) return typ.name.right(6)
} }
if false && p.tok == LSBR { if false && p.tok == LSBR {
println('!!!!!!!! [ in DOT')
// if is_indexer { // if is_indexer {
return p.index_expr(method.typ, method_ph) return p.index_expr(method.typ, method_ph)
} }
@ -1567,7 +1555,6 @@ fn (p mut Parser) index_expr(typ string, fn_ph int) string {
is_indexer := p.tok == LSBR is_indexer := p.tok == LSBR
mut close_bracket := false mut close_bracket := false
if is_indexer { if is_indexer {
// println('!!! GOT []')p
is_fixed_arr := typ[0] == `[` is_fixed_arr := typ[0] == `[`
if !is_str && !is_arr && !is_map && !is_ptr && !is_fixed_arr { if !is_str && !is_arr && !is_map && !is_ptr && !is_fixed_arr {
p.error('Cant [] non-array/string/map. Got type "$typ"') p.error('Cant [] non-array/string/map. Got type "$typ"')
@ -1747,7 +1734,6 @@ fn (p mut Parser) index_expr(typ string, fn_ph int) string {
} }
} }
// else if is_arr && is_indexer{} // else if is_arr && is_indexer{}
p.log('!!indexer typ=$typ')
return typ return typ
} }
@ -2592,7 +2578,6 @@ fn (p mut Parser) chash() {
} }
if hash.starts_with('flag ') { if hash.starts_with('flag ') {
mut flag := hash.right(5) mut flag := hash.right(5)
// println('FLAG!!! $flag OS=$p.os')
// No the right os? Skip! // No the right os? Skip!
// mut ok := true // mut ok := true
if hash.contains('linux') && p.os != LINUX { if hash.contains('linux') && p.os != LINUX {

38
examples/json.v Normal file
View File

@ -0,0 +1,38 @@
import json
struct User {
name string
age int
mut:
is_registered bool
}
fn main() {
s := '[{ "name":"Frodo", "age":25}, {"name":"Bobby", "age":10}]'
users := json.decode( []User, s) or {
eprintln('Failed to parse json')
return
}
for user in users {
println('$user.name: $user.age')
}
println('')
for i, user in users {
println('$i) $user.name')
if !user.can_register() {
println('Cannot register $user.name, they are too young')
}
}
// Let's encode users again just for fun
println('')
println(json.encode(users))
}
fn (u User) can_register() bool {
return u.age >= 16
}
fn (u mut User) register() {
u.is_registered = true
}