-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Introduce support for Universally Unique Identifiers #2749
Closed
Closed
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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package graphql | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/gofrs/uuid" | ||
) | ||
|
||
func MarshalUUID(t uuid.UUID) Marshaler { | ||
if t.IsNil() { | ||
return Null | ||
} | ||
return WriterFunc(func(w io.Writer) { | ||
_, _ = io.WriteString(w, t.String()) | ||
}) | ||
} | ||
|
||
func UnmarshalUUID(v interface{}) (uuid.UUID, error) { | ||
if str, ok := v.(string); ok { | ||
return uuid.FromString(str) | ||
} | ||
return uuid.Nil, errors.New("input must be an RFC-4122 formatted string") | ||
} |
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,96 @@ | ||
package graphql | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMarshalUUID(t *testing.T) { | ||
t.Run("Null Values", func(t *testing.T) { | ||
var input = []uuid.UUID{uuid.Nil, uuid.FromStringOrNil("00000000-0000-0000-0000-000000000000")} | ||
for _, v := range input { | ||
assert.Equal(t, Null, MarshalUUID(v)) | ||
} | ||
}) | ||
|
||
t.Run("Valid Values", func(t *testing.T) { | ||
var generator = uuid.NewGen() | ||
var v1, _ = generator.NewV1() | ||
var v3 = generator.NewV3(uuid.FromStringOrNil("6ba7b810-9dad-11d1-80b4-00c04fd430c8"), "gqlgen.com") | ||
var v4, _ = generator.NewV4() | ||
var v5 = generator.NewV5(uuid.FromStringOrNil("6ba7b810-9dad-11d1-80b4-00c04fd430c8"), "gqlgen.com") | ||
var v6, _ = generator.NewV6() | ||
var v7, _ = generator.NewV7() | ||
var values = []struct { | ||
input uuid.UUID | ||
expected string | ||
}{ | ||
{v1, v1.String()}, | ||
{v3, v3.String()}, | ||
{v4, v4.String()}, | ||
{v5, v5.String()}, | ||
{v6, v6.String()}, | ||
{v7, v7.String()}, | ||
} | ||
for _, v := range values { | ||
assert.Equal(t, v.expected, m2s(MarshalUUID(v.input))) | ||
} | ||
}) | ||
} | ||
|
||
func TestUnmarshalUUID(t *testing.T) { | ||
t.Run("Invalid Non-String Values", func(t *testing.T) { | ||
var values = []interface{}{123, 1.2345678901, 1.2e+20, 1.2e-20, true, false} | ||
for _, v := range values { | ||
result, err := UnmarshalUUID(v) | ||
assert.Equal(t, uuid.Nil, result) | ||
assert.ErrorContains(t, err, "input must be an RFC-4122 formatted string") | ||
} | ||
}) | ||
|
||
t.Run("Invalid String Values", func(t *testing.T) { | ||
var values = []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"x50e8400-e29b-41d4-a716-446655440000", "uuid: invalid UUID format"}, | ||
{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "uuid: invalid UUID format"}, | ||
{"f50e8400-e29b-41d4-a716-44665544000", "uuid: incorrect UUID length 35 in string"}, | ||
{"foo", "uuid: incorrect UUID length 3 in string"}, | ||
{"", "uuid: incorrect UUID length 0 in string"}, | ||
} | ||
for _, v := range values { | ||
result, err := UnmarshalUUID(v.input) | ||
assert.Equal(t, uuid.Nil, result) | ||
assert.ErrorContains(t, err, v.expected) | ||
} | ||
}) | ||
|
||
t.Run("Valid Values", func(t *testing.T) { | ||
var generator = uuid.NewGen() | ||
var v1, _ = generator.NewV1() | ||
var v3 = generator.NewV3(uuid.FromStringOrNil("6ba7b810-9dad-11d1-80b4-00c04fd430c8"), "gqlgen.com") | ||
var v4, _ = generator.NewV4() | ||
var v5 = generator.NewV5(uuid.FromStringOrNil("6ba7b810-9dad-11d1-80b4-00c04fd430c8"), "gqlgen.com") | ||
var v6, _ = generator.NewV6() | ||
var v7, _ = generator.NewV7() | ||
var values = []struct { | ||
input string | ||
expected uuid.UUID | ||
}{ | ||
{v1.String(), v1}, | ||
{v3.String(), v3}, | ||
{v4.String(), v4}, | ||
{v5.String(), v5}, | ||
{v6.String(), v6}, | ||
{v7.String(), v7}, | ||
} | ||
for _, v := range values { | ||
result, err := UnmarshalUUID(v.input) | ||
assert.Equal(t, v.expected, result) | ||
assert.Nil(t, err) | ||
} | ||
}) | ||
} |
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.
writeQuotedString(w, t.String()) ?
uuid type marsher is "00000000-0000-0000-0000-00 0000000000"
must Quoted
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.
I don't fully understand why is that needed here. I think that might be the case for arbitrary string values that should be double-quoted because they could have escaped/control characters and non-printable ones, but the
uuid.UUID
will never have those.