Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This fixes #86, Duplicated field in the generated code #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

GiovanePS
Copy link

PR Details

Description

The problem happens due to the combination of two lines of code:

  1. xmlElement.go:58:
    • opt.Element.Push(&e)
      This line inserts an element in Element stack, to indicate that we are inside <element></element>.
  2. xmlRestriction.go:49:
    • opt.ComplexType.Peek().(*ComplexType).Elements[len(opt.ComplexType.Peek().(*ComplexType).Elements)-1] = *opt.Element.Peek().(*Element)
      This replaces the last element of ComplexType.Elements.

The problem occurs because when some element neither has a type attribute nor contains a restriction (which is valid), the elements were not popped properly (when they were pushed only because they had no name). And when a restriction is found, the second code mentioned runs, replacing the last element.

It's good to know that it only happens with an attribute because, if the restriction is an element, like this:

<element name="ElementWithRestriction">
	<simpleType>
		<restriction base="string" />
	</simpleType>
</element>

the restriction handling code runs, but the *opt.Element.Peek().(*Element) refers to itself, in other words, replacing the last element (itself) with itself.

The solution I found to solve this problem is like the solution proposed by @senekor (#5), that simply pops the element at the end:

if opt.Element.Len() > 0 && opt.ComplexType.Len() > 0 {
     opt.Element.Pop()
}

His problem is not the same as mine, but the root cause is: the opt.Element stack.
With the above solution, the code stops generating the same field twice, because there are no remnant elements.

Related Issue

#86

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@senekor
Copy link

senekor commented Mar 5, 2025

@GiovanePS stop tagging me, I don't use this library anymore.

You should've commented on the issue I opened, that way I could unsubscribe from my own issue if it is not relevant to me anymore. If you tag people directly, they have no practical way to escape the notification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants