From 9192f2e5f9e97e4816921f1b0a2322d73ee6657b Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sun, 13 Nov 2022 13:38:47 -0900 Subject: [PATCH] refactor display + marshal/unmarshal --- backend/internal/data/repo/asset_id_type.go | 30 +++++ .../internal/data/repo/asset_id_type_test.go | 115 ++++++++++++++++++ backend/internal/data/repo/repo_items.go | 30 +---- backend/internal/data/repo/repo_items_test.go | 28 ----- 4 files changed, 148 insertions(+), 55 deletions(-) create mode 100644 backend/internal/data/repo/asset_id_type.go create mode 100644 backend/internal/data/repo/asset_id_type_test.go diff --git a/backend/internal/data/repo/asset_id_type.go b/backend/internal/data/repo/asset_id_type.go new file mode 100644 index 0000000..6aecf9c --- /dev/null +++ b/backend/internal/data/repo/asset_id_type.go @@ -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 + +} diff --git a/backend/internal/data/repo/asset_id_type_test.go b/backend/internal/data/repo/asset_id_type_test.go new file mode 100644 index 0000000..6a692d9 --- /dev/null +++ b/backend/internal/data/repo/asset_id_type_test.go @@ -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) + } + }) + } +} diff --git a/backend/internal/data/repo/repo_items.go b/backend/internal/data/repo/repo_items.go index 6c12287..d01c1dc 100644 --- a/backend/internal/data/repo/repo_items.go +++ b/backend/internal/data/repo/repo_items.go @@ -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 } diff --git a/backend/internal/data/repo/repo_items_test.go b/backend/internal/data/repo/repo_items_test.go index bc9c91d..4b958b0 100644 --- a/backend/internal/data/repo/repo_items_test.go +++ b/backend/internal/data/repo/repo_items_test.go @@ -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),