Skip to content

Commit

Permalink
feat: support sql.Scanner & drive.Valuer
Browse files Browse the repository at this point in the history
  • Loading branch information
masajip committed Jun 12, 2024
1 parent 1a4bc65 commit 5f0c690
Show file tree
Hide file tree
Showing 24 changed files with 312 additions and 233 deletions.
21 changes: 11 additions & 10 deletions echo_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package nill

import (
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

type user struct {
Expand Down Expand Up @@ -92,9 +93,9 @@ func TestFromJson(t *testing.T) {
}

assert.True(t, u.Name.Valid)
assert.Equal(t, "Jon Snow", u.Name.Value)
assert.Equal(t, "Jon Snow", u.Name.V)
assert.True(t, u.Email.Valid)
assert.Equal(t, "[email protected]", u.Email.Value)
assert.Equal(t, "[email protected]", u.Email.V)
})

t.Run("one of the value is null", func(t *testing.T) {
Expand All @@ -110,9 +111,9 @@ func TestFromJson(t *testing.T) {
}

assert.True(t, u.Name.Valid)
assert.Equal(t, "Jon Snow", u.Name.Value)
assert.Equal(t, "Jon Snow", u.Name.V)
assert.False(t, u.Email.Valid)
assert.Equal(t, "", u.Email.Value)
assert.Equal(t, "", u.Email.V)
})

t.Run("one of the value is empty", func(t *testing.T) {
Expand All @@ -128,9 +129,9 @@ func TestFromJson(t *testing.T) {
}

assert.True(t, u.Name.Valid)
assert.Equal(t, "Jon Snow", u.Name.Value)
assert.Equal(t, "Jon Snow", u.Name.V)
assert.True(t, u.Email.Valid)
assert.Equal(t, "", u.Email.Value)
assert.Equal(t, "", u.Email.V)
})

t.Run("only has one value", func(t *testing.T) {
Expand All @@ -146,9 +147,9 @@ func TestFromJson(t *testing.T) {
}

assert.True(t, u.Name.Valid)
assert.Equal(t, "Jon Snow", u.Name.Value)
assert.Equal(t, "Jon Snow", u.Name.V)
assert.False(t, u.Email.Valid)
assert.Equal(t, "", u.Email.Value)
assert.Equal(t, "", u.Email.V)
})
}

Expand Down
9 changes: 5 additions & 4 deletions example_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package nill_test
import (
"encoding/json"
"fmt"
"github.com/RekeningkuDev/nill"
"log"

"github.com/RekeningkuDev/nill"
)

func ExampleTypeMarshal() {
func ExampleType() {
type BarTest struct {
Text nill.Type[string] `json:"text"`
}
Expand All @@ -19,10 +20,10 @@ func ExampleTypeMarshal() {
foo := FooTest{
Bar: nill.Type[BarTest]{
Valid: true,
Value: BarTest{
V: BarTest{
Text: nill.Type[string]{
Valid: true,
Value: "test",
V: "test",
},
},
},
Expand Down
20 changes: 10 additions & 10 deletions float32.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,50 @@ type Float32 Type[float32]

// NewFloat32 creates a new Float32.
func NewFloat32(value float32) Float32 {
return Float32{Valid: true, Value: value}
return Float32{Valid: true, V: value}
}

// Add adds value to Float32.
func (v *Float32) Add(value float32) {
v.Value += value
v.V += value

Check failure on line 13 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.20.x

v.V undefined (type *Float32 has no field or method V)

Check failure on line 13 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.19

v.V undefined (type *Float32 has no field or method V)

Check failure on line 13 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.18

v.V undefined (type *Float32 has no field or method V)
}

// Sub subtracts value from Float32.
func (v *Float32) Sub(value float32) {
v.Value -= value
v.V -= value

Check failure on line 18 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.20.x

v.V undefined (type *Float32 has no field or method V)

Check failure on line 18 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.19

v.V undefined (type *Float32 has no field or method V)

Check failure on line 18 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.18

v.V undefined (type *Float32 has no field or method V)
}

// Mul multiplies Float32 by value.
func (v *Float32) Mul(value float32) {
v.Value *= value
v.V *= value

Check failure on line 23 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.20.x

v.V undefined (type *Float32 has no field or method V)

Check failure on line 23 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.19

v.V undefined (type *Float32 has no field or method V)

Check failure on line 23 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.18

v.V undefined (type *Float32 has no field or method V)
}

// Div divides Float32 by value.
func (v *Float32) Div(value float32) {
v.Value /= value
v.V /= value

Check failure on line 28 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.20.x

v.V undefined (type *Float32 has no field or method V)

Check failure on line 28 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.19

v.V undefined (type *Float32 has no field or method V)

Check failure on line 28 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.18

v.V undefined (type *Float32 has no field or method V)
}

// Inc calculates the remainder of Float32 divided by value.
func (v *Float32) Inc() {
v.Value++
v.V++

Check failure on line 33 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.20.x

v.V undefined (type *Float32 has no field or method V)

Check failure on line 33 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.19

v.V undefined (type *Float32 has no field or method V)

Check failure on line 33 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.18

v.V undefined (type *Float32 has no field or method V)
}

// Dec decrements Float32 by 1.
func (v *Float32) Dec() {
v.Value--
v.V--

Check failure on line 38 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.20.x

v.V undefined (type *Float32 has no field or method V)

Check failure on line 38 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.19

v.V undefined (type *Float32 has no field or method V)

Check failure on line 38 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.18

v.V undefined (type *Float32 has no field or method V)
}

// Zero returns true if Float32 is zero.
func (v *Float32) Zero() bool {
return v.Value == 0
return v.V == 0

Check failure on line 43 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.20.x

v.V undefined (type *Float32 has no field or method V)

Check failure on line 43 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.19

v.V undefined (type *Float32 has no field or method V)

Check failure on line 43 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.18

v.V undefined (type *Float32 has no field or method V)
}

// Positive returns true if Float32 is positive.
func (v *Float32) Positive() bool {
return v.Value > 0
return v.V > 0

Check failure on line 48 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.20.x

v.V undefined (type *Float32 has no field or method V)

Check failure on line 48 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.19

v.V undefined (type *Float32 has no field or method V)

Check failure on line 48 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.18

v.V undefined (type *Float32 has no field or method V)
}

// Negative returns true if Float32 is negative.
func (v *Float32) Negative() bool {
return v.Value < 0
return v.V < 0

Check failure on line 53 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.20.x

v.V undefined (type *Float32 has no field or method V)

Check failure on line 53 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.19

v.V undefined (type *Float32 has no field or method V)

Check failure on line 53 in float32.go

View workflow job for this annotation

GitHub Actions / Go 1.18

v.V undefined (type *Float32 has no field or method V)
}
15 changes: 8 additions & 7 deletions float32_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
package nill

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFloat32(t *testing.T) {
t.Run("add", func(t *testing.T) {
value := NewFloat32(1.5)
value.Add(1)
assert.Equal(t, Float32{Valid: true, Value: 2.5}, value)
assert.Equal(t, Float32{Valid: true, V: 2.5}, value)
})
t.Run("sub", func(t *testing.T) {
value := NewFloat32(1.5)
value.Sub(1)
assert.Equal(t, Float32{Valid: true, Value: 0.5}, value)
assert.Equal(t, Float32{Valid: true, V: 0.5}, value)
})
t.Run("mul", func(t *testing.T) {
value := NewFloat32(1.5)
value.Mul(5)
assert.Equal(t, Float32{Valid: true, Value: 7.5}, value)
assert.Equal(t, Float32{Valid: true, V: 7.5}, value)
})
t.Run("div", func(t *testing.T) {
value := NewFloat32(1.5)
value.Div(5)
assert.Equal(t, Float32{Valid: true, Value: 0.3}, value)
assert.Equal(t, Float32{Valid: true, V: 0.3}, value)
})
t.Run("inc", func(t *testing.T) {
value := NewFloat32(1.5)
value.Inc()
assert.Equal(t, Float32{Valid: true, Value: 2.5}, value)
assert.Equal(t, Float32{Valid: true, V: 2.5}, value)
})
t.Run("dec", func(t *testing.T) {
value := NewFloat32(1.5)
value.Dec()
assert.Equal(t, Float32{Valid: true, Value: 0.5}, value)
assert.Equal(t, Float32{Valid: true, V: 0.5}, value)
})
t.Run("zero", func(t *testing.T) {
value := NewFloat32(0)
Expand Down
20 changes: 10 additions & 10 deletions float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,50 @@ package nill
type Float64 Type[float64]

func NewFloat64(value float64) Float64 {
return Float64{Valid: true, Value: value}
return Float64{Valid: true, V: value}
}

// Add adds value to Float32.
func (v *Float64) Add(value float64) {
v.Value += value
v.V += value
}

// Sub subtracts value from Float32.
func (v *Float64) Sub(value float64) {
v.Value -= value
v.V -= value
}

// Mul multiplies Float32 by value.
func (v *Float64) Mul(value float64) {
v.Value *= value
v.V *= value
}

// Div divides Float32 by value.
func (v *Float64) Div(value float64) {
v.Value /= value
v.V /= value
}

// Inc calculates the remainder of Float32 divided by value.
func (v *Float64) Inc() {
v.Value++
v.V++
}

// Dec decrements Float32 by 1.
func (v *Float64) Dec() {
v.Value--
v.V--
}

// Zero returns true if Float32 is zero.
func (v *Float64) Zero() bool {
return v.Value == 0
return v.V == 0
}

// Positive returns true if Float32 is positive.
func (v *Float64) Positive() bool {
return v.Value > 0
return v.V > 0
}

// Negative returns true if Float32 is negative.
func (v *Float64) Negative() bool {
return v.Value < 0
return v.V < 0
}
15 changes: 8 additions & 7 deletions float64_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
package nill

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFloat64(t *testing.T) {
t.Run("add", func(t *testing.T) {
value := NewFloat64(1.5)
value.Add(1)
assert.Equal(t, Float64{Valid: true, Value: 2.5}, value)
assert.Equal(t, Float64{Valid: true, V: 2.5}, value)
})
t.Run("sub", func(t *testing.T) {
value := NewFloat64(1.5)
value.Sub(1)
assert.Equal(t, Float64{Valid: true, Value: 0.5}, value)
assert.Equal(t, Float64{Valid: true, V: 0.5}, value)
})
t.Run("mul", func(t *testing.T) {
value := NewFloat64(1.5)
value.Mul(5)
assert.Equal(t, Float64{Valid: true, Value: 7.5}, value)
assert.Equal(t, Float64{Valid: true, V: 7.5}, value)
})
t.Run("div", func(t *testing.T) {
value := NewFloat64(1.5)
value.Div(5)
assert.Equal(t, Float64{Valid: true, Value: 0.3}, value)
assert.Equal(t, Float64{Valid: true, V: 0.3}, value)
})
t.Run("inc", func(t *testing.T) {
value := NewFloat64(1.5)
value.Inc()
assert.Equal(t, Float64{Valid: true, Value: 2.5}, value)
assert.Equal(t, Float64{Valid: true, V: 2.5}, value)
})
t.Run("dec", func(t *testing.T) {
value := NewFloat64(1.5)
value.Dec()
assert.Equal(t, Float64{Valid: true, Value: 0.5}, value)
assert.Equal(t, Float64{Valid: true, V: 0.5}, value)
})
t.Run("zero", func(t *testing.T) {
value := NewFloat64(0)
Expand Down
Loading

0 comments on commit 5f0c690

Please sign in to comment.