Skip to content

Commit

Permalink
bump go version, update readme, release v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaGiorgadze committed Dec 20, 2023
1 parent 21cd149 commit 1a0d5a0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 60 deletions.
78 changes: 39 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Go Nullable with Generics

[![PkgGoDev](https://pkg.go.dev/badge/github.com/lomsa-dev/gonull)](https://pkg.go.dev/github.com/lomsa-dev/gonull) ![mod-verify](https://github.com/lomsa-dev/gonull/workflows/mod-verify/badge.svg) ![golangci-lint](https://github.com/lomsa-dev/gonull/workflows/golangci-lint/badge.svg) ![staticcheck](https://github.com/lomsa-dev/gonull/workflows/staticcheck/badge.svg) ![gosec](https://github.com/lomsa-dev/gonull/workflows/gosec/badge.svg) [![codecov](https://codecov.io/gh/lomsa-dev/gonull/branch/main/graph/badge.svg?token=76089e7b-f137-4459-8eae-4b48007bd0d6)](https://codecov.io/gh/lomsa-dev/gonull)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/LukaGiorgadze/gonull)](https://pkg.go.dev/github.com/LukaGiorgadze/gonull) ![mod-verify](https://github.com/LukaGiorgadze/gonull/workflows/mod-verify/badge.svg) ![golangci-lint](https://github.com/LukaGiorgadze/gonull/workflows/golangci-lint/badge.svg) ![staticcheck](https://github.com/LukaGiorgadze/gonull/workflows/staticcheck/badge.svg) ![gosec](https://github.com/LukaGiorgadze/gonull/workflows/gosec/badge.svg) [![codecov](https://codecov.io/gh/LukaGiorgadze/gonull/branch/main/graph/badge.svg?token=76089e7b-f137-4459-8eae-4b48007bd0d6)](https://codecov.io/gh/LukaGiorgadze/gonull)

## Go package simplifies nullable fields handling with Go Generics.

Expand All @@ -17,7 +17,7 @@ Unlike other nullable libraries, gonull leverages Go's generics feature, enablin
## Usage

```bash
go get github.com/lomsa-dev/gonull
go get github.com/LukaGiorgadze/gonull
```

### Example
Expand All @@ -26,37 +26,37 @@ go get github.com/lomsa-dev/gonull
package main

import (
"encoding/json"
"fmt"
"encoding/json"
"fmt"

"github.com/lomsa-dev/gonull"
"github.com/LukaGiorgadze/gonull"
)

type MyCustomInt int
type MyCustomFloat32 float32

type Person struct {
Name string
Age gonull.Nullable[MyCustomInt]
Address gonull.Nullable[string]
Height gonull.Nullable[MyCustomFloat32]
Name string
Age gonull.Nullable[MyCustomInt]
Address gonull.Nullable[string]
Height gonull.Nullable[MyCustomFloat32]
}

func main() {
jsonData := []byte(`{"Name":"Alice","Age":15,"Address":null,"Height":null}`)

var person Person
err := json.Unmarshal(jsonData, &person)
if err != nil {
panic(err)
}
fmt.Printf("Unmarshalled Person: %+v\n", person)

marshalledData, err := json.Marshal(person)
if err != nil {
panic(err)
}
fmt.Printf("Marshalled JSON: %s\n", string(marshalledData))
jsonData := []byte(`{"Name":"Alice","Age":15,"Address":null,"Height":null}`)

var person Person
err := json.Unmarshal(jsonData, &person)
if err != nil {
panic(err)
}
fmt.Printf("Unmarshalled Person: %+v\n", person)

marshalledData, err := json.Marshal(person)
if err != nil {
panic(err)
}
fmt.Printf("Marshalled JSON: %s\n", string(marshalledData))
}


Expand All @@ -66,26 +66,26 @@ func main() {

```go
type User struct {
Name gonull.Nullable[string]
Age gonull.Nullable[int]
Name gonull.Nullable[string]
Age gonull.Nullable[int]
}

func main() {
// ...
rows, err := db.Query("SELECT id, name, age FROM users")
if err != nil {
log.Fatal(err)
}
defer rows.Close()

for rows.Next() {
var user User
err := rows.Scan( &user.Name, &user.Age)
if err != nil {
log.Fatal(err)
}
fmt.Printf("ID: %d, Name: %v, Age: %v\n", user.Name.Val, user.Age.Val)
}
rows, err := db.Query("SELECT id, name, age FROM users")
if err != nil {
log.Fatal(err)
}
defer rows.Close()

for rows.Next() {
var user User
err := rows.Scan(&user.Name, &user.Age)
if err != nil {
log.Fatal(err)
}
fmt.Printf("ID: %d, Name: %v, Age: %v\n", user.Name.Val, user.Age.Val)
}
// ...
}
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/lomsa-dev/gonull
module github.com/LukaGiorgadze/gonull

go 1.20
go 1.21

require github.com/stretchr/testify v1.8.2

Expand Down
48 changes: 29 additions & 19 deletions gonull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"testing"

"github.com/lomsa-dev/gonull"
"github.com/LukaGiorgadze/gonull"
"github.com/stretchr/testify/assert"
)

Expand All @@ -27,22 +27,26 @@ func TestNullableScan(t *testing.T) {
name string
value interface{}
Valid bool
Present bool
wantErr bool
}{
{
name: "nil value",
value: nil,
Valid: false,
name: "nil value",
value: nil,
Valid: false,
Present: true,
},
{
name: "string value",
value: "test",
Valid: true,
name: "string value",
value: "test",
Valid: true,
Present: true,
},
{
name: "unsupported type",
value: []byte{1, 2, 3},
wantErr: true,
Present: true,
},
}

Expand All @@ -56,6 +60,7 @@ func TestNullableScan(t *testing.T) {
} else {
assert.NoError(t, err)
assert.Equal(t, tt.Valid, n.Valid)
assert.Equal(t, tt.Present, n.Present)
if tt.Valid {
assert.Equal(t, tt.value, n.Val)
}
Expand Down Expand Up @@ -97,24 +102,27 @@ func TestNullableValue(t *testing.T) {

func TestNullableUnmarshalJSON(t *testing.T) {
type testCase struct {
name string
jsonData []byte
expectedVal int
expectedValid bool
name string
jsonData []byte
expectedVal any
expectedValid bool
expectedPresent bool
}

testCases := []testCase{
{
name: "ValuePresent",
jsonData: []byte(`123`),
expectedVal: 123,
expectedValid: true,
name: "ValuePresent",
jsonData: []byte(`123`),
expectedVal: 123,
expectedValid: true,
expectedPresent: true,
},
{
name: "ValueNull",
jsonData: []byte(`null`),
expectedVal: 0,
expectedValid: false,
name: "ValueNull",
jsonData: []byte(`null`),
expectedVal: 0,
expectedValid: false,
expectedPresent: true,
},
}

Expand All @@ -123,9 +131,11 @@ func TestNullableUnmarshalJSON(t *testing.T) {
var nullable gonull.Nullable[int]

err := nullable.UnmarshalJSON(tc.jsonData)

assert.NoError(t, err)
assert.Equal(t, tc.expectedVal, nullable.Val)
assert.Equal(t, tc.expectedValid, nullable.Valid)
assert.Equal(t, tc.expectedPresent, nullable.Present)
})
}
}
Expand Down

0 comments on commit 1a0d5a0

Please sign in to comment.