From 55f8b0dea9772b976acb81057f92313ca0949b57 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Fri, 5 May 2023 12:10:20 +0300 Subject: [PATCH] Adding initial support for null structs (but not generic ones) --- src/codegen.inc.php | 37 +++++++++++++++++++++++++------------ src/data_utils.inc.php | 12 ++++++------ tpl/macro.twig | 6 ++++++ 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 4cba5b0..b004f46 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -145,20 +145,25 @@ function data2value($name, \mtgType $type, $buf, $prefix = '', $tokens = array() $cond_indent = $indent." "; - $default_value_arg = array_key_exists('default', $tokens) ? default_value($tokens['default']) : 'null'; + $default_value_arg = array_key_exists('default', $tokens) ? ', '.default_value($tokens['default']) : ''; if($type instanceof \mtgMetaStruct) { $default = array_key_exists('default', $tokens) ? $tokens['default'] : null; if($default) { $default = json_decode($default, true); - if(!is_array($default)) + if(is_array($default)) + { + $default = str_replace("\n", "", var_export($default, true)); + $default_value_arg = ", \$assoc ? $default : array_values($default)"; + } + else if($default === null) + { + $default_value_arg = ", null"; + } + else throw new Exception("Bad default struct: " . $tokens['default']); - $default = str_replace("\n", "", var_export($default, true)); - $default_value_arg = "\$assoc ? $default : array_values($default)"; } - else - $default_value_arg = "null"; } $str .= "\n"; @@ -166,7 +171,7 @@ function data2value($name, \mtgType $type, $buf, $prefix = '', $tokens = array() if($as_is) $tmp_val = $buf; else - $str .= $indent."{$tmp_val} = \metagen_php\array_extract_val({$buf}, \$assoc, '{$name}', {$default_value_arg});\n"; + $str .= $indent."{$tmp_val} = \metagen_php\array_extract_val({$buf}, \$assoc, '{$name}' {$default_value_arg});\n"; if($type instanceof \mtgBuiltinType) { @@ -184,9 +189,13 @@ function data2value($name, \mtgType $type, $buf, $prefix = '', $tokens = array() } else { - $str .= $cond_indent."{$tmp_val} = " . apply_value_filters($name, $tokens, "{$tmp_val}"). ";\n"; - $str .= $cond_indent."\$tmp_sub_arr__ = \metagen_php\\val_arr({$tmp_val});\n"; - $str .= $cond_indent."{$pname} = new {$type}(\$tmp_sub_arr__, \$assoc);\n"; + $str .= $cond_indent."if({$tmp_val} === null) {\n"; + $str .= $cond_indent." {$pname} = null; \n"; + $str .= $cond_indent."} else {\n"; + $str .= $cond_indent." {$tmp_val} = " . apply_value_filters($name, $tokens, "{$tmp_val}"). ";\n"; + $str .= $cond_indent." \$tmp_sub_arr__ = \metagen_php\\val_arr({$tmp_val});\n"; + $str .= $cond_indent." {$pname} = new {$type}(\$tmp_sub_arr__, \$assoc);\n"; + $str .= "}"; } } else if($type instanceof \mtgArrType) @@ -242,9 +251,9 @@ function value2data($name, \mtgType $type, $buf, $prefix = '', $tokens = array() else if($type instanceof \mtgMetaStruct) { if(array_key_exists('virtual', $tokens)) - $str .= $indent."\metagen_php\array_set_value({$buf}, \$assoc, '$name', is_array({$pname}) ? {$pname} : {$pname}->export(\$assoc, true/*virtual*/));"; + $str .= $indent."\metagen_php\array_set_value({$buf}, \$assoc, '$name', !is_object({$pname}) ? {$pname} : {$pname}->export(\$assoc, true/*virtual*/));"; else - $str .= $indent."\metagen_php\array_set_value({$buf}, \$assoc, '$name', is_array({$pname}) ? {$pname} : {$pname}->export(\$assoc));"; + $str .= $indent."\metagen_php\array_set_value({$buf}, \$assoc, '$name', !is_object({$pname}) ? {$pname} : {$pname}->export(\$assoc));"; } else if($type instanceof \mtgMetaEnum) { @@ -277,3 +286,7 @@ function value2data($name, \mtgType $type, $buf, $prefix = '', $tokens = array() return $str; } +function is_null_str($default) +{ + return is_string($default) && json_decode($default, true) === null; +} diff --git a/src/data_utils.inc.php b/src/data_utils.inc.php index ceb5062..a28a514 100644 --- a/src/data_utils.inc.php +++ b/src/data_utils.inc.php @@ -2,7 +2,7 @@ namespace metagen_php; use Exception; -function array_extract_val(&$arr, $assoc, $name, $default = null) +function array_extract_val(&$arr, $assoc, $name) { if(!is_array($arr)) throw new Exception("$name: Not an array"); @@ -11,17 +11,17 @@ function array_extract_val(&$arr, $assoc, $name, $default = null) { if(sizeof($arr) == 0) { - if($default !== null) - return $default; + if(func_num_args() == 4) + return func_get_arg(3); throw new Exception("$name: No next array item"); } return array_shift($arr); } - if(!isset($arr[$name])) + if(!array_key_exists($name, $arr)) { - if($default !== null) - return $default; + if(func_num_args() == 4) + return func_get_arg(3); throw new Exception("$name: No array item"); } diff --git a/tpl/macro.twig b/tpl/macro.twig index 1babf4b..b444d32 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -191,7 +191,13 @@ array_merge(parent::CLASS_FIELDS_PROPS(), {% macro field_init_value(o, f) %} {%- if f.type is instanceof('\\mtgMetaStruct') ~%} + + {%- if has_token(f, 'default') and token(f, 'default') == 'null' -%} + null + {%- else -%} new {{f.type}}() + {%- endif -%} + {%- elseif f.type is instanceof('\\mtgMetaEnum') ~%} {%- if has_token(f, 'default') -%} {{f.type}}::{{token(f, 'default')|replace({'"' : ''})|default_value}}