From fcb46e36a16dbd36c28b4c1d1faafb3b63e1aec4 Mon Sep 17 00:00:00 2001 From: Marko Ristin Date: Wed, 15 May 2024 22:50:55 +0200 Subject: [PATCH] Allow for `xmlns` attributes in Golang We adapt the Golang SDK generator to ignore both the `xmlns` attribute as well as attributes prefixed with `xmlns:` since they are necessary for correct namespacing even though the AAS specs does not allow any attributes. --- .../golang/xmlization/_generate.py | 18 +++++++++++------- .../expected_output/xmlization/xmlization.go | 18 +++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/aas_core_codegen/golang/xmlization/_generate.py b/aas_core_codegen/golang/xmlization/_generate.py index 24bf6b91b..c274fe8bc 100644 --- a/aas_core_codegen/golang/xmlization/_generate.py +++ b/aas_core_codegen/golang/xmlization/_generate.py @@ -402,19 +402,23 @@ def _generate_check_start_element() -> Stripped: func checkStartElement( {I}current xml.StartElement, ) (err error) {{ -{I}var unexpectedAttr []xml.Attr +{I}const xmlnsLen = len("xmlns") + +{I}unexpectedAttr := 0 {I}for _, attr := range current.Attr {{ -{II}if attr.Name.Space == "" && attr.Name.Local == "xmlns" {{ +{II}if (attr.Name.Space == "" && attr.Name.Local == "xmlns") || +{III}attr.Name.Space == "xmlns" {{ {III}continue {II}}} -{II}unexpectedAttr = append(unexpectedAttr, attr) + +{II}unexpectedAttr++ {I}}} -{I}if len(unexpectedAttr) != 0 {{ -{II} err = newDeserializationError( +{I}if unexpectedAttr != 0 {{ +{II}err = newDeserializationError( {III}fmt.Sprintf( {IIII}"Expected no attributes except 'xmlns' in the start element, "+ -{IIII}"but got %d in the start element %s", -{IIII}len(unexpectedAttr), current.Name.Local, +{IIIII}"but got %d in the start element %s", +{IIII}unexpectedAttr, current.Name.Local, {III}), {II}) {II}return diff --git a/test_data/golang/test_main/aas_core_meta.v3/expected_output/xmlization/xmlization.go b/test_data/golang/test_main/aas_core_meta.v3/expected_output/xmlization/xmlization.go index 85a14bb66..32ca588c3 100644 --- a/test_data/golang/test_main/aas_core_meta.v3/expected_output/xmlization/xmlization.go +++ b/test_data/golang/test_main/aas_core_meta.v3/expected_output/xmlization/xmlization.go @@ -344,19 +344,23 @@ const Namespace = "https://admin-shell.io/aas/3/0" func checkStartElement( current xml.StartElement, ) (err error) { - var unexpectedAttr []xml.Attr + const xmlnsLen = len("xmlns") + + unexpectedAttr := 0 for _, attr := range current.Attr { - if attr.Name.Space == "" && attr.Name.Local == "xmlns" { + if (attr.Name.Space == "" && attr.Name.Local == "xmlns") || + attr.Name.Space == "xmlns" { continue } - unexpectedAttr = append(unexpectedAttr, attr) + + unexpectedAttr++ } - if len(unexpectedAttr) != 0 { - err = newDeserializationError( + if unexpectedAttr != 0 { + err = newDeserializationError( fmt.Sprintf( "Expected no attributes except 'xmlns' in the start element, "+ - "but got %d in the start element %s", - len(unexpectedAttr), current.Name.Local, + "but got %d in the start element %s", + unexpectedAttr, current.Name.Local, ), ) return