Adding initial support for null structs (but not generic ones)

This commit is contained in:
Pavel Shevaev 2023-05-05 12:10:20 +03:00
parent 496bf3aabf
commit 55f8b0dea9
3 changed files with 37 additions and 18 deletions

View File

@ -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;
}

View File

@ -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");
}

View File

@ -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}}