refactor(meta-compile): extract Build-TypeStr helper for type+length combination

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 <noreply@anthropic.com>
This commit is contained in:
Nick Shirokov
2026-03-08 12:13:46 +03:00
parent 49108f72dc
commit 97fc6dbd7f
2 changed files with 33 additions and 42 deletions
@@ -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<Comment/>"
# 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 "$i<UseStandardCommands>true</UseStandardCommands>"
@@ -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}<Name>{esc_xml(obj_name)}</Name>')
emit_mltext(i, 'Synonym', synonym)
X(f'{i}<Comment/>')
# 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}<UseStandardCommands>true</UseStandardCommands>')
X(f'{i}<DefaultForm/>')