Skip to content

Commit

Permalink
Add UUIDArray struct for handling them correctly with databases (#10)
Browse files Browse the repository at this point in the history
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
jmaitrehenry authored Mar 6, 2023
1 parent 9cc8942 commit 94f804b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: CI

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]

jobs:
build:
strategy:
matrix:
go-version: [ 1.15, 1.16, 1.17, 1.18, 1.19 ]
go-version: [ 1.16, 1.17, 1.18, 1.19, '1.20' ]
os: [ ubuntu-latest, macos-latest, windows-latest ]

runs-on: ${{ matrix.os }}
Expand All @@ -25,7 +25,7 @@ jobs:
- name: Enforce standard format
uses: golangci/golangci-lint-action@v3
with:
version: v1.50.1
version: v1.51.2
args: --timeout 3m --enable=gofmt --verbose
- name: Test
run: go test --cover -v ./...
Expand Down
27 changes: 27 additions & 0 deletions uuid_array.go
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)
}
51 changes: 51 additions & 0 deletions uuid_array_test.go
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))
}

0 comments on commit 94f804b

Please sign in to comment.