dynamic-struct icon indicating copy to clipboard operation
dynamic-struct copied to clipboard

Expose anonymous/embedded struct member.

Open infogulch opened this issue 2 years ago • 3 comments

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 avatar Oct 30 '23 22:10 infogulch

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 avatar Jan 12 '24 22:01 infogulch

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

https://github.com/golang/go/issues/65144

infogulch avatar Jan 18 '24 03:01 infogulch

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

infogulch avatar Mar 05 '24 20:03 infogulch