refactor display + marshal/unmarshal

This commit is contained in:
Hayden 2022-11-13 13:38:47 -09:00
parent e7eee7a129
commit 9192f2e5f9
No known key found for this signature in database
GPG key ID: 17CF79474E257545
4 changed files with 148 additions and 55 deletions

View file

@ -0,0 +1,30 @@
package repo
import (
"bytes"
"fmt"
"strconv"
)
type AssetID int
func (aid AssetID) MarshalJSON() ([]byte, error) {
aidStr := fmt.Sprintf("%06d", aid)
aidStr = fmt.Sprintf("%s-%s", aidStr[:3], aidStr[3:])
return []byte(fmt.Sprintf(`"%s"`, aidStr)), nil
}
func (aid *AssetID) UnmarshalJSON(d []byte) error {
d = bytes.Replace(d, []byte(`"`), []byte(``), -1)
d = bytes.Replace(d, []byte(`-`), []byte(``), -1)
aidInt, err := strconv.Atoi(string(d))
if err != nil {
return err
}
*aid = AssetID(aidInt)
return nil
}

View file

@ -0,0 +1,115 @@
package repo
import (
"encoding/json"
"reflect"
"testing"
)
func TestAssetID_MarshalJSON(t *testing.T) {
tests := []struct {
name string
aid AssetID
want []byte
wantErr bool
}{
{
name: "basic test",
aid: 123,
want: []byte(`"000-123"`),
},
{
name: "zero test",
aid: 0,
want: []byte(`"000-000"`),
},
{
name: "large int",
aid: 123456789,
want: []byte(`"123-456789"`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.aid.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("AssetID.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AssetID.MarshalJSON() = %v, want %v", got, tt.want)
}
})
}
}
func TestAssetID_UnmarshalJSON(t *testing.T) {
type args struct {
data []byte
}
tests := []struct {
name string
aid *AssetID
args args
want AssetID
wantErr bool
}{
{
name: "basic test",
aid: new(AssetID),
want: 123,
args: args{
data: []byte(`{"AssetID":"000123"}`),
},
},
{
name: "dashed format",
aid: new(AssetID),
want: 123,
args: args{
data: []byte(`{"AssetID":"000-123"}`),
},
},
{
name: "no leading zeros",
aid: new(AssetID),
want: 123,
args: args{
data: []byte(`{"AssetID":"123"}`),
},
},
{
name: "trailing zeros",
aid: new(AssetID),
want: 123000,
args: args{
data: []byte(`{"AssetID":"000123000"}`),
},
},
{
name: "large int",
aid: new(AssetID),
want: 123456789,
args: args{
data: []byte(`{"AssetID":"123456789"}`),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
st := struct {
AssetID AssetID `json:"AssetID"`
}{}
err := json.Unmarshal(tt.args.data, &st)
if (err != nil) != tt.wantErr {
t.Errorf("AssetID.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if st.AssetID != tt.want {
t.Errorf("AssetID.UnmarshalJSON() = %v, want %v", st.AssetID, tt.want)
}
})
}
}

View file

@ -2,9 +2,6 @@ package repo
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/google/uuid"
@ -21,30 +18,6 @@ type ItemsRepository struct {
db *ent.Client
}
type AssetID int
func (aid AssetID) MarshalJSON() ([]byte, error) {
str := fmt.Sprintf("%d", aid)
for len(str) < 6 {
str = "0" + str
}
return []byte(fmt.Sprintf(`"%s"`, str)), nil
}
func (aid *AssetID) UnmarshalJSON(data []byte) error {
str := string(strings.Replace(string(data), `"`, "", -1))
aidInt, err := strconv.Atoi(str)
if err != nil {
return err
}
*aid = AssetID(aidInt)
return nil
}
type (
ItemQuery struct {
Page int
@ -410,6 +383,9 @@ func (e *ItemsRepository) GetHighestAssetID(ctx context.Context, GID uuid.UUID)
result, err := q.First(ctx)
if err != nil {
if ent.IsNotFound(err) {
return 0, nil
}
return 0, err
}

View file

@ -2,7 +2,6 @@ package repo
import (
"context"
"encoding/json"
"testing"
"time"
@ -10,33 +9,6 @@ import (
"github.com/stretchr/testify/assert"
)
func TestAssetID_UnmarshalJSON(t *testing.T) {
rawjson := `{"aid":"000123"}`
st := struct {
AID AssetID `json:"aid"`
}{
AID: AssetID(0),
}
err := json.Unmarshal([]byte(rawjson), &st)
assert.NoError(t, err)
assert.Equal(t, AssetID(123), st.AID)
}
func TestAssetID_MarshalJSON(t *testing.T) {
st := struct {
AID AssetID `json:"aid"`
}{
AID: AssetID(123),
}
b, err := json.Marshal(st)
assert.NoError(t, err)
assert.JSONEq(t, `{"aid":"000123"}`, string(b))
}
func itemFactory() ItemCreate {
return ItemCreate{
Name: fk.Str(10),