Skip to content

Commit

Permalink
fix broken AllowUnquoted logic and update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jul 19, 2024
1 parent 54c3ee4 commit 0d0bcbf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module github.com/disgoorg/snowflake/v2

go 1.18

// Broken AllowUnquoted logic
retract v2.0.2
10 changes: 7 additions & 3 deletions snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ func (id *ID) UnmarshalJSON(data []byte) error {
return nil
}

snowflake, err := strconv.Unquote(string(data))
if err != nil && !AllowUnquoted {
return fmt.Errorf("failed to unquote snowflake: %w", err)
s := string(data)
snowflake, err := strconv.Unquote(s)
if err != nil {
if !AllowUnquoted {
return fmt.Errorf("failed to unquote snowflake: %w", err)
}
snowflake = s
}

i, err := strconv.ParseUint(snowflake, 10, 64)
Expand Down
12 changes: 10 additions & 2 deletions snowflake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,23 @@ func TestID_UnmarshalJSON(t *testing.T) {
err: nil,
},
{
name: "unquoted 1",
name: "unquoted 1 no allowUnquoted",
data: []byte("1"),
expected: 0,
allowUnquoted: true,
allowUnquoted: false,
err: strconv.ErrSyntax,
},
{
name: "unquoted 1 allowUnquoted",
data: []byte("1"),
expected: 1,
allowUnquoted: true,
err: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
AllowUnquoted = tt.allowUnquoted
var id ID
err := json.Unmarshal(tt.data, &id)

Expand Down

0 comments on commit 0d0bcbf

Please sign in to comment.