Skip to content

Commit

Permalink
use generics
Browse files Browse the repository at this point in the history
  • Loading branch information
MetalBlueberry committed May 21, 2024
1 parent c7622db commit 7e93539
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
57 changes: 29 additions & 28 deletions types/arrayok.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
package types

import "encoding/json"
import (
"encoding/json"
"reflect"
)

type ArrayOKfloat64 struct {
Value float64
Array []float64
type ArrayOK[T any] struct {
Value T
Array []T
}

func (arrayOK *ArrayOKfloat64) MarshalJSON() ([]byte, error) {
if arrayOK.Array != nil {
return json.Marshal(arrayOK.Array)
}
return json.Marshal(arrayOK.Value)
func (arrayOK *ArrayOK[T]) MarshalJSON() ([]byte, error) {
if arrayOK.Array!= nil {
return json.Marshal(arrayOK.Array)
}
return json.Marshal(arrayOK.Value)
}

func (arrayOK *ArrayOKfloat64) UnmarshalJSON(data []byte) error {
arrayOK.Array = nil
arrayOK.Value = 0
func (arrayOK *ArrayOK[T]) UnmarshalJSON(data []byte) error {
arrayOK.Array = nil
arrayOK.Value = reflect.Zero(reflect.TypeOf(arrayOK.Value)).Interface().(T)

var array []float64
err := json.Unmarshal(data, &array)
if err == nil {
arrayOK.Array = array
return nil
}
var array []T
err := json.Unmarshal(data, &array)
if err == nil {
arrayOK.Array = array
return nil
}

var value float64
err = json.Unmarshal(data, &value)
if err != nil {
return err
}
arrayOK.Value = value
return nil
return nil

}
var value T
err = json.Unmarshal(data, &value)
if err!= nil {
return err
}
arrayOK.Value = value
return nil
}
18 changes: 9 additions & 9 deletions types/arrayok_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type TestMarshallScenario struct {
Name string
Input types.ArrayOKfloat64
Input types.ArrayOK[float64]
Expected string
}

Expand All @@ -20,21 +20,21 @@ func TestFloat64Marshal(t *testing.T) {
table := []TestMarshallScenario{
{
Name: "A single number",
Input: types.ArrayOKfloat64{
Input: types.ArrayOK[float64]{
Value: 12.3,
},
Expected: "12.3",
},
{
Name: "A single number in a list",
Input: types.ArrayOKfloat64{
Input: types.ArrayOK[float64]{
Array: []float64{12.3},
},
Expected: "[12.3]",
},
{
Name: "Multiple values",
Input: types.ArrayOKfloat64{
Input: types.ArrayOK[float64]{
Array: []float64{1, 2, 3, 4.5},
},
Expected: "[1,2,3,4.5]",
Expand All @@ -55,7 +55,7 @@ func TestFloat64Marshal(t *testing.T) {
type TestUnmarshallScenario struct {
Name string
Input string
Expected types.ArrayOKfloat64
Expected types.ArrayOK[float64]
}

func TestFloat64Unmarshal(t *testing.T) {
Expand All @@ -65,28 +65,28 @@ func TestFloat64Unmarshal(t *testing.T) {
{
Name: "A single number",
Input: "12.3",
Expected: types.ArrayOKfloat64{
Expected: types.ArrayOK[float64]{
Value: 12.3,
},
},
{
Name: "A single number in a list",
Input: "[12.3]",
Expected: types.ArrayOKfloat64{
Expected: types.ArrayOK[float64]{
Array: []float64{12.3},
},
},
{
Name: "Multiple values",
Input: "[1,2,3,4.5]",
Expected: types.ArrayOKfloat64{
Expected: types.ArrayOK[float64]{
Array: []float64{1, 2, 3, 4.5},
},
},
}
for _, tt := range table {
t.Run(tt.Name, func(t *testing.T) {
result := types.ArrayOKfloat64{}
result := types.ArrayOK[float64]{}
err := json.Unmarshal([]byte(tt.Input), &result)
Expect(err).To(BeNil())
Expect(result).To(Equal(tt.Expected))
Expand Down

0 comments on commit 7e93539

Please sign in to comment.