From 1a0d5a0fca7ca794035bd8786bb5a73e87935e3f Mon Sep 17 00:00:00 2001 From: Luka Giorgadze Date: Wed, 20 Dec 2023 20:35:05 +0400 Subject: [PATCH] bump go version, update readme, release v1.0.0 --- README.md | 78 +++++++++++++++++++++++++------------------------- go.mod | 4 +-- gonull_test.go | 48 +++++++++++++++++++------------ 3 files changed, 70 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index 96606ab..6ce4fc2 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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)) } @@ -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) + } // ... } ``` diff --git a/go.mod b/go.mod index c6ee52f..9468603 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/gonull_test.go b/gonull_test.go index d120e7e..badebd7 100644 --- a/gonull_test.go +++ b/gonull_test.go @@ -5,7 +5,7 @@ import ( "encoding/json" "testing" - "github.com/lomsa-dev/gonull" + "github.com/LukaGiorgadze/gonull" "github.com/stretchr/testify/assert" ) @@ -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, }, } @@ -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) } @@ -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, }, } @@ -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) }) } }