-
Notifications
You must be signed in to change notification settings - Fork 196
First pass of #67 - Let the shim functions return an error #182
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -4,4 +4,5 @@ _generated/*_gen.go | |
_generated/*_gen_test.go | ||
msgp/defgen_test.go | ||
msgp/cover.out | ||
*~ | ||
*~ | ||
*.coverprofile |
This file contains hidden or 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,75 @@ | ||
package _generated | ||
|
||
import "errors" | ||
|
||
//go:generate msgp | ||
|
||
//msgp:shim ConvertStringVal as:string using:fromConvertStringVal/toConvertStringVal mode:convert | ||
//msgp:ignore ConvertStringVal | ||
|
||
func fromConvertStringVal(v ConvertStringVal) (string, error) { | ||
return string(v), nil | ||
} | ||
|
||
func toConvertStringVal(s string) (ConvertStringVal, error) { | ||
return ConvertStringVal(s), nil | ||
} | ||
|
||
type ConvertStringVal string | ||
|
||
type ConvertString struct { | ||
String ConvertStringVal | ||
} | ||
|
||
//msgp:shim ConvertIntfVal as:interface{} using:fromConvertIntfVal/toConvertIntfVal mode:convert | ||
//msgp:ignore ConvertIntfVal | ||
|
||
func fromConvertIntfVal(v ConvertIntfVal) (interface{}, error) { | ||
return v.Test, nil | ||
} | ||
|
||
func toConvertIntfVal(s interface{}) (ConvertIntfVal, error) { | ||
return ConvertIntfVal{Test: s.(string)}, nil | ||
} | ||
|
||
type ConvertIntfVal struct { | ||
Test string | ||
} | ||
|
||
type ConvertIntf struct { | ||
Intf ConvertIntfVal | ||
} | ||
|
||
//msgp:shim ConvertErrVal as:string using:fromConvertErrVal/toConvertErrVal mode:convert | ||
//msgp:ignore ConvertErrVal | ||
|
||
var ( | ||
errConvertFrom = errors.New("error: convert from") | ||
errConvertTo = errors.New("error: convert to") | ||
) | ||
|
||
const ( | ||
fromFailStr = "fromfail" | ||
toFailStr = "tofail" | ||
) | ||
|
||
func fromConvertErrVal(v ConvertErrVal) (string, error) { | ||
s := string(v) | ||
if s == fromFailStr { | ||
return "", errConvertFrom | ||
} | ||
return s, nil | ||
} | ||
|
||
func toConvertErrVal(s string) (ConvertErrVal, error) { | ||
if s == toFailStr { | ||
return ConvertErrVal(""), errConvertTo | ||
} | ||
return ConvertErrVal(s), nil | ||
} | ||
|
||
type ConvertErrVal string | ||
|
||
type ConvertErr struct { | ||
Err ConvertErrVal | ||
} |
This file contains hidden or 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,59 @@ | ||
package _generated | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/tinylib/msgp/msgp" | ||
) | ||
|
||
func TestConvertFromEncodeError(t *testing.T) { | ||
e := ConvertErr{ConvertErrVal(fromFailStr)} | ||
var buf bytes.Buffer | ||
w := msgp.NewWriter(&buf) | ||
err := e.EncodeMsg(w) | ||
if err != errConvertFrom { | ||
t.Fatalf("expected conversion error, found %v", err.Error()) | ||
} | ||
} | ||
|
||
func TestConvertToEncodeError(t *testing.T) { | ||
var in, out ConvertErr | ||
in = ConvertErr{ConvertErrVal(toFailStr)} | ||
var buf bytes.Buffer | ||
w := msgp.NewWriter(&buf) | ||
err := in.EncodeMsg(w) | ||
if err != nil { | ||
t.FailNow() | ||
} | ||
w.Flush() | ||
|
||
r := msgp.NewReader(&buf) | ||
err = (&out).DecodeMsg(r) | ||
if err != errConvertTo { | ||
t.Fatalf("expected conversion error, found %v", err.Error()) | ||
} | ||
} | ||
|
||
func TestConvertFromMarshalError(t *testing.T) { | ||
e := ConvertErr{ConvertErrVal(fromFailStr)} | ||
var b []byte | ||
_, err := e.MarshalMsg(b) | ||
if err != errConvertFrom { | ||
t.Fatalf("expected conversion error, found %v", err.Error()) | ||
} | ||
} | ||
|
||
func TestConvertToMarshalError(t *testing.T) { | ||
var in, out ConvertErr | ||
in = ConvertErr{ConvertErrVal(toFailStr)} | ||
b, err := in.MarshalMsg(nil) | ||
if err != nil { | ||
t.FailNow() | ||
} | ||
|
||
_, err = (&out).UnmarshalMsg(b) | ||
if err != errConvertTo { | ||
t.Fatalf("expected conversion error, found %v", err.Error()) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you're generating code that shadows
err
without checking it first.