-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UUIDArray struct for handling them correctly with databases (#10)
Drop support for Go 1.15 and add support for Go 1.20 Update golangci-lint to 1.51.2 Change master for main
- Loading branch information
1 parent
9cc8942
commit 94f804b
Showing
3 changed files
with
82 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package uuid | ||
|
||
import ( | ||
"database/sql/driver" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type UUIDArray []UUID | ||
|
||
func (a *UUIDArray) Scan(val interface{}) error { | ||
switch v := val.(type) { | ||
case nil: | ||
*a = []UUID{} | ||
return nil | ||
case []byte: | ||
return json.Unmarshal(v, &a) | ||
case string: | ||
return json.Unmarshal([]byte(v), &a) | ||
default: | ||
return fmt.Errorf("Unsupported type: %T", v) | ||
} | ||
} | ||
|
||
func (a UUIDArray) Value() (driver.Value, error) { | ||
return json.Marshal(a) | ||
} |
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,51 @@ | ||
package uuid | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_StringArray_Scan(t *testing.T) { | ||
var ( | ||
uuid1, _ = FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8") | ||
uuid2, _ = FromString("3964fdbc-dc5a-4ec6-8a6b-eecfa5a8e9ae") | ||
uuidArray = UUIDArray{uuid1, uuid2} | ||
) | ||
|
||
json := `["6ba7b810-9dad-11d1-80b4-00c04fd430c8","3964fdbc-dc5a-4ec6-8a6b-eecfa5a8e9ae"]` | ||
newArrays := UUIDArray{} | ||
err := newArrays.Scan(json) | ||
assert.Equal(t, uuidArray, newArrays) | ||
assert.NoError(t, err) | ||
|
||
newArrays = UUIDArray{} | ||
err = newArrays.Scan([]byte(json)) | ||
assert.Equal(t, uuidArray, newArrays) | ||
assert.NoError(t, err) | ||
|
||
err = newArrays.Scan(nil) | ||
assert.Equal(t, newArrays, UUIDArray{}) | ||
assert.NoError(t, err) | ||
|
||
err = newArrays.Scan(42) | ||
assert.Error(t, err) | ||
|
||
err = newArrays.Scan("{I am not a valid json") | ||
assert.Error(t, err) | ||
|
||
err = newArrays.Scan(nil) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func Test_StringArray_Value(t *testing.T) { | ||
var ( | ||
uuid1, _ = FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8") | ||
uuid2, _ = FromString("3964fdbc-dc5a-4ec6-8a6b-eecfa5a8e9ae") | ||
uuidArray = UUIDArray{uuid1, uuid2} | ||
) | ||
|
||
expected := `["6ba7b810-9dad-11d1-80b4-00c04fd430c8","3964fdbc-dc5a-4ec6-8a6b-eecfa5a8e9ae"]` | ||
val, _ := uuidArray.Value() | ||
assert.Equal(t, expected, fmt.Sprintf("%s", val)) | ||
} |