-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
node/bindnode: redesign the shape of unions in Go
Before, the shape of IPLD schema Unions in Go was as follows: struct { Index int // 0..len(typ.Members)-1 Value interface{} } This worked perfectly fine when inferring Go types from a schema. The inferred Go types would be anonymous, so it didn't really matter that the value was behind interface{}. However, this mechanism did not work for providing a custom Go type. For example, the equivalent of the added example would be: type CustomIntType int64 type StringOrInt struct { Index int Value interface{} } proto := bindnode.Prototype((*StringOrInt)(nil), schemaType) bindnode failed to use CustomIntType at all, since StringOrInt did not reference CustomIntType in any way. Moreover, the interface{} layer also felt prone to type assert panics. Now, the shape of Unions in Go is: struct { Type1 *Type1 Type2 *Type2 ... } The problem described above is no longer present anymore, as the added runnable example demonstrates. That alone makes the redesign worthwhile. One minor downside of the new method is large unions, since looking up the "index" is now a linear search for the first non-nil field pointer, and the size of the Go type increases with each member type. However, unions with more than a dozen member types should be rare. A different design direction would have been to keep interface{} values, and have "Register" APIs to tell bindnode about the member Go types. However, that feels worse in terms of usability and design complexity. Fixes #210.
- Loading branch information
Showing
5 changed files
with
119 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters