Skip to content

Commit

Permalink
Add DataOr method to nullable_json.go + minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
demdxx committed Jul 9, 2024
1 parent c934b9a commit 0879451
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Run linters
uses: golangci/golangci-lint-action@v3
with:
version: v1.57
version: v1.59
skip-cache: true
args: --fix

Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
strategy:
fail-fast: false
matrix:
go-version: [1.21.x]
go-version: [1.22.x]
steps:
- uses: actions/setup-go@v3
with:
Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# gosql
# GoSQL types collection

[![Build Status](https://github.com/geniusrabbit/gosql/workflows/run%20tests/badge.svg)](https://github.com/geniusrabbit/gosql/actions?workflow=run%20tests)
[![Go Report Card](https://goreportcard.com/badge/github.com/geniusrabbit/gosql)](https://goreportcard.com/report/github.com/geniusrabbit/gosql)
Expand All @@ -7,12 +7,10 @@

Library of standart sql collections an types like:

* Char
* NumberArrays generic (Ordered, Nullable) Numbers only (int, float, ...)
* StringArray
* JSON generic with any type of value `structs`, `scalars`, ...

New version supports generics from `go1.8` to make easear using of data processing.
* Char
* NumberArrays generic (Ordered, Nullable) Numbers only (int, float, ...)
* StringArray
* JSON generic with any type of value `structs`, `scalars`, ...

```go
import (
Expand All @@ -30,6 +28,6 @@ type Model struct {
}
```

# License MIT
## License MIT

[LICENSE](LICENSE)
4 changes: 2 additions & 2 deletions char.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (f Char) Value() (driver.Value, error) {
}

// Scan implements the sql.Scanner interface, char field
func (f *Char) Scan(value interface{}) (err error) {
func (f *Char) Scan(value any) (err error) {
*f, err = decodeChar(value)
return err
}
Expand All @@ -42,7 +42,7 @@ func (f *Char) UnmarshalJSON(b []byte) (err error) {
/// Helpers
///////////////////////////////////////////////////////////////////////////////

func decodeChar(value interface{}) (Char, error) {
func decodeChar(value any) (Char, error) {
if value == nil {
return Char(0), ErrNullValueNotAllowed
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
golang.org/x/exp v0.0.0-20240707233637-46b078467d37
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 h1:uLDX+AfeFCct3a2C7uIWBKMJIR3CJMhcgfrUAqjRK6w=
golang.org/x/exp v0.0.0-20240707233637-46b078467d37/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 h1:FVCohIoYO7IJoDDVpV2pdq7SgrMH6wHnuTyrdrxJNoY=
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
8 changes: 8 additions & 0 deletions nullable_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func (f *NullableJSON[T]) String() string {
return string(data)
}

// DataOr returns data or default value
func (f *NullableJSON[T]) DataOr(def T) T {
if f == nil || f.Data == nil {
return def
}
return *f.Data
}

// SetValue of json
func (f *NullableJSON[T]) SetValue(value any) error {
switch vl := value.(type) {
Expand Down
16 changes: 16 additions & 0 deletions nullable_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,20 @@ func TestNullableJSON(t *testing.T) {
assert.NoError(t, js.Scan([]byte(nil)), "scan nil byte")
assert.NoError(t, js.Scan([]byte(" ")), "scan empty string")
})

t.Run("data_or", func(t *testing.T) {
js, _ := NewNullableJSON[map[string]string](map[string]any{
"name": "Yoda",
"number": "1",
})
assert.Equal(t, "Yoda", js.DataOr(map[string]string{})["name"])
assert.Equal(t, "1", js.DataOr(map[string]string{})["number"])
assert.Equal(t, "Yoda", js.DataOr(map[string]string{"name": "Luke"})["name"])
assert.Equal(t, "1", js.DataOr(map[string]string{"number": "2"})["number"])

null, _ := NewNullableJSON[map[string]string](nil)
assert.Equal(t, "Luke", null.DataOr(map[string]string{"name": "Luke"})["name"])
assert.Equal(t, "2", null.DataOr(map[string]string{"number": "2"})["number"])
assert.Nil(t, null.DataOr(nil))
})
}
4 changes: 2 additions & 2 deletions nullable_number_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (f NullableNumberArray[T]) Value() (driver.Value, error) {
}

// Scan implements the driver.Valuer interface, []int field
func (f *NullableNumberArray[T]) Scan(value interface{}) error {
func (f *NullableNumberArray[T]) Scan(value any) error {
if res, err := ArrayNumberDecode[T](value, '{', '}'); err == nil {
*f = NullableNumberArray[T](res)
} else {
Expand All @@ -42,7 +42,7 @@ func (f *NullableNumberArray[T]) UnmarshalJSON(b []byte) error {
}

// DecodeValue implements the gocast.Decoder
func (f *NullableNumberArray[T]) DecodeValue(v interface{}) error {
func (f *NullableNumberArray[T]) DecodeValue(v any) error {
switch val := v.(type) {
case []T:
*f = NullableNumberArray[T](val)
Expand Down
4 changes: 2 additions & 2 deletions nullable_ordered_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (f NullableOrderedNumberArray[T]) Value() (driver.Value, error) {
}

// Scan implements the driver.Valuer interface, []int field
func (f *NullableOrderedNumberArray[T]) Scan(value interface{}) error {
func (f *NullableOrderedNumberArray[T]) Scan(value any) error {
if value == nil {
return ErrNullValueNotAllowed
}
Expand All @@ -38,7 +38,7 @@ func (f NullableOrderedNumberArray[T]) MarshalJSON() ([]byte, error) {
}

// DecodeValue implements the gocast.Decoder
func (f *NullableOrderedNumberArray[T]) DecodeValue(v interface{}) error {
func (f *NullableOrderedNumberArray[T]) DecodeValue(v any) error {
if v == nil {
return ErrNullValueNotAllowed
}
Expand Down
4 changes: 2 additions & 2 deletions number_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (f NumberArray[T]) Value() (driver.Value, error) {
}

// Scan implements the driver.Valuer interface, []int field
func (f *NumberArray[T]) Scan(value interface{}) error {
func (f *NumberArray[T]) Scan(value any) error {
if value == nil {
return ErrNullValueNotAllowed
}
Expand All @@ -41,7 +41,7 @@ func (f NumberArray[T]) MarshalJSON() ([]byte, error) {
}

// DecodeValue implements the gocast.Decoder
func (f *NumberArray[T]) DecodeValue(v interface{}) error {
func (f *NumberArray[T]) DecodeValue(v any) error {
if v == nil {
return ErrNullValueNotAllowed
}
Expand Down
4 changes: 2 additions & 2 deletions ordered_number_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (f OrderedNumberArray[T]) Value() (driver.Value, error) {
}

// Scan implements the driver.Valuer interface, []int field
func (f *OrderedNumberArray[T]) Scan(value interface{}) error {
func (f *OrderedNumberArray[T]) Scan(value any) error {
if value == nil {
return ErrNullValueNotAllowed
}
Expand All @@ -38,7 +38,7 @@ func (f OrderedNumberArray[T]) MarshalJSON() ([]byte, error) {
}

// DecodeValue implements the gocast.Decoder
func (f *OrderedNumberArray[T]) DecodeValue(v interface{}) error {
func (f *OrderedNumberArray[T]) DecodeValue(v any) error {
if v == nil {
return ErrNullValueNotAllowed
}
Expand Down
8 changes: 4 additions & 4 deletions string_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (f NullableStringArray) Value() (driver.Value, error) {
}

// Scan implements the driver.Valuer interface, []string field
func (f *NullableStringArray) Scan(value interface{}) error {
func (f *NullableStringArray) Scan(value any) error {
switch val := value.(type) {
case []byte:
*f = decodeNullableStringArray(string(val), '{', '}', '"', `""`)
Expand Down Expand Up @@ -69,7 +69,7 @@ func (f *NullableStringArray) UnmarshalJSON(b []byte) error {
}

// DecodeValue implements the gocast.Decoder
func (f *NullableStringArray) DecodeValue(v interface{}) error {
func (f *NullableStringArray) DecodeValue(v any) error {
switch val := v.(type) {
case []string:
*f = NullableStringArray(val)
Expand Down Expand Up @@ -146,7 +146,7 @@ func (f StringArray) Value() (driver.Value, error) {
}

// Scan implements the driver.Valuer interface, []string field
func (f *StringArray) Scan(value interface{}) error {
func (f *StringArray) Scan(value any) error {
if value == nil {
return ErrNullValueNotAllowed
}
Expand All @@ -162,7 +162,7 @@ func (f *StringArray) UnmarshalJSON(b []byte) error {
}

// DecodeValue implements the gocast.Decoder
func (f *StringArray) DecodeValue(v interface{}) error {
func (f *StringArray) DecodeValue(v any) error {
if v == nil {
return ErrNullValueNotAllowed
}
Expand Down

0 comments on commit 0879451

Please sign in to comment.