diff --git a/echo_test.go b/echo_test.go index aff3330..2bdc317 100644 --- a/echo_test.go +++ b/echo_test.go @@ -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 { @@ -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, "jon@labstack.com", u.Email.Value) + assert.Equal(t, "jon@labstack.com", u.Email.V) }) t.Run("one of the value is null", func(t *testing.T) { @@ -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) { @@ -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) { @@ -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) }) } diff --git a/example_json_test.go b/example_json_test.go index eb2a583..c7bd6d0 100644 --- a/example_json_test.go +++ b/example_json_test.go @@ -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"` } @@ -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", }, }, }, diff --git a/float32.go b/float32.go index ac831b4..ef7e422 100644 --- a/float32.go +++ b/float32.go @@ -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 } // Sub subtracts value from Float32. func (v *Float32) Sub(value float32) { - v.Value -= value + v.V -= value } // Mul multiplies Float32 by value. func (v *Float32) Mul(value float32) { - v.Value *= value + v.V *= value } // Div divides Float32 by value. func (v *Float32) Div(value float32) { - v.Value /= value + v.V /= value } // Inc calculates the remainder of Float32 divided by value. func (v *Float32) Inc() { - v.Value++ + v.V++ } // Dec decrements Float32 by 1. func (v *Float32) Dec() { - v.Value-- + v.V-- } // Zero returns true if Float32 is zero. func (v *Float32) Zero() bool { - return v.Value == 0 + return v.V == 0 } // Positive returns true if Float32 is positive. func (v *Float32) Positive() bool { - return v.Value > 0 + return v.V > 0 } // Negative returns true if Float32 is negative. func (v *Float32) Negative() bool { - return v.Value < 0 + return v.V < 0 } diff --git a/float32_test.go b/float32_test.go index cc9c284..8ef5a80 100644 --- a/float32_test.go +++ b/float32_test.go @@ -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) diff --git a/float64.go b/float64.go index 0bb9d7b..172e93b 100644 --- a/float64.go +++ b/float64.go @@ -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 } diff --git a/float64_test.go b/float64_test.go index d8b11d6..18d679b 100644 --- a/float64_test.go +++ b/float64_test.go @@ -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) diff --git a/int.go b/int.go index 7b01271..e9010ea 100644 --- a/int.go +++ b/int.go @@ -5,112 +5,112 @@ type Int Type[int] // NewInt creates a new Int. func NewInt(value int) Int { - return Int{Valid: true, Value: value} + return Int{Valid: true, V: value} } // Add adds value to Int. func (v *Int) Add(value int) { - v.Value += value + v.V += value } // Sub subtracts value from Int. func (v *Int) Sub(value int) { - v.Value -= value + v.V -= value } // Mul multiplies Int by value. func (v *Int) Mul(value int) { - v.Value *= value + v.V *= value } // Div divides Int by value. func (v *Int) Div(value int) { - v.Value /= value + v.V /= value } // Mod calculates the remainder of Int divided by value. func (v *Int) Mod(value int) { - v.Value %= value + v.V %= value } // Inc increments Int by 1. func (v *Int) Inc() { - v.Value++ + v.V++ } // Dec decrements Int by 1. func (v *Int) Dec() { - v.Value-- + v.V-- } // Zero returns true if Int is zero. func (v *Int) Zero() bool { - return v.Value == 0 + return v.V == 0 } // Positive returns true if Int is positive. func (v *Int) Positive() bool { - return v.Value > 0 + return v.V > 0 } // Negative returns true if Int is negative. func (v *Int) Negative() bool { - return v.Value < 0 + return v.V < 0 } // Even returns true if Int is even. func (v *Int) Even() bool { - return v.Value%2 == 0 + return v.V%2 == 0 } // Odd returns true if Int is odd. func (v *Int) Odd() bool { - return v.Value%2 != 0 + return v.V%2 != 0 } // GreaterThan returns true if Int is greater than value. func (v *Int) GreaterThan(value int) bool { - return v.Value > value + return v.V > value } // GreaterThanOrEqual returns true if Int is greater than or equal to value. func (v *Int) GreaterThanOrEqual(value int) bool { - return v.Value >= value + return v.V >= value } // LessThan returns true if Int is less than value. func (v *Int) LessThan(value int) bool { - return v.Value < value + return v.V < value } // LessThanOrEqual returns true if Int is less than or equal to value. func (v *Int) LessThanOrEqual(value int) bool { - return v.Value <= value + return v.V <= value } // Equal returns true if Int is equal to value. func (v *Int) Equal(value int) bool { - return v.Value == value + return v.V == value } // NotEqual returns true if Int is not equal to value. func (v *Int) NotEqual(value int) bool { - return v.Value != value + return v.V != value } // DivisibleBy returns true if Int is divisible by value. func (v *Int) DivisibleBy(value int) bool { - return v.Value%value == 0 + return v.V%value == 0 } // Prime returns true if Int is a prime number. func (v *Int) Prime() bool { - if v.Value <= 1 { + if v.V <= 1 { return false } - for i := 2; i < v.Value; i++ { - if v.Value%i == 0 { + for i := 2; i < v.V; i++ { + if v.V%i == 0 { return false } } diff --git a/int16.go b/int16.go index 33aa45a..1dba7fc 100644 --- a/int16.go +++ b/int16.go @@ -5,112 +5,112 @@ type Int16 Type[int16] // NewInt16 creates a new Int16. func NewInt16(value int16) Int16 { - return Int16{Valid: true, Value: value} + return Int16{Valid: true, V: value} } // Add adds value to Int16. func (v *Int16) Add(value int16) { - v.Value += value + v.V += value } // Sub subtracts value from Int16. func (v *Int16) Sub(value int16) { - v.Value -= value + v.V -= value } // Mul multiplies Int16 by value. func (v *Int16) Mul(value int16) { - v.Value *= value + v.V *= value } // Div divides Int16 by value. func (v *Int16) Div(value int16) { - v.Value /= value + v.V /= value } // Mod calculates the remainder of Int16 divided by value. func (v *Int16) Mod(value int16) { - v.Value %= value + v.V %= value } // Inc increments Int16 by 1. func (v *Int16) Inc() { - v.Value++ + v.V++ } // Dec decrements Int16 by 1. func (v *Int16) Dec() { - v.Value-- + v.V-- } // Zero returns true if Int16 is zero. func (v *Int16) Zero() bool { - return v.Value == 0 + return v.V == 0 } // Positive returns true if Int16 is positive. func (v *Int16) Positive() bool { - return v.Value > 0 + return v.V > 0 } // Negative returns true if Int16 is negative. func (v *Int16) Negative() bool { - return v.Value < 0 + return v.V < 0 } // Even returns true if Int16 is even. func (v *Int16) Even() bool { - return v.Value%2 == 0 + return v.V%2 == 0 } // Odd returns true if Int16 is odd. func (v *Int16) Odd() bool { - return v.Value%2 != 0 + return v.V%2 != 0 } // GreaterThan returns true if Int16 is greater than value. func (v *Int16) GreaterThan(value int16) bool { - return v.Value > value + return v.V > value } // GreaterThanOrEqual returns true if Int16 is greater than or equal to value. func (v *Int16) GreaterThanOrEqual(value int16) bool { - return v.Value >= value + return v.V >= value } // LessThan returns true if Int16 is less than value. func (v *Int16) LessThan(value int16) bool { - return v.Value < value + return v.V < value } // LessThanOrEqual returns true if Int16 is less than or equal to value. func (v *Int16) LessThanOrEqual(value int16) bool { - return v.Value <= value + return v.V <= value } // Equal returns true if Int16 is equal to value. func (v *Int16) Equal(value int16) bool { - return v.Value == value + return v.V == value } // NotEqual returns true if Int16 is not equal to value. func (v *Int16) NotEqual(value int16) bool { - return v.Value != value + return v.V != value } // DivisibleBy returns true if Int16 is divisible by value. func (v *Int16) DivisibleBy(value int16) bool { - return v.Value%value == 0 + return v.V%value == 0 } // Prime returns true if Int16 is a prime number. func (v *Int16) Prime() bool { - if v.Value <= 1 { + if v.V <= 1 { return false } - for i := int16(2); i < v.Value; i++ { - if v.Value%i == 0 { + for i := int16(2); i < v.V; i++ { + if v.V%i == 0 { return false } } diff --git a/int16_test.go b/int16_test.go index c9562c8..fe708fd 100644 --- a/int16_test.go +++ b/int16_test.go @@ -1,45 +1,46 @@ package nill import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestInt16(t *testing.T) { t.Run("add", func(t *testing.T) { value := NewInt16(1) value.Add(1) - assert.Equal(t, Int16{Valid: true, Value: 2}, value) + assert.Equal(t, Int16{Valid: true, V: 2}, value) }) t.Run("sub", func(t *testing.T) { value := NewInt16(1) value.Sub(1) - assert.Equal(t, Int16{Valid: true, Value: 0}, value) + assert.Equal(t, Int16{Valid: true, V: 0}, value) }) t.Run("mul", func(t *testing.T) { value := NewInt16(3) value.Mul(5) - assert.Equal(t, Int16{Valid: true, Value: 15}, value) + assert.Equal(t, Int16{Valid: true, V: 15}, value) }) t.Run("div", func(t *testing.T) { value := NewInt16(10) value.Div(5) - assert.Equal(t, Int16{Valid: true, Value: 2}, value) + assert.Equal(t, Int16{Valid: true, V: 2}, value) }) t.Run("mod", func(t *testing.T) { value := NewInt16(1) value.Mod(5) - assert.Equal(t, Int16{Valid: true, Value: 1}, value) + assert.Equal(t, Int16{Valid: true, V: 1}, value) }) t.Run("inc", func(t *testing.T) { value := NewInt16(1) value.Inc() - assert.Equal(t, Int16{Valid: true, Value: 2}, value) + assert.Equal(t, Int16{Valid: true, V: 2}, value) }) t.Run("dec", func(t *testing.T) { value := NewInt16(1) value.Dec() - assert.Equal(t, Int16{Valid: true, Value: 0}, value) + assert.Equal(t, Int16{Valid: true, V: 0}, value) }) t.Run("zero", func(t *testing.T) { value := NewInt16(0) diff --git a/int32.go b/int32.go index 6107f4b..52fdf01 100644 --- a/int32.go +++ b/int32.go @@ -5,112 +5,112 @@ type Int32 Type[int32] // NewInt32 creates a new Int32. func NewInt32(value int32) Int32 { - return Int32{Valid: true, Value: value} + return Int32{Valid: true, V: value} } // Add adds value to Int32. func (v *Int32) Add(value int32) { - v.Value += value + v.V += value } // Sub subtracts value from Int32. func (v *Int32) Sub(value int32) { - v.Value -= value + v.V -= value } // Mul multiplies Int32 by value. func (v *Int32) Mul(value int32) { - v.Value *= value + v.V *= value } // Div divides Int32 by value. func (v *Int32) Div(value int32) { - v.Value /= value + v.V /= value } // Mod calculates the remainder of Int32 divided by value. func (v *Int32) Mod(value int32) { - v.Value %= value + v.V %= value } // Inc increments Int32 by 1. func (v *Int32) Inc() { - v.Value++ + v.V++ } // Dec decrements Int32 by 1. func (v *Int32) Dec() { - v.Value-- + v.V-- } // Zero returns true if Int32 is zero. func (v *Int32) Zero() bool { - return v.Value == 0 + return v.V == 0 } // Positive returns true if Int32 is positive. func (v *Int32) Positive() bool { - return v.Value > 0 + return v.V > 0 } // Negative returns true if Int32 is negative. func (v *Int32) Negative() bool { - return v.Value < 0 + return v.V < 0 } // Even returns true if Int32 is even. func (v *Int32) Even() bool { - return v.Value%2 == 0 + return v.V%2 == 0 } // Odd returns true if Int32 is odd. func (v *Int32) Odd() bool { - return v.Value%2 != 0 + return v.V%2 != 0 } // GreaterThan returns true if Int32 is greater than value. func (v *Int32) GreaterThan(value int32) bool { - return v.Value > value + return v.V > value } // GreaterThanOrEqual returns true if Int32 is greater than or equal to value. func (v *Int32) GreaterThanOrEqual(value int32) bool { - return v.Value >= value + return v.V >= value } // LessThan returns true if Int32 is less than value. func (v *Int32) LessThan(value int32) bool { - return v.Value < value + return v.V < value } // LessThanOrEqual returns true if Int32 is less than or equal to value. func (v *Int32) LessThanOrEqual(value int32) bool { - return v.Value <= value + return v.V <= value } // Equal returns true if Int32 is equal to value. func (v *Int32) Equal(value int32) bool { - return v.Value == value + return v.V == value } // NotEqual returns true if Int32 is not equal to value. func (v *Int32) NotEqual(value int32) bool { - return v.Value != value + return v.V != value } // DivisibleBy returns true if Int32 is divisible by value. func (v *Int32) DivisibleBy(value int32) bool { - return v.Value%value == 0 + return v.V%value == 0 } // Prime returns true if Int32 is a prime number. func (v *Int32) Prime() bool { - if v.Value <= 1 { + if v.V <= 1 { return false } - for i := int32(2); i < v.Value; i++ { - if v.Value%i == 0 { + for i := int32(2); i < v.V; i++ { + if v.V%i == 0 { return false } } diff --git a/int32_test.go b/int32_test.go index 88170bd..5641ded 100644 --- a/int32_test.go +++ b/int32_test.go @@ -1,45 +1,46 @@ package nill import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestInt32(t *testing.T) { t.Run("add", func(t *testing.T) { value := NewInt32(1) value.Add(1) - assert.Equal(t, Int32{Valid: true, Value: 2}, value) + assert.Equal(t, Int32{Valid: true, V: 2}, value) }) t.Run("sub", func(t *testing.T) { value := NewInt32(1) value.Sub(1) - assert.Equal(t, Int32{Valid: true, Value: 0}, value) + assert.Equal(t, Int32{Valid: true, V: 0}, value) }) t.Run("mul", func(t *testing.T) { value := NewInt32(3) value.Mul(5) - assert.Equal(t, Int32{Valid: true, Value: 15}, value) + assert.Equal(t, Int32{Valid: true, V: 15}, value) }) t.Run("div", func(t *testing.T) { value := NewInt32(10) value.Div(5) - assert.Equal(t, Int32{Valid: true, Value: 2}, value) + assert.Equal(t, Int32{Valid: true, V: 2}, value) }) t.Run("mod", func(t *testing.T) { value := NewInt32(1) value.Mod(5) - assert.Equal(t, Int32{Valid: true, Value: 1}, value) + assert.Equal(t, Int32{Valid: true, V: 1}, value) }) t.Run("inc", func(t *testing.T) { value := NewInt32(1) value.Inc() - assert.Equal(t, Int32{Valid: true, Value: 2}, value) + assert.Equal(t, Int32{Valid: true, V: 2}, value) }) t.Run("dec", func(t *testing.T) { value := NewInt32(1) value.Dec() - assert.Equal(t, Int32{Valid: true, Value: 0}, value) + assert.Equal(t, Int32{Valid: true, V: 0}, value) }) t.Run("zero", func(t *testing.T) { value := NewInt32(0) diff --git a/int64.go b/int64.go index f7228ac..b92271b 100644 --- a/int64.go +++ b/int64.go @@ -5,112 +5,112 @@ type Int64 Type[int64] // NewInt64 creates a new Int64. func NewInt64(value int64) Int64 { - return Int64{Valid: true, Value: value} + return Int64{Valid: true, V: value} } // Add adds value to Int64. func (v *Int64) Add(value int64) { - v.Value += value + v.V += value } // Sub subtracts value from Int64. func (v *Int64) Sub(value int64) { - v.Value -= value + v.V -= value } // Mul multiplies Int64 by value. func (v *Int64) Mul(value int64) { - v.Value *= value + v.V *= value } // Div divides Int64 by value. func (v *Int64) Div(value int64) { - v.Value /= value + v.V /= value } // Mod calculates the remainder of Int64 divided by value. func (v *Int64) Mod(value int64) { - v.Value %= value + v.V %= value } // Inc increments Int64 by 1. func (v *Int64) Inc() { - v.Value++ + v.V++ } // Dec decrements Int64 by 1. func (v *Int64) Dec() { - v.Value-- + v.V-- } // Zero returns true if Int64 is zero. func (v *Int64) Zero() bool { - return v.Value == 0 + return v.V == 0 } // Positive returns true if Int64 is positive. func (v *Int64) Positive() bool { - return v.Value > 0 + return v.V > 0 } // Negative returns true if Int64 is negative. func (v *Int64) Negative() bool { - return v.Value < 0 + return v.V < 0 } // Even returns true if Int64 is even. func (v *Int64) Even() bool { - return v.Value%2 == 0 + return v.V%2 == 0 } // Odd returns true if Int64 is odd. func (v *Int64) Odd() bool { - return v.Value%2 != 0 + return v.V%2 != 0 } // GreaterThan returns true if Int64 is greater than value. func (v *Int64) GreaterThan(value int64) bool { - return v.Value > value + return v.V > value } // GreaterThanOrEqual returns true if Int64 is greater than or equal to value. func (v *Int64) GreaterThanOrEqual(value int64) bool { - return v.Value >= value + return v.V >= value } // LessThan returns true if Int64 is less than value. func (v *Int64) LessThan(value int64) bool { - return v.Value < value + return v.V < value } // LessThanOrEqual returns true if Int64 is less than or equal to value. func (v *Int64) LessThanOrEqual(value int64) bool { - return v.Value <= value + return v.V <= value } // Equal returns true if Int64 is equal to value. func (v *Int64) Equal(value int64) bool { - return v.Value == value + return v.V == value } // NotEqual returns true if Int64 is not equal to value. func (v *Int64) NotEqual(value int64) bool { - return v.Value != value + return v.V != value } // DivisibleBy returns true if Int64 is divisible by value. func (v *Int64) DivisibleBy(value int64) bool { - return v.Value%value == 0 + return v.V%value == 0 } // Prime returns true if Int64 is a prime number. func (v *Int64) Prime() bool { - if v.Value <= 1 { + if v.V <= 1 { return false } - for i := int64(2); i < v.Value; i++ { - if v.Value%i == 0 { + for i := int64(2); i < v.V; i++ { + if v.V%i == 0 { return false } } diff --git a/int64_test.go b/int64_test.go index ea63216..27b8f5a 100644 --- a/int64_test.go +++ b/int64_test.go @@ -1,45 +1,46 @@ package nill import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestInt64(t *testing.T) { t.Run("add", func(t *testing.T) { value := NewInt64(1) value.Add(1) - assert.Equal(t, Int64{Valid: true, Value: 2}, value) + assert.Equal(t, Int64{Valid: true, V: 2}, value) }) t.Run("sub", func(t *testing.T) { value := NewInt64(1) value.Sub(1) - assert.Equal(t, Int64{Valid: true, Value: 0}, value) + assert.Equal(t, Int64{Valid: true, V: 0}, value) }) t.Run("mul", func(t *testing.T) { value := NewInt64(3) value.Mul(5) - assert.Equal(t, Int64{Valid: true, Value: 15}, value) + assert.Equal(t, Int64{Valid: true, V: 15}, value) }) t.Run("div", func(t *testing.T) { value := NewInt64(10) value.Div(5) - assert.Equal(t, Int64{Valid: true, Value: 2}, value) + assert.Equal(t, Int64{Valid: true, V: 2}, value) }) t.Run("mod", func(t *testing.T) { value := NewInt64(1) value.Mod(5) - assert.Equal(t, Int64{Valid: true, Value: 1}, value) + assert.Equal(t, Int64{Valid: true, V: 1}, value) }) t.Run("inc", func(t *testing.T) { value := NewInt64(1) value.Inc() - assert.Equal(t, Int64{Valid: true, Value: 2}, value) + assert.Equal(t, Int64{Valid: true, V: 2}, value) }) t.Run("dec", func(t *testing.T) { value := NewInt64(1) value.Dec() - assert.Equal(t, Int64{Valid: true, Value: 0}, value) + assert.Equal(t, Int64{Valid: true, V: 0}, value) }) t.Run("zero", func(t *testing.T) { value := NewInt64(0) diff --git a/int8.go b/int8.go index f5bc7c4..7e636c1 100644 --- a/int8.go +++ b/int8.go @@ -5,112 +5,112 @@ type Int8 Type[int8] // NewInt8 creates a new Int8. func NewInt8(value int8) Int8 { - return Int8{Valid: true, Value: value} + return Int8{Valid: true, V: value} } // Add adds value to Int8. func (v *Int8) Add(value int8) { - v.Value += value + v.V += value } // Sub subtracts value from Int8. func (v *Int8) Sub(value int8) { - v.Value -= value + v.V -= value } // Mul multiplies Int8 by value. func (v *Int8) Mul(value int8) { - v.Value *= value + v.V *= value } // Div divides Int8 by value. func (v *Int8) Div(value int8) { - v.Value /= value + v.V /= value } // Mod calculates the remainder of Int8 divided by value. func (v *Int8) Mod(value int8) { - v.Value %= value + v.V %= value } // Inc increments Int8 by 1. func (v *Int8) Inc() { - v.Value++ + v.V++ } // Dec decrements Int8 by 1. func (v *Int8) Dec() { - v.Value-- + v.V-- } // Zero returns true if Int8 is zero. func (v *Int8) Zero() bool { - return v.Value == 0 + return v.V == 0 } // Positive returns true if Int8 is positive. func (v *Int8) Positive() bool { - return v.Value > 0 + return v.V > 0 } // Negative returns true if Int8 is negative. func (v *Int8) Negative() bool { - return v.Value < 0 + return v.V < 0 } // Even returns true if Int8 is even. func (v *Int8) Even() bool { - return v.Value%2 == 0 + return v.V%2 == 0 } // Odd returns true if Int8 is odd. func (v *Int8) Odd() bool { - return v.Value%2 != 0 + return v.V%2 != 0 } // GreaterThan returns true if Int8 is greater than value. func (v *Int8) GreaterThan(value int8) bool { - return v.Value > value + return v.V > value } // GreaterThanOrEqual returns true if Int8 is greater than or equal to value. func (v *Int8) GreaterThanOrEqual(value int8) bool { - return v.Value >= value + return v.V >= value } // LessThan returns true if Int8 is less than value. func (v *Int8) LessThan(value int8) bool { - return v.Value < value + return v.V < value } // LessThanOrEqual returns true if Int8 is less than or equal to value. func (v *Int8) LessThanOrEqual(value int8) bool { - return v.Value <= value + return v.V <= value } // Equal returns true if Int8 is equal to value. func (v *Int8) Equal(value int8) bool { - return v.Value == value + return v.V == value } // NotEqual returns true if Int8 is not equal to value. func (v *Int8) NotEqual(value int8) bool { - return v.Value != value + return v.V != value } // DivisibleBy returns true if Int8 is divisible by value. func (v *Int8) DivisibleBy(value int8) bool { - return v.Value%value == 0 + return v.V%value == 0 } // Prime returns true if Int8 is a prime number. func (v *Int8) Prime() bool { - if v.Value <= 1 { + if v.V <= 1 { return false } - for i := int8(2); i < v.Value; i++ { - if v.Value%i == 0 { + for i := int8(2); i < v.V; i++ { + if v.V%i == 0 { return false } } diff --git a/int8_test.go b/int8_test.go index 18d060a..213017d 100644 --- a/int8_test.go +++ b/int8_test.go @@ -1,45 +1,46 @@ package nill import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestInt8(t *testing.T) { t.Run("add", func(t *testing.T) { value := NewInt8(1) value.Add(1) - assert.Equal(t, Int8{Valid: true, Value: 2}, value) + assert.Equal(t, Int8{Valid: true, V: 2}, value) }) t.Run("sub", func(t *testing.T) { value := NewInt8(1) value.Sub(1) - assert.Equal(t, Int8{Valid: true, Value: 0}, value) + assert.Equal(t, Int8{Valid: true, V: 0}, value) }) t.Run("mul", func(t *testing.T) { value := NewInt8(3) value.Mul(5) - assert.Equal(t, Int8{Valid: true, Value: 15}, value) + assert.Equal(t, Int8{Valid: true, V: 15}, value) }) t.Run("div", func(t *testing.T) { value := NewInt8(10) value.Div(5) - assert.Equal(t, Int8{Valid: true, Value: 2}, value) + assert.Equal(t, Int8{Valid: true, V: 2}, value) }) t.Run("mod", func(t *testing.T) { value := NewInt8(1) value.Mod(5) - assert.Equal(t, Int8{Valid: true, Value: 1}, value) + assert.Equal(t, Int8{Valid: true, V: 1}, value) }) t.Run("inc", func(t *testing.T) { value := NewInt8(1) value.Inc() - assert.Equal(t, Int8{Valid: true, Value: 2}, value) + assert.Equal(t, Int8{Valid: true, V: 2}, value) }) t.Run("dec", func(t *testing.T) { value := NewInt8(1) value.Dec() - assert.Equal(t, Int8{Valid: true, Value: 0}, value) + assert.Equal(t, Int8{Valid: true, V: 0}, value) }) t.Run("zero", func(t *testing.T) { value := NewInt8(0) diff --git a/int_test.go b/int_test.go index a3a2dab..42ced1b 100644 --- a/int_test.go +++ b/int_test.go @@ -1,45 +1,46 @@ package nill import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestInt(t *testing.T) { t.Run("add", func(t *testing.T) { value := NewInt(1) value.Add(1) - assert.Equal(t, Int{Valid: true, Value: 2}, value) + assert.Equal(t, Int{Valid: true, V: 2}, value) }) t.Run("sub", func(t *testing.T) { value := NewInt(1) value.Sub(1) - assert.Equal(t, Int{Valid: true, Value: 0}, value) + assert.Equal(t, Int{Valid: true, V: 0}, value) }) t.Run("mul", func(t *testing.T) { value := NewInt(3) value.Mul(5) - assert.Equal(t, Int{Valid: true, Value: 15}, value) + assert.Equal(t, Int{Valid: true, V: 15}, value) }) t.Run("div", func(t *testing.T) { value := NewInt(10) value.Div(5) - assert.Equal(t, Int{Valid: true, Value: 2}, value) + assert.Equal(t, Int{Valid: true, V: 2}, value) }) t.Run("mod", func(t *testing.T) { value := NewInt(1) value.Mod(5) - assert.Equal(t, Int{Valid: true, Value: 1}, value) + assert.Equal(t, Int{Valid: true, V: 1}, value) }) t.Run("inc", func(t *testing.T) { value := NewInt(1) value.Inc() - assert.Equal(t, Int{Valid: true, Value: 2}, value) + assert.Equal(t, Int{Valid: true, V: 2}, value) }) t.Run("dec", func(t *testing.T) { value := NewInt(1) value.Dec() - assert.Equal(t, Int{Valid: true, Value: 0}, value) + assert.Equal(t, Int{Valid: true, V: 0}, value) }) t.Run("zero", func(t *testing.T) { value := NewInt(0) diff --git a/json.go b/json.go index d5f771d..9b962f8 100644 --- a/json.go +++ b/json.go @@ -11,7 +11,7 @@ func (n *Type[T]) UnmarshalJSON(data []byte) error { return err } - n.Value = value + n.V = value n.Valid = true return nil } @@ -21,5 +21,5 @@ func (n Type[T]) MarshalJSON() ([]byte, error) { return JSONMarshal(nil) } - return JSONMarshal(n.Value) + return JSONMarshal(n.V) } diff --git a/json_marshal_benchmark_test.go b/json_marshal_benchmark_test.go index 917ab96..fbc1e41 100644 --- a/json_marshal_benchmark_test.go +++ b/json_marshal_benchmark_test.go @@ -1,9 +1,10 @@ package nill import ( + "testing" + goJson "github.com/goccy/go-json" "github.com/stretchr/testify/assert" - "testing" ) type BarBenchmarkMarshal struct { @@ -15,7 +16,7 @@ func BenchmarkMarshal(b *testing.B) { foo := BarBenchmarkMarshal{ Text: Type[string]{ Valid: true, - Value: "test", + V: "test", }, } @@ -58,7 +59,7 @@ func BenchmarkMarshal_GoJson(b *testing.B) { foo := BarBenchmarkMarshal{ Text: Type[string]{ Valid: true, - Value: "test", + V: "test", }, } diff --git a/json_marshal_nested_benchmark_test.go b/json_marshal_nested_benchmark_test.go index b88848a..1082e7c 100644 --- a/json_marshal_nested_benchmark_test.go +++ b/json_marshal_nested_benchmark_test.go @@ -2,8 +2,9 @@ package nill import ( "encoding/json" - goJson "github.com/goccy/go-json" "testing" + + goJson "github.com/goccy/go-json" ) type BarBenchmarkNestedMarshal struct { @@ -21,10 +22,10 @@ func BenchmarkNestedMarshal(b *testing.B) { foo := FooBenchmarkNestedMarshal{ Bar: Type[BarBenchmarkNestedMarshal]{ Valid: true, - Value: BarBenchmarkNestedMarshal{ + V: BarBenchmarkNestedMarshal{ Text: Type[string]{ Valid: true, - Value: "test", + V: "test", }, }, }, @@ -69,10 +70,10 @@ func BenchmarkNestedMarshal_GoJson(b *testing.B) { foo := FooBenchmarkNestedMarshal{ Bar: Type[BarBenchmarkNestedMarshal]{ Valid: true, - Value: BarBenchmarkNestedMarshal{ + V: BarBenchmarkNestedMarshal{ Text: Type[string]{ Valid: true, - Value: "test", + V: "test", }, }, }, diff --git a/json_test.go b/json_test.go index 83ed6a0..208e0d2 100644 --- a/json_test.go +++ b/json_test.go @@ -2,8 +2,9 @@ package nill import ( "encoding/json" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) type BarTest struct { @@ -23,8 +24,8 @@ func TestType_UnmarshalJSON(t *testing.T) { } assert.True(t, foo.Bar.Valid) - assert.True(t, foo.Bar.Value.Text.Valid) - assert.Equal(t, foo.Bar.Value.Text.Value, "test") + assert.True(t, foo.Bar.V.Text.Valid) + assert.Equal(t, foo.Bar.V.Text.V, "test") }) t.Run("should unmarshal json Baz with null value", func(t *testing.T) { @@ -36,8 +37,8 @@ func TestType_UnmarshalJSON(t *testing.T) { } assert.True(t, foo.Bar.Valid) - assert.False(t, foo.Bar.Value.Text.Valid) - assert.Equal(t, foo.Bar.Value.Text.Value, "") + assert.False(t, foo.Bar.V.Text.Valid) + assert.Equal(t, foo.Bar.V.Text.V, "") }) t.Run("should unmarshal json BarTest with empty value", func(t *testing.T) { @@ -49,8 +50,8 @@ func TestType_UnmarshalJSON(t *testing.T) { } assert.True(t, foo.Bar.Valid) - assert.False(t, foo.Bar.Value.Text.Valid) - assert.Equal(t, foo.Bar.Value.Text.Value, "") + assert.False(t, foo.Bar.V.Text.Valid) + assert.Equal(t, foo.Bar.V.Text.V, "") }) t.Run("should unmarshal json with empty value", func(t *testing.T) { @@ -62,8 +63,8 @@ func TestType_UnmarshalJSON(t *testing.T) { } assert.False(t, foo.Bar.Valid) - assert.False(t, foo.Bar.Value.Text.Valid) - assert.Equal(t, foo.Bar.Value.Text.Value, "") + assert.False(t, foo.Bar.V.Text.Valid) + assert.Equal(t, foo.Bar.V.Text.V, "") }) } @@ -72,10 +73,10 @@ func TestType_MarshalJSON(t *testing.T) { foo := FooTest{ Bar: Type[BarTest]{ Valid: true, - Value: BarTest{ + V: BarTest{ Text: Type[string]{ Valid: true, - Value: "test", + V: "test", }, }, }, @@ -93,10 +94,10 @@ func TestType_MarshalJSON(t *testing.T) { foo := FooTest{ Bar: Type[BarTest]{ Valid: true, - Value: BarTest{ + V: BarTest{ Text: Type[string]{ Valid: false, - Value: "", + V: "", }, }, }, diff --git a/nill.go b/nill.go index 02f7f81..526bbd3 100644 --- a/nill.go +++ b/nill.go @@ -1,10 +1,9 @@ package nill -type Type[T any] struct { - Valid bool - Value T -} +import "database/sql" + +type Type[T any] sql.Null[T] -func New[T any](value T) Type[T] { - return Type[T]{Valid: true, Value: value} +func New[T any](v T) Type[T] { + return Type[T]{Valid: true, V: v} } diff --git a/sql.go b/sql.go new file mode 100644 index 0000000..d781294 --- /dev/null +++ b/sql.go @@ -0,0 +1,25 @@ +package nill + +import ( + "database/sql" + "database/sql/driver" +) + +// Scan implements the [sql.Scanner] interface. +func (t *Type[T]) Scan(value interface{}) error { + var x sql.Null[T] + err := x.Scan(value) + if err != nil { + return err + } + + *t = Type[T](x) + + return nil +} + +// Value implements the [driver.Valuer] interface. +func (t Type[T]) Value() (driver.Value, error) { + sql := sql.Null[T](t) + return sql.Value() +} diff --git a/sql_test.go b/sql_test.go new file mode 100644 index 0000000..dcbfb54 --- /dev/null +++ b/sql_test.go @@ -0,0 +1,43 @@ +package nill + +import "testing" + +func TestScan(t *testing.T) { + var foo Type[int] + err := foo.Scan(nil) + if err != nil { + t.Error(err) + } + + if foo.Valid { + t.Error("should be invalid") + } + + err = foo.Scan(1) + if err != nil { + t.Error(err) + } + + wantFoo := Type[int]{Valid: true, V: 1} + + if foo != wantFoo { + t.Errorf("foo %v want %v", foo, wantFoo) + } +} + +func TestValue(t *testing.T) { + foo := Type[int]{Valid: true, V: 1} + v, err := foo.Value() + if err != nil { + t.Error("error should be nil") + } + + vInt, ok := v.(int) + if !ok { + t.Error("should be 1") + } + + if vInt != 1 { + t.Error("should be 1") + } +} diff --git a/string.go b/string.go index 124ab49..e2fc8ce 100644 --- a/string.go +++ b/string.go @@ -3,5 +3,5 @@ package nill type String Type[string] func NewString(value string) String { - return String{Valid: true, Value: value} + return String{Valid: true, V: value} }