leanovate / gopter

GOlang Property TestER
MIT License
598 stars 40 forks source link

Allow gen.Struct() with interface-type fields #55

Closed philandstuff closed 5 years ago

philandstuff commented 5 years ago

Fixes #54.

When generating a struct that has interface-type fields, the code was causing panics in (*BiMapper).ConvertUp. This was because Struct() returns a DeriveGen() but the unbuildStructFunc fed into it just returns the reflect.Value from each field. When this is of interface type, it doesn't fit the underlying generator type and you get a panic of the pattern:

panic: reflect: function created by MakeFunc using closure returned wrong type: have interface {} for string

The solution is: for interface field types, we return the underlying element in the interface type. This should always be safe because the only things we're converting up were generated by ConvertDown in the first place, so the concrete types should match. In other words, if we always put in an AnyString() to an interface{} field type, we'll always get a string out of it again.