-
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.
fix(bindnode): more helpful error message for enum value footgun
Fixes: #348
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package bindnode_test | ||
|
||
import ( | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
"github.com/ipld/go-ipld-prime" | ||
"github.com/ipld/go-ipld-prime/codec/dagcbor" | ||
"github.com/ipld/go-ipld-prime/node/bindnode" | ||
) | ||
|
||
func TestEnumError(t *testing.T) { | ||
type Action string | ||
const ( | ||
ActionPresent = Action("p") | ||
ActionMissing = Action("m") | ||
) | ||
type S struct{ Action Action } | ||
|
||
schema := ` | ||
type S struct { | ||
Action Action | ||
} representation tuple | ||
type Action enum { | ||
| Present ("p") | ||
| Missing ("m") | ||
} representation string | ||
` | ||
|
||
typeSystem, err := ipld.LoadSchemaBytes([]byte(schema)) | ||
qt.Assert(t, err, qt.IsNil) | ||
schemaType := typeSystem.TypeByName("S") | ||
|
||
node := bindnode.Wrap(&S{Action: ActionPresent}, schemaType).Representation() | ||
_, err = ipld.Encode(node, dagcbor.Encode) | ||
qt.Assert(t, err, qt.IsNotNil) | ||
qt.Assert(t, err.Error(), qt.Equals, `AsString: "p" is not a valid member of enum Action (bindnode works at the type level; did you mean "Present"?)`) | ||
} |
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