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

Clone Field or Use it Twice? #628

Open
Smithx10 opened this issue Sep 25, 2024 · 1 comment
Open

Clone Field or Use it Twice? #628

Smithx10 opened this issue Sep 25, 2024 · 1 comment

Comments

@Smithx10
Copy link

I Am trying to set the same field for a req and response but only 1 gets set. I have to do the following for it to work.
Doesn't Work:

			case "Create":
				// Res
				if err := req.TryAddField(tf); err != nil {
					return err
				}
				// Res
				if err := res.TryAddField(tf); err != nil {
					return err
				}

Always have to create a New Field:

			case "Create":
				// Res
				if err := req.TryAddField(protobuilder.NewField(tf.Name(), tf.Type())); err != nil {
					return err
				}
				// Res
				if err := res.TryAddField(protobuilder.NewField(tf.Name(), tf.Type())); err != nil {
					return err
				}
@jhump
Copy link
Owner

jhump commented Sep 25, 2024

@Smithx10, I see the documentation isn't particularly clear on this. But this is, in fact, expected. Each builder also includes a link up to its parent. If you add a field to another message, it changes its parent.

I think a proper solution to the issue you face will be twofold:

  1. Updates docs to make it more clear that adding an element to a new parent moves the element; it doesn't copy it nor can multiple builders share references to the same child element.
  2. Provide a Clone() method on each builder type, to make it trivial to copy them, for cases like this.

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

No branches or pull requests

2 participants