From 97fc6dbd7f2c13346b78e436fb28be358129a2d3 Mon Sep 17 00:00:00 2001 From: Nick Shirokov Date: Sun, 8 Mar 2026 12:13:46 +0300 Subject: [PATCH] refactor(meta-compile): extract Build-TypeStr helper for type+length combination MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unify inline type string building logic (type + length/precision → "String(100)", "Number(10,2)") into a shared Build-TypeStr/build_type_str function. Used by both Parse-AttributeShorthand and Emit-ConstantProperties. Fix: check valueType before type to avoid treating metadata type name (e.g. "Constant") as data type. Co-Authored-By: Claude Opus 4.6 --- .../meta-compile/scripts/meta-compile.ps1 | 42 +++++++++---------- .../meta-compile/scripts/meta-compile.py | 33 +++++++-------- 2 files changed, 33 insertions(+), 42 deletions(-) diff --git a/.claude/skills/meta-compile/scripts/meta-compile.ps1 b/.claude/skills/meta-compile/scripts/meta-compile.ps1 index dd310970..c0359019 100644 --- a/.claude/skills/meta-compile/scripts/meta-compile.ps1 +++ b/.claude/skills/meta-compile/scripts/meta-compile.ps1 @@ -311,6 +311,21 @@ function Emit-FillValue { # --- 5. Attribute shorthand parser --- +function Build-TypeStr { + param($obj) + $t = if ($obj.valueType) { "$($obj.valueType)" } elseif ($obj.type) { "$($obj.type)" } else { "" } + if ($t -and -not $t.Contains('(')) { + if ($t -eq "String" -and $obj.length) { + $t = "String($($obj.length))" + } elseif ($t -eq "Number" -and $obj.length) { + $p = if ($obj.precision) { $obj.precision } else { 0 } + $nn = if ($obj.nonneg -or $obj.nonnegative) { ",nonneg" } else { "" } + $t = "Number($($obj.length),$p$nn)" + } + } + return $t +} + function Parse-AttributeShorthand { param($val) @@ -345,20 +360,9 @@ function Parse-AttributeShorthand { # Object form $name = "$($val.name)" - # Build type string combining type + length/precision from separate JSON fields - $typeStr = if ($val.type) { "$($val.type)" } else { "" } - if ($typeStr -and -not $typeStr.Contains('(')) { - if ($typeStr -eq "String" -and $val.length) { - $typeStr = "String($($val.length))" - } elseif ($typeStr -eq "Number" -and $val.length) { - $prec = if ($val.precision) { $val.precision } else { 0 } - $nn = if ($val.nonneg -or $val.nonnegative) { ",nonneg" } else { "" } - $typeStr = "Number($($val.length),$prec$nn)" - } - } return @{ name = $name - type = $typeStr + type = Build-TypeStr $val synonym = if ($val.synonym) { "$($val.synonym)" } else { Split-CamelCase $name } comment = if ($val.comment) { "$($val.comment)" } else { "" } flags = @(if ($val.flags) { $val.flags } else { @() }) @@ -1150,17 +1154,9 @@ function Emit-ConstantProperties { Emit-MLText $i "Synonym" $synonym X "$i" - # Type — combine valueType + length/precision from separate JSON fields - $valueType = if ($def.valueType) { "$($def.valueType)" } else { "String" } - if ($valueType -and -not $valueType.Contains('(')) { - if ($valueType -eq "String" -and $def.length) { - $valueType = "String($($def.length))" - } elseif ($valueType -eq "Number" -and $def.length) { - $p = if ($def.precision) { $def.precision } else { 0 } - $nn = if ($def.nonneg -or $def.nonnegative) { ",nonneg" } else { "" } - $valueType = "Number($($def.length),$p$nn)" - } - } + # Type + $valueType = Build-TypeStr $def + if (-not $valueType) { $valueType = "String" } Emit-ValueType $i $valueType X "$itrue" diff --git a/.claude/skills/meta-compile/scripts/meta-compile.py b/.claude/skills/meta-compile/scripts/meta-compile.py index f422955f..6d629b0b 100644 --- a/.claude/skills/meta-compile/scripts/meta-compile.py +++ b/.claude/skills/meta-compile/scripts/meta-compile.py @@ -296,6 +296,17 @@ def emit_fill_value(indent, type_str): # 5. Attribute shorthand parser # --------------------------------------------------------------------------- +def build_type_str(obj): + t = str(obj.get('valueType') or obj.get('type') or '') + if t and '(' not in t: + if t == 'String' and obj.get('length'): + t = f"String({obj['length']})" + elif t == 'Number' and obj.get('length'): + prec = obj.get('precision', 0) + nn = ',nonneg' if obj.get('nonneg') or obj.get('nonnegative') else '' + t = f"Number({obj['length']},{prec}{nn})" + return t + def parse_attribute_shorthand(val): if isinstance(val, str): parsed = { @@ -320,18 +331,9 @@ def parse_attribute_shorthand(val): return parsed # Object form name = str(val.get('name', '')) - # Build type string combining type + length/precision from separate JSON fields - type_str = str(val['type']) if val.get('type') else '' - if type_str and '(' not in type_str: - if type_str == 'String' and val.get('length'): - type_str = f"String({val['length']})" - elif type_str == 'Number' and val.get('length'): - prec = val.get('precision', 0) - nn = ',nonneg' if val.get('nonneg') or val.get('nonnegative') else '' - type_str = f"Number({val['length']},{prec}{nn})" return { 'name': name, - 'type': type_str, + 'type': build_type_str(val), 'synonym': str(val['synonym']) if val.get('synonym') else split_camel_case(name), 'comment': str(val['comment']) if val.get('comment') else '', 'flags': list(val.get('flags', [])), @@ -1023,15 +1025,8 @@ def emit_constant_properties(indent): X(f'{i}{esc_xml(obj_name)}') emit_mltext(i, 'Synonym', synonym) X(f'{i}') - # Type — combine valueType + length/precision from separate JSON fields - value_type = str(defn['valueType']) if defn.get('valueType') else 'String' - if value_type and '(' not in value_type: - if value_type == 'String' and defn.get('length'): - value_type = f"String({defn['length']})" - elif value_type == 'Number' and defn.get('length'): - prec = defn.get('precision', 0) - nn = ',nonneg' if defn.get('nonneg') or defn.get('nonnegative') else '' - value_type = f"Number({defn['length']},{prec}{nn})" + # Type + value_type = build_type_str(defn) or 'String' emit_value_type(i, value_type) X(f'{i}true') X(f'{i}')