From b7725936e604bcb3a589990d72a1aa6fb4f6ec88 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Tue, 8 Apr 2025 18:31:27 +0300 Subject: [PATCH] Fixing check for required fields without @default --- src/codegen.inc.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/codegen.inc.php b/src/codegen.inc.php index f1fbe2e..f63cdf3 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -304,9 +304,10 @@ function is_null_str($default) return is_string($default) && json_decode($default, true) === null; } -function get_default_value_arg(\mtgType $type, array $tokens) : string +function get_default_value_arg(\mtgType $type, array $tokens, bool &$has_default = true) : string { - $default_value_arg = array_key_exists('default', $tokens) ? default_value($tokens['default']) : "null"; + $has_default = array_key_exists('default', $tokens); + $default_value_arg = $has_default ? default_value($tokens['default']) : "null"; if($type instanceof \mtgMetaStruct) { @@ -335,13 +336,16 @@ function data_normalize($name, \mtgType $type, $buf, $dst, $tokens = array(), $t { $str = ''; - $default_value_arg = get_default_value_arg($type, $tokens); + $has_default = false; + $default_value_arg = get_default_value_arg($type, $tokens, $has_default); $str .= "\n"; if($name !== null) { $str .= "{$tmp_val} = {$buf}['{$name}'] ?? {$default_value_arg};\n"; + if(!$has_default) + $str .= "if({$tmp_val} === null) throw new Exception(\"'{$name}' is missing\");\n"; $str .= "if(\$check_junk) unset({$buf}['{$name}']);\n"; }