Skip to content

Commit 4880de2

Browse files
committed
experiment using json marshal / unmarshal for hashid
1 parent 7fb7062 commit 4880de2

7 files changed

Lines changed: 48 additions & 82 deletions

File tree

api/dbv1/full_developer_apps.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

api/dbv1/get_developer_apps.sql.go

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/dbv1/models.go

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1_developer_apps.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (as *ApiServer) v1DeveloperApps(c *fiber.Ctx) error {
1818
address = "0x" + address
1919
}
2020

21-
developerApps, err := as.queries.FullDeveloperApps(c.Context(), dbv1.GetDeveloperAppsParams{
21+
developerApps, err := as.queries.GetDeveloperApps(c.Context(), dbv1.GetDeveloperAppsParams{
2222
Address: address,
2323
})
2424
if err != nil {
@@ -29,12 +29,6 @@ func (as *ApiServer) v1DeveloperApps(c *fiber.Ctx) error {
2929
return fiber.NewError(fiber.StatusNotFound, "Developer app not found")
3030
}
3131

32-
if !c.Locals("isFull").(bool) {
33-
return c.JSON(fiber.Map{
34-
"data": dbv1.ToMinDeveloperApps(developerApps)[0],
35-
})
36-
}
37-
3832
return c.JSON(fiber.Map{
3933
"data": developerApps[0],
4034
})

api/v1_developer_apps_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ import (
55
"testing"
66

77
"bridgerton.audius.co/api/dbv1"
8-
"github.com/jackc/pgx/v5/pgtype"
98
"github.com/stretchr/testify/assert"
109
)
1110

1211
func TestGetDeveloperAppsQueries(t *testing.T) {
1312
developerApps, err := app.queries.GetDeveloperApps(t.Context(), dbv1.GetDeveloperAppsParams{
14-
UserID: pgtype.Int4{
15-
Int32: 1,
16-
Valid: true,
17-
},
13+
UserID: 1,
1814
})
1915
assert.NoError(t, err)
2016
assert.Len(t, developerApps, 1)
@@ -23,7 +19,7 @@ func TestGetDeveloperAppsQueries(t *testing.T) {
2319

2420
func TestGetDeveloperApp(t *testing.T) {
2521
var resp struct {
26-
Data dbv1.FullDeveloperApp
22+
Data dbv1.GetDeveloperAppsRow
2723
}
2824
status, body := testGet(t, "/v1/developer_apps/0x7d7b6b7a97d1deefe3a1ccc5a13c48e8f055e0b6", &resp)
2925
assert.Equal(t, 200, status)

sqlc.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ sql:
3737
import: "encoding/json"
3838
type: "RawMessage"
3939

40+
- column: "developer_apps.user_id"
41+
go_type:
42+
import: "bridgerton.audius.co/trashid"
43+
type: "TrashId"
44+
4045
- column: "tracks.copyright_line"
4146
go_type:
4247
import: "encoding/json"

trashid/hashid.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package trashid
33
import (
44
"errors"
55
"strconv"
6+
"strings"
67

78
"github.com/speps/go-hashids/v2"
89
)
@@ -50,3 +51,20 @@ func MustEncodeHashID(id int) string {
5051
}
5152
return enc
5253
}
54+
55+
type TrashId int
56+
57+
func (num TrashId) MarshalJSON() ([]byte, error) {
58+
hid, err := EncodeHashId(int(num))
59+
return []byte(`"` + hid + `"`), err
60+
}
61+
62+
func (num *TrashId) UnmarshalJSON(data []byte) error {
63+
idStr := strings.Trim(string(data), `"`)
64+
id, err := DecodeHashId(idStr)
65+
if err != nil {
66+
return err
67+
}
68+
*num = TrashId(id)
69+
return nil
70+
}

0 commit comments

Comments
 (0)