feat(protocol): negotiate display and native input
This commit is contained in:
+16
-2
@@ -160,7 +160,11 @@ def go_validation(definition: dict[str, Any]) -> list[str]:
|
||||
lines.append(f"\tfor index := range v.{field} {{ if err := v.{field}[index].Validate(); err != nil {{ violations = append(violations, FieldViolation{{Field: fmt.Sprintf(\"{prop_name}[%d]\", index), Code: \"invalid_item\"}}) }} }}")
|
||||
reference = ref_name(prop)
|
||||
if reference:
|
||||
lines.append(f"\tif err := v.{field}.Validate(); err != nil {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"invalid_object\"}}) }}")
|
||||
validation = f"if err := v.{field}.Validate(); err != nil {{ violations = append(violations, FieldViolation{{Field: \"{prop_name}\", Code: \"invalid_object\"}}) }}"
|
||||
if prop_name not in required:
|
||||
lines.append(f"\tif v.{field} != nil {{ {validation} }}")
|
||||
else:
|
||||
lines.append(f"\t{validation}")
|
||||
if name in {"AllocationPolicy", "ManifestBounds"}:
|
||||
lines.append("\tif v.MinimumKbps > v.TargetKbps || v.TargetKbps > v.MaximumKbps { violations = append(violations, FieldViolation{Field: \"bounds\", Code: \"invalid_order\"}) }")
|
||||
if name == "GatewayRegistration":
|
||||
@@ -212,7 +216,10 @@ def generate_go(defs: dict[str, dict[str, Any]], schema_hash: str, version: str,
|
||||
required = set(definition.get("required", []))
|
||||
for prop_name, prop in definition.get("properties", {}).items():
|
||||
tag = prop_name + (",omitempty" if prop_name not in required else "")
|
||||
out.append(f"\t{go_field(prop_name)} {prop_type(prop, 'go')} `json:\"{tag}\"`")
|
||||
typ = prop_type(prop, "go")
|
||||
if prop_name not in required and ref_name(prop):
|
||||
typ = "*" + typ
|
||||
out.append(f"\t{go_field(prop_name)} {typ} `json:\"{tag}\"`")
|
||||
out.extend(["}", ""])
|
||||
for name in sorted(defs):
|
||||
out.append(f"func (v {name}) Validate() error {{")
|
||||
@@ -234,6 +241,11 @@ def generate_go(defs: dict[str, dict[str, Any]], schema_hash: str, version: str,
|
||||
% (prop_name, prop_name)
|
||||
)
|
||||
for prop_name, prop in defs[name].get("properties", {}).items():
|
||||
if prop_name not in required_fields and ref_name(prop):
|
||||
out.append(
|
||||
'\tif raw, ok := fields["%s"]; ok && bytes.Equal(bytes.TrimSpace(raw), []byte("null")) { return value, ValidationError{Violations: []FieldViolation{{Field: "%s", Code: "invalid_object"}}} }'
|
||||
% (prop_name, prop_name)
|
||||
)
|
||||
if "x-max-bytes" in prop and prop.get("type") != "string":
|
||||
out.append(
|
||||
'\tif raw, ok := fields["%s"]; ok && len(raw) > %d { return value, ValidationError{Violations: []FieldViolation{{Field: "%s", Code: "max_bytes"}}} }'
|
||||
@@ -603,6 +615,8 @@ def generate_swift(defs: dict[str, dict[str, Any]], schema_hash: str, compatibil
|
||||
typ = swift_type(prop)
|
||||
if prop_name in required:
|
||||
decoded.append(f"{field}: try c.decode({typ}.self, forKey: .{field})")
|
||||
elif ref_name(prop):
|
||||
decoded.append(f"{field}: try c.contains(.{field}) ? c.decode({typ}.self, forKey: .{field}) : nil")
|
||||
else:
|
||||
decoded.append(f"{field}: try c.decodeIfPresent({typ}.self, forKey: .{field})")
|
||||
out.append(f" try self.init({', '.join(decoded)})")
|
||||
|
||||
Reference in New Issue
Block a user