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

all: comptime type lowercase ($int, $enum, $option, etc) (#17732)

This commit is contained in:
Felipe Pena
2023-03-22 20:02:42 -03:00
committed by GitHub
parent 0afb41f7e1
commit 9a9cfe425c
24 changed files with 203 additions and 122 deletions

View File

@@ -132,11 +132,11 @@ fn (e &Encoder) encode_value_with_level[T](val T, level int, mut wr io.Writer) !
e.encode_any(val, level, mut wr)!
} $else $if T is Encodable {
wr.write(val.json_str().bytes())!
} $else $if T is $Struct {
} $else $if T is $struct {
e.encode_struct(val, level, mut wr)!
} $else $if T is $Enum {
} $else $if T is $enum {
e.encode_any(Any(int(val)), level, mut wr)!
} $else $if T in [Null, bool, $Float, $Int] {
} $else $if T in [Null, bool, $float, $int] {
e.encode_any(val, level, mut wr)!
} $else {
// dump(val.str())
@@ -245,21 +245,21 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! {
wr.write(json2.quote_bytes)!
wr.write(val.$(field.name).format_rfc3339().bytes())!
wr.write(json2.quote_bytes)!
} $else $if field.typ in [bool, $Float, $Int] {
} $else $if field.typ in [bool, $float, $int] {
wr.write(val.$(field.name).str().bytes())!
} $else $if field.is_array {
// TODO - replace for `field.typ is $Array`
// TODO - replace for `field.typ is $array`
e.encode_array(value, level + 1, mut wr)!
} $else $if field.typ is $Array {
} $else $if field.typ is $array {
// e.encode_array(value, level + 1, mut wr)! // FIXME - error: could not infer generic type `U` in call to `encode_array`
} $else $if field.typ is $Struct {
} $else $if field.typ is $struct {
e.encode_struct(value, level + 1, mut wr)!
} $else $if field.is_enum {
// TODO - replace for `field.typ is $Enum`
// TODO - replace for `field.typ is $enum`
wr.write(int(val.$(field.name)).str().bytes())!
} $else $if field.typ is $Enum {
} $else $if field.typ is $enum {
// wr.write(int(val.$(field.name)).str().bytes())! // FIXME - error: cannot cast string to `int`, use `val.$field.name.int()` instead.
} $else $if field.typ is $Sumtype {
} $else $if field.typ is $sumtype {
// dump(val.$(field.name).str())
// dump(is_none)
sum_type_value := value.str()#[typeof(val.$(field.name)).name.len + 1..-1]
@@ -319,27 +319,27 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! {
if is_string {
e.encode_string(sum_type_value#[1..-1], mut wr)!
}
} $else $if field.typ is $Alias {
} $else $if field.typ is $alias {
$if field.unaliased_typ is string {
e.encode_string(val.$(field.name).str(), mut wr)!
} $else $if field.unaliased_typ is time.Time {
parsed_time := time.parse(val.$(field.name).str()) or { time.Time{} }
e.encode_string(parsed_time.format_rfc3339(), mut wr)!
} $else $if field.unaliased_typ in [bool, $Float, $Int] {
} $else $if field.unaliased_typ in [bool, $float, $int] {
wr.write(val.$(field.name).str().bytes())!
} $else $if field.unaliased_typ is $Array {
} $else $if field.unaliased_typ is $array {
// e.encode_array(val.$(field.name), level + 1, mut wr)! // FIXME - error: could not infer generic type `U` in call to `encode_array`
} $else $if field.unaliased_typ is $Struct {
} $else $if field.unaliased_typ is $struct {
// e.encode_struct(val.$(field.name), level + 1, mut wr)! // FIXME - error: cannot use `BoolAlias` as `StringAlias` in argument 1 to `x.json2.Encoder.encode_struct`
e.encode_struct(value, level + 1, mut wr)!
} $else $if field.unaliased_typ is $Enum {
} $else $if field.unaliased_typ is $enum {
// enum_value := val.$(field.name)
// dump(int(val.$(field.name))) // FIXME
// dump(val.$(field.name).int()) // FIXME - error: unknown method or field: `BoolAlias.int`
// dump(val.$(field.name).int()) // FIXME - error: cannot convert 'enum <anonymous>' to 'struct string'
// wr.write(val.$(field.name).int().str().bytes())! // FIXME - error: unknown method or field: `BoolAlias.int`
} $else $if field.unaliased_typ is $Sumtype {
} $else $if field.unaliased_typ is $sumtype {
} $else {
return error('the alias ${typeof(val).name} cannot be encoded')
}
@@ -390,18 +390,18 @@ fn (e &Encoder) encode_array[U](val []U, level int, mut wr io.Writer) ! {
e.encode_any(u32(val[i]), level + 1, mut wr)!
} $else $if U is u64 {
e.encode_any(u64(val[i]), level + 1, mut wr)!
} $else $if U is $Array {
} $else $if U is $array {
// FIXME - error: could not infer generic type `U` in call to `encode_array`
// e.encode_array(val[i], level + 1, mut wr)!
} $else $if U is $Struct {
} $else $if U is $struct {
e.encode_struct(val[i], level + 1, mut wr)!
} $else $if U is $Sumtype {
} $else $if U is $sumtype {
$if U is Any {
e.encode_any(val[i], level + 1, mut wr)!
} $else {
// TODO
}
} $else $if U is $Enum {
} $else $if U is $enum {
e.encode_any(i64(val[i]), level + 1, mut wr)!
} $else {
return error('type ${typeof(val).name} cannot be array encoded')

View File

@@ -135,7 +135,7 @@ pub fn decode[T](src string) !T {
// encode is a generic function that encodes a type into a JSON string.
pub fn encode[T](val T) string {
$if T is $Array {
$if T is $array {
$compile_error('Cannot use `json.encode` to encode array. Try `json.encode_array` instead')
}
mut sb := strings.new_builder(64)
@@ -406,7 +406,7 @@ pub fn (f Any) to_time() !time.Time {
fn map_from[T](t T) map[string]Any {
mut m := map[string]Any{}
$if T is $Struct {
$if T is $struct {
$for field in T.fields {
value := t.$(field.name)