Skip to content

Commit

Permalink
fix: lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Mar 4, 2024
1 parent d80faf6 commit fbb743c
Show file tree
Hide file tree
Showing 21 changed files with 145 additions and 162 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ linters:
- predeclared # find code that shadows one of Go's predeclared identifiers [fast: true, auto-fix: false]
- promlinter # Check Prometheus metrics naming via promlint [fast: true, auto-fix: false]
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. [fast: false, auto-fix: false]
- tagliatelle # Checks the struct tags. [fast: true, auto-fix: false]
#- tagliatelle # Checks the struct tags. [fast: true, auto-fix: false]
- testpackage # linter that makes you use a separate _test package [fast: true, auto-fix: false]
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers [fast: false, auto-fix: false]
- unconvert # Remove unnecessary type conversions [fast: false, auto-fix: false]
Expand Down
4 changes: 2 additions & 2 deletions integration/watermill/gtm/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gtm

const (
HeaderUUID = "Message-Uuid"
ProviderName = "gtm"
HeaderUUID = "Message-Uuid"
ProviderName = "gtm"
)
71 changes: 30 additions & 41 deletions integration/watermill/gtm/publisher.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package gtm

import (
"bytes"
"encoding/json"
"io"
"net/http"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
mpv2 "github.com/foomo/sesamy/measurementprotocol/v2"
"github.com/pkg/errors"
Expand All @@ -19,7 +16,6 @@ var (

type (
Publisher struct {
//l watermill.LoggerAdapter
url string
client *http.Client
marshalMessageFunc MarshalMessageFunc
Expand Down Expand Up @@ -77,7 +73,6 @@ func (p *Publisher) Publish(topic string, messages ...*message.Message) error {
}

for _, msg := range messages {

var event *mpv2.Event
if err := json.Unmarshal(msg.Payload, &event); err != nil {
return err
Expand All @@ -88,40 +83,48 @@ func (p *Publisher) Publish(topic string, messages ...*message.Message) error {
return err
}

u := p.url + "?" + values.Encode() + "&richsstsse"
var richsstsse bool
if values.Has("richsstsse") {
values.Del("richsstsse")
richsstsse = true
}

req, err := http.NewRequestWithContext(msg.Context(), "POST", u, body)
u := p.url + "?"

for s, s2 := range msg.Metadata {
req.Header.Set(s, s2)
if richsstsse {
u += "&richsstsse"
}

//req, err := p.marshalMessageFunc(topic, msg)
req, err := http.NewRequestWithContext(msg.Context(), http.MethodPost, u, body)
if err != nil {
return errors.Wrapf(err, "cannot marshal message %s", msg.UUID)
return errors.Wrap(err, "failed to create request")
}

logFields := watermill.LogFields{
"uuid": msg.UUID,
"provider": ProviderName,
for s, s2 := range msg.Metadata {
req.Header.Set(s, s2)
}

//p.l.Trace("Publishing message", logFields)
// logFields := watermill.LogFields{
// "uuid": msg.UUID,
// "provider": ProviderName,
// }

// p.l.Trace("Publishing message", logFields)

resp, err := p.client.Do(req)
if err != nil {
return errors.Wrapf(err, "publishing message %s failed", msg.UUID)
}

if err = p.handleResponseBody(resp, logFields); err != nil {
if err = p.handleResponseBody(resp); err != nil {
return err
}

if resp.StatusCode >= http.StatusBadRequest {
return errors.Wrap(ErrErrorResponse, resp.Status)
}

//p.l.Trace("Message published", logFields)
// p.l.Trace("Message published", logFields)
}

return nil
Expand All @@ -140,40 +143,26 @@ func (p *Publisher) Close() error {
// ~ Private methods
// ------------------------------------------------------------------------------------------------

func (p *Publisher) handleResponseBody(resp *http.Response, logFields watermill.LogFields) error {
func (p *Publisher) handleResponseBody(resp *http.Response) error {
defer resp.Body.Close()

if resp.StatusCode < http.StatusBadRequest {
return nil
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "could not read response body")
}
// body, err := io.ReadAll(resp.Body)
// if err != nil {
// return errors.Wrap(err, "could not read response body")
// }

logFields = logFields.Add(watermill.LogFields{
"http_status": resp.StatusCode,
"http_response": string(body),
})
//p.l.Info("Server responded with error", logFields)
// logFields = logFields.Add(watermill.LogFields{
// "http_status": resp.StatusCode,
// "http_response": string(body),
// })
// p.l.Info("Server responded with error", logFields)

return nil
}

// MarshalMessageFunc transforms the message into a HTTP request to be sent to the specified url.
type MarshalMessageFunc func(url string, msg *message.Message) (*http.Request, error)

// DefaultMarshalMessageFunc transforms the message into a HTTP POST request.
// It encodes the UUID and Metadata in request headers.
func DefaultMarshalMessageFunc(url string, msg *message.Message) (*http.Request, error) {

req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(msg.Payload))
if err != nil {
return nil, err
}

req.Header.Set(HeaderUUID, msg.UUID)

return req, nil
}
12 changes: 6 additions & 6 deletions integration/watermill/keel/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ var (

type (
Subscriber struct {
l *zap.Logger
uuidFunc func() string
messages chan *message.Message
middlewares []SubscriberMiddleware
closed bool
l *zap.Logger
uuidFunc func() string
messages chan *message.Message
middlewares []SubscriberMiddleware
closed bool
}
SubscriberOption func(*Subscriber)
SubscriberHandler func(l *zap.Logger, r *http.Request, event *mpv2.Event) error
Expand Down Expand Up @@ -60,7 +60,7 @@ func SubscriberWithLogger(fields ...zap.Field) SubscriberOption {
o.middlewares = append(o.middlewares, func(next SubscriberHandler) SubscriberHandler {
return func(l *zap.Logger, r *http.Request, event *mpv2.Event) error {
fields = append(fields, zap.String("event_name", mp.GetDefault(event.EventName, "-").String()))
if labeler, ok := log.LabelerFromRequest(r);ok {
if labeler, ok := log.LabelerFromRequest(r); ok {
labeler.Add(fields...)
}
return next(l.With(fields...), r, event)
Expand Down
2 changes: 1 addition & 1 deletion measurementprotocol/getset.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Get[T any](v *T) T {
}

func GetDefault[T any](v *T, fallback T) T {
if v == nil {
if v == nil {
return fallback
}
return *v
Expand Down
2 changes: 0 additions & 2 deletions measurementprotocol/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (c *Client) HTTPClient() *http.Client {
// ------------------------------------------------------------------------------------------------

func (c *Client) Send(ctx context.Context, event *Event) error {

values, body, err := Marshal(event)
if err != nil {
return errors.Wrap(err, "failed to marshall event")
Expand Down Expand Up @@ -90,4 +89,3 @@ func (c *Client) Send(ctx context.Context, event *Event) error {

return nil
}

14 changes: 6 additions & 8 deletions measurementprotocol/v2/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ type Event struct {
// Defines a parameter for the current Event
// Example: epn.plays_count: 42
EventParameterNumber map[string]string `json:"epn,omitempty" mapstructure:"epn,omitempty"`
// If the current event is set as a conversion on the admin interace the evfent will have this value present
// If the current event is set as a conversion on the admin interacted the evfent will have this value present
// Example: 1
IsConversion *string `json:"_c,omitempty" mapstructure:"_c,omitempty"`
// External Event
Expand Down Expand Up @@ -181,7 +181,7 @@ type Event struct {
// If the "_ga_THYNGSTER" cookie last session time value is older than 1800 seconds, the current event will have this value present. This will internally create a new "session_start" event on GA4. If this event is also a conversion the value will be "2" if not, will be "1"
// Example: 1|2
SessionStart *string `json:"_ss,omitempty" mapstructure:"_ss,omitempty"`
// This seems to be related to the ServerSide hits, it's 0 if the FPLC Cookie is not present and to the current value if it's comming from a Cross Domain linker
// This seems to be related to the ServerSide hits, it's 0 if the FPLC Cookie is not present and to the current value if it's coming from a Cross Domain linker
// Example: bVhVicbfiSXaGNxeawKaPlDQc9QXPD6bKcsn36Elden6wZNb7Q5X1iXlkTVP5iP3H3y76cgM3UIgHCaRsYfPoyLGlbiIYMPRjvnUU7KWbdWLagodzxjrlPnvaRZJkw
FirstPartyLinkerCookie *string `json:"_fplc,omitempty" mapstructure:"_fplc,omitempty"`
// If the current user has a GA4 session cookie, but not a GA (_ga) client id cookie, this parameter will be added to the hit
Expand All @@ -202,7 +202,7 @@ type Event struct {
// Example: JPY
Currency *string `json:"cu,omitempty" mapstructure:"cu,omitempty"`
// Example:
Items []Item `json:"pr,omitempty" mapstructure:"pr,omitempty"`
Items []*Item `json:"pr,omitempty" mapstructure:"pr,omitempty"`
// Promotion Impression/Click Tracking. Promotion Id
// Example: summer-offer
PromotionID *string `json:"pi,omitempty" mapstructure:"pi,omitempty"`
Expand All @@ -211,10 +211,10 @@ type Event struct {
PromotionName *string `json:"pn,omitempty" mapstructure:"pn,omitempty"`
// Promotion Impression/Click Tracking. Creative Name
// Example: red-car
CreativeName *string `json:"cn,omitempty" mapstructure:"cn,omitempty"`
// CreativeName *string `json:"cn,omitempty" mapstructure:"cn,omitempty"`
// Promotion Impression/Click Tracking. Promotion Slot / Position
// Example: slide-3
CreativeSlot *string `json:"cs,omitempty" mapstructure:"cs,omitempty"`
// CreativeSlot *string `json:"cs,omitempty" mapstructure:"cs,omitempty"`
// Google Place ID: Refer to: https://developers.google.com/maps/documentation/places/web-service/place-id . Seems to be inherited from Firebase, not sure about the current use on GA4
// Example: ChIJiyj437sx3YAR9kUWC8QkLzQ
LocationID *string `json:"lo,omitempty" mapstructure:"lo,omitempty"`
Expand Down Expand Up @@ -254,12 +254,10 @@ type Event struct {
PrivacySandboxCookieDeprecationLabel *string `json:"pscdl,omitempty" mapstructure:"pscdl,omitempty"`
// A timestamp measuring the difference between the moment this parameter gets populated and the moment the navigation started on that particular page.
TFD *string `json:"tfd,omitempty" mapstructure:"tfd,omitempty"`
SST *SST `json:"sst,omitempty" mapstructure:"sst,omitempty"`
SST *SST `json:"sst,omitempty" mapstructure:"sst,omitempty"`
PAE *string `json:"pae,omitempty" mapstructure:"pae,omitempty"`

// --- Unresolved ---

Unknown map[string]any `json:"-" mapstructure:",remain"`
}


6 changes: 3 additions & 3 deletions measurementprotocol/v2/event/addpaymentinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ type AddPaymentInfo struct {
}

func (e *AddPaymentInfo) MPv2() *mpv2.Event {
items := make([]mpv2.Item, len(e.Items))
items := make([]*mpv2.Item, len(e.Items))
for i, item := range e.Items {
items[i] = *item.MPv2()
items[i] = item.MPv2()
}
return &mpv2.Event{
Currency: mp.SetString(e.Currency),
EventParameter: map[string]string{
mpv2.EventParameterCoupon.String(): e.Coupon,
mpv2.EventParameterCoupon.String(): e.Coupon,
mpv2.EventParameterPaymentType.String(): e.PaymentType,
},
EventParameterNumber: map[string]string{
Expand Down
14 changes: 7 additions & 7 deletions measurementprotocol/v2/event/addshippinginfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ AddShippingInfo https://developers.google.com/tag-platform/gtagjs/reference/even
Query: v=2&tid=G-PZ5ELRCR31&gtm=45je42t1v9177778896z89175355532za220&_p=1709357665402&_dbg=1&gcd=13l3l3l3l1&npa=0&dma_cps=sypham&dma=1&cid=1220643501.1708014725&ul=en-us&sr=3840x1600&_fplc=0&ur=DE-BY&uaa=arm&uab=64&uafvl=Chromium%3B122.0.6261.69%7CNot(A%253ABrand%3B24.0.0.0%7CGoogle%2520Chrome%3B122.0.6261.69&uamb=0&uam=&uap=macOS&uapv=14.3.1&uaw=0&are=1&pscdl=noapi&_eu=IA&sst.uc=DE&sst.etld=google.de&sst.gcsub=region1&sst.gcd=13l3l3l3l1&sst.tft=1709357665402&_s=10&cu=USD&sid=1709529821&sct=9&seg=0&dl=https%3A%2F%2Fsniffer.local.bestbytes.net%2F%3Fgtm_debug%3D1709357665301&dr=https%3A%2F%2Ftagassistant.google.com%2F&dt=Server%20Side%20Tracking%20Prototype%20(codename%3A%20sniffer)&en=add_payment_info&_ss=1&pr1=idSKU_12345~nmStan%20and%20Friends%20Tee~afGoogle%20Store~cpSUMMER_FUN~ds2.22~lp5~brGoogle~caApparel~c2Adult~c3Shirts~c4Crew~c5Short%20sleeve~lirelated_products~lnRelated%20products~vagreen~loChIJIQBpAG2ahYAR_6128GcTUEo~pr10.01~qt3&epn.value=30.03&ep.coupon=SUMMER_FUN&ep.shipping_tier=Ground&tfd=137238301&richsstsse
*/
type AddShippingInfo struct {
Currency string
Value float64
Coupon string
Currency string
Value float64
Coupon string
ShippingTier string
Items []*Item
Items []*Item
}

func (e *AddShippingInfo) MPv2() *mpv2.Event {
items := make([]mpv2.Item, len(e.Items))
items := make([]*mpv2.Item, len(e.Items))
for i, item := range e.Items {
items[i] = *item.MPv2()
items[i] = item.MPv2()
}
return &mpv2.Event{
Currency: mp.SetString(e.Currency),
EventParameter: map[string]string{
mpv2.EventParameterCoupon.String(): e.Coupon,
mpv2.EventParameterCoupon.String(): e.Coupon,
mpv2.EventParameterShippingTier.String(): e.ShippingTier,
},
EventParameterNumber: map[string]string{
Expand Down
4 changes: 2 additions & 2 deletions measurementprotocol/v2/event/addtocart.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ type AddToCart struct {
}

func (e *AddToCart) MPv2() *mpv2.Event {
items := make([]mpv2.Item, len(e.Items))
items := make([]*mpv2.Item, len(e.Items))
for i, item := range e.Items {
items[i] = *item.MPv2()
items[i] = item.MPv2()
}
return &mpv2.Event{
Currency: mp.SetString(e.Currency),
Expand Down
4 changes: 2 additions & 2 deletions measurementprotocol/v2/event/addtowishlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ type AddToWishlist struct {
}

func (e *AddToWishlist) MPv2() *mpv2.Event {
items := make([]mpv2.Item, len(e.Items))
items := make([]*mpv2.Item, len(e.Items))
for i, item := range e.Items {
items[i] = *item.MPv2()
items[i] = item.MPv2()
}
return &mpv2.Event{
Currency: mp.SetString(e.Currency),
Expand Down
12 changes: 6 additions & 6 deletions measurementprotocol/v2/event/begincheckout.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ BeginCheckout https://developers.google.com/tag-platform/gtagjs/reference/events
Query: v=2&tid=G-123456&gtm=45je42t1v9177778896z89175355532za200&_p=1709325262551&gcd=13l3l3l3l1&npa=0&dma_cps=sypham&dma=1&cid=1220643501.1708014725&ul=en-us&sr=3840x1600&_fplc=0&ur=DE-BY&uaa=arm&uab=64&uafvl=Chromium%3B122.0.6261.69%7CNot(A%253ABrand%3B24.0.0.0%7CGoogle%2520Chrome%3B122.0.6261.69&uamb=0&uam=&uap=macOS&uapv=14.3.1&uaw=0&are=1&pscdl=noapi&_eu=IA&sst.uc=DE&sst.etld=google.de&sst.gcsub=region1&sst.gcd=13l3l3l3l1&sst.tft=1709325262551&_s=5&cu=USD&sid=1709445696&sct=8&seg=0&dl=https%3A%2F%2Fsniffer.local.bestbytes.net%2F&dt=Server%20Side%20Tracking%20Prototype%20(codename%3A%20sniffer)&en=begin_checkout&_ss=1&pr1=idSKU_12345~nmStan%20and%20Friends%20Tee~afGoogle%20Store~cpSUMMER_FUN~ds2.22~lp5~brGoogle~caApparel~c2Adult~c3Shirts~c4Crew~c5Short%20sleeve~lirelated_products~lnRelated%20products~vagreen~loChIJIQBpAG2ahYAR_6128GcTUEo~pr10.01~qt3&epn.value=30.03&ep.coupon=SUMMER_FUN&tfd=120434230&richsstsse
*/
type BeginCheckout struct {
Currency string
Value float64
Coupon string
Items []*Item
Currency string
Value float64
Coupon string
Items []*Item
}

func (e *BeginCheckout) MPv2() *mpv2.Event {
items := make([]mpv2.Item, len(e.Items))
items := make([]*mpv2.Item, len(e.Items))
for i, item := range e.Items {
items[i] = *item.MPv2()
items[i] = item.MPv2()
}
return &mpv2.Event{
Currency: mp.SetString(e.Currency),
Expand Down
Loading

0 comments on commit fbb743c

Please sign in to comment.