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

Expose anonymous/embedded struct member. #30

Open
infogulch opened this issue Oct 30, 2023 · 3 comments · May be fixed by #31
Open

Expose anonymous/embedded struct member. #30

infogulch opened this issue Oct 30, 2023 · 3 comments · May be fixed by #31

Comments

@infogulch
Copy link

I see that last year support for embedded structs was added, but it doesn't enable the builder to create a type with embedded structs.

Something like this:

	type stitle struct{ Title string }
	type stext struct{ Text string }

	dstruct := dynamicstruct.NewStruct().
		AddField("", stitle{}, ``).
		AddField("", stext{}, ``).
		Build().
		New()

	type compare struct {
		stitle
		stext 
	}

	if dstruct != compare{} {
		fmt.Println("expected equal")
	}

Besides the general idea of creating a new struct, do you think this is a reasonable interface? In particular if name == "", then set anonymous: true.

@infogulch
Copy link
Author

infogulch commented Jan 12, 2024

Well it seems reflect cannot represent unexported embedded struct members:

https://go.dev/play/p/C7P38xkmjka?v=gotip

But it seems to work fine as long as the member structs are public:

https://go.dev/play/p/V0OchLN74bU?v=gotip

@infogulch
Copy link
Author

I've marked #31 as draft because I found a bug in the reflect package that causes issues with embedded fields.

golang/go#65144

@infogulch
Copy link
Author

Bugfix by ianlancetaylor: https://go-review.googlesource.com/c/go/+/567897

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