Skip to content

Commit

Permalink
chore: add unmarshal method to api metadata struct
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Sep 26, 2024
1 parent 78d7a14 commit e95ee51
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
48 changes: 48 additions & 0 deletions api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"encoding/json"
"io"
"time"

Expand Down Expand Up @@ -230,6 +231,24 @@ type Metadata struct {
PayerData *PayerData `json:"payerData,omitempty"`
}

func (m *Metadata) UnmarshalJSON(data []byte) error {
var t struct {
Comment string `json:"comment"`
Nostr *NostrEvent `json:"nostr"`
PayerData *PayerData `json:"payer_data"`
}

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

m.Comment = t.Comment
m.Nostr = t.Nostr
m.PayerData = t.PayerData

return nil
}

type NostrEvent struct {
Content string `json:"content"`
CreatedAt int64 `json:"createdAt"`
Expand All @@ -240,12 +259,41 @@ type NostrEvent struct {
Tags [][]string `json:"tags"`
}

func (ne *NostrEvent) UnmarshalJSON(data []byte) error {
var t struct {
Content string `json:"content"`
CreatedAt int64 `json:"created_at"`
ID string `json:"id"`
Kind int `json:"kind"`
PubKey string `json:"pubkey"`
Sig string `json:"sig"`
Tags [][]string `json:"tags"`
}

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

ne.Content = t.Content
ne.CreatedAt = t.CreatedAt
ne.ID = t.ID
ne.Kind = t.Kind
ne.PubKey = t.PubKey
ne.Sig = t.Sig
ne.Tags = t.Tags

return nil
}

// unmarshal method is only needed for structs with snake_case fields
type PayerData struct {
Email string `json:"email,omitempty"`
Name string `json:"name,omitempty"`
Pubkey string `json:"pubkey,omitempty"`
}

// we did not add the unmarshal method here as Boostagram
// struct is anyways being used in transaction service
type Boostagram struct {
AppName string `json:"appName"`
Name string `json:"name"`
Expand Down
53 changes: 2 additions & 51 deletions api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction {

var metadata *Metadata
if transaction.Metadata != nil {
var txMetadata transactions.Metadata
jsonErr := json.Unmarshal(transaction.Metadata, &txMetadata)
jsonErr := json.Unmarshal(transaction.Metadata, &metadata)
if jsonErr != nil {
logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{
"payment_hash": transaction.PaymentHash,
"metadata": transaction.Metadata,
}).Error("Failed to deserialize transaction metadata info")
}
metadata = toApiMetadata(&txMetadata)
}

var boostagram *Boostagram
Expand All @@ -96,7 +94,7 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction {
"boostagram": transaction.Boostagram,
}).Error("Failed to deserialize transaction boostagram info")
}
boostagram = toApiBoostagram(&txBoostagram)
boostagram = (*Boostagram)(&txBoostagram)
}

return &Transaction{
Expand All @@ -115,50 +113,3 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction {
Boostagram: boostagram,
}
}

func toApiBoostagram(boostagram *transactions.Boostagram) *Boostagram {
return &Boostagram{
AppName: boostagram.AppName,
Name: boostagram.Name,
Podcast: boostagram.Podcast,
URL: boostagram.URL,
Episode: boostagram.Episode,
FeedId: boostagram.FeedId,
ItemId: boostagram.ItemId,
Timestamp: boostagram.Timestamp,
Message: boostagram.Message,
SenderId: boostagram.SenderId,
SenderName: boostagram.SenderName,
Time: boostagram.Time,
Action: boostagram.Action,
ValueMsatTotal: boostagram.ValueMsatTotal,
}
}

func toApiMetadata(metadata *transactions.Metadata) *Metadata {
apiMetadata := &Metadata{
Comment: metadata.Comment,
}

if metadata.Nostr != nil {
apiMetadata.Nostr = &NostrEvent{
Content: metadata.Nostr.Content,
CreatedAt: metadata.Nostr.CreatedAt,
ID: metadata.Nostr.ID,
Kind: metadata.Nostr.Kind,
PubKey: metadata.Nostr.PubKey,
Sig: metadata.Nostr.Sig,
Tags: metadata.Nostr.Tags,
}
}

if metadata.PayerData != nil {
apiMetadata.PayerData = &PayerData{
Email: metadata.PayerData.Email,
Name: metadata.PayerData.Name,
Pubkey: metadata.PayerData.Pubkey,
}
}

return apiMetadata
}
8 changes: 4 additions & 4 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ type Boostagram struct {
}

type Metadata struct {
Comment string `json:"comment"`
Nostr *NostrEvent `json:"nostr"`
PayerData *PayerData `json:"payer_data"`
AdditionalFields map[string]interface{}
Comment string `json:"comment"`
Nostr *NostrEvent `json:"nostr"`
PayerData *PayerData `json:"payer_data"`
AdditionalFields map[string]interface{} `json:"-"`
}

type NostrEvent struct {
Expand Down

0 comments on commit e95ee51

Please sign in to comment.