Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use string or number type for boostagram field flexibility #713

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ func toApiBoostagram(boostagram *transactions.Boostagram) *Boostagram {
Name: boostagram.Name,
Podcast: boostagram.Podcast,
URL: boostagram.URL,
Episode: boostagram.Episode,
FeedId: boostagram.FeedId,
ItemId: boostagram.ItemId,
Episode: boostagram.Episode.String(),
FeedId: boostagram.FeedId.String(),
ItemId: boostagram.ItemId.String(),
Timestamp: boostagram.Timestamp,
Message: boostagram.Message,
SenderId: boostagram.SenderId,
SenderId: boostagram.SenderId.String(),
SenderName: boostagram.SenderName,
Time: boostagram.Time,
Action: boostagram.Action,
Expand Down
4 changes: 2 additions & 2 deletions transactions/keysend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ func TestSendKeysend_TLVs(t *testing.T) {
assert.Equal(t, "⚡ WebLN Demo", boostagram.AppName)
assert.Equal(t, "⚡ WebLN Demo", boostagram.Name)
assert.Equal(t, "Podcasting 2.0", boostagram.Podcast)
assert.Equal(t, "Episode 104: A New Dump", boostagram.Episode)
assert.Equal(t, "https://feeds.podcastindex.org/pc20.xml", boostagram.FeedId)
assert.Equal(t, "Episode 104: A New Dump", boostagram.Episode.String())
assert.Equal(t, "https://feeds.podcastindex.org/pc20.xml", boostagram.FeedId.String())
assert.Equal(t, int64(21), boostagram.Timestamp)
assert.Equal(t, "Go podcasting!", boostagram.Message)
assert.Equal(t, "Satoshi Nakamoto", boostagram.SenderName)
Expand Down
4 changes: 2 additions & 2 deletions transactions/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func TestNotifications_ReceivedKeysend(t *testing.T) {
assert.Equal(t, "⚡ WebLN Demo", boostagram.AppName)
assert.Equal(t, "⚡ WebLN Demo", boostagram.Name)
assert.Equal(t, "Podcasting 2.0", boostagram.Podcast)
assert.Equal(t, "Episode 104: A New Dump", boostagram.Episode)
assert.Equal(t, "https://feeds.podcastindex.org/pc20.xml", boostagram.FeedId)
assert.Equal(t, "Episode 104: A New Dump", boostagram.Episode.String())
assert.Equal(t, "https://feeds.podcastindex.org/pc20.xml", boostagram.FeedId.String())
assert.Equal(t, int64(21), boostagram.Timestamp)
assert.Equal(t, "Go podcasting!", boostagram.Message)
assert.Equal(t, "Satoshi Nakamoto", boostagram.SenderName)
Expand Down
52 changes: 38 additions & 14 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,44 @@ const (
type Transaction = db.Transaction

type Boostagram struct {
AppName string `json:"app_name"`
Name string `json:"name"`
Podcast string `json:"podcast"`
URL string `json:"url"`
Episode string `json:"episode,omitempty"`
FeedId string `json:"feedID,omitempty"`
ItemId string `json:"itemID,omitempty"`
Timestamp int64 `json:"ts,omitempty"`
Message string `json:"message,omitempty"`
SenderId string `json:"sender_id"`
SenderName string `json:"sender_name"`
Time string `json:"time"`
Action string `json:"action"`
ValueMsatTotal int64 `json:"value_msat_total"`
AppName string `json:"app_name"`
Name string `json:"name"`
Podcast string `json:"podcast"`
URL string `json:"url"`
Episode StringOrNumber `json:"episode,omitempty"`
FeedId StringOrNumber `json:"feedID,omitempty"`
ItemId StringOrNumber `json:"itemID,omitempty"`
Timestamp int64 `json:"ts,omitempty"`
Message string `json:"message,omitempty"`
SenderId StringOrNumber `json:"sender_id"`
SenderName string `json:"sender_name"`
Time string `json:"time"`
Action string `json:"action"`
ValueMsatTotal int64 `json:"value_msat_total"`
}

type StringOrNumber struct {
im-adithya marked this conversation as resolved.
Show resolved Hide resolved
StringData string
NumberData int64
}

func (sn *StringOrNumber) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &sn.StringData); err == nil {
return nil
}

if err := json.Unmarshal(data, &sn.NumberData); err == nil {
return nil
}

return fmt.Errorf("cannot unmarshal %s into StringOrNumber type", data)
}

func (sn StringOrNumber) String() string {
if sn.StringData != "" {
return sn.StringData
}
return fmt.Sprintf("%d", sn.NumberData)
}

type notFoundError struct {
Expand Down
Loading