-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
514 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Publisher is an implementation of a Publisher using Log | ||
type Publisher struct{} | ||
|
||
// NewPublisher returns a new Publisher | ||
func NewPublisher() *Publisher { | ||
return &Publisher{} | ||
} | ||
|
||
func (p *Publisher) PublishEvent(ctx context.Context, subject string, event any) error { | ||
if event == nil { | ||
return nil | ||
} | ||
|
||
fields := make(logrus.Fields) | ||
b, err := json.Marshal(event) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(b, &fields) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
WithContext(ctx).WithFields(logrus.Fields{ | ||
"subject": subject, | ||
"data": fields, | ||
}).Info("published") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package log_test | ||
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/require" | ||
"go.ketch.com/lib/orlop/v2/log" | ||
"testing" | ||
) | ||
|
||
type SomeEvent struct { | ||
ID string | ||
Name string | ||
} | ||
|
||
func TestLog(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
pub := log.NewPublisher() | ||
|
||
err := pub.PublishEvent(ctx, "SomeEvent", &SomeEvent{ | ||
ID: "123", | ||
Name: "one two three", | ||
}) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package request | ||
|
||
import ( | ||
"context" | ||
"go.ketch.com/lib/orlop/v2/errors" | ||
) | ||
|
||
// WithConnection returns a new context with the given request Connection | ||
func WithConnection(parent context.Context, connectionID string) context.Context { | ||
if len(connectionID) == 0 { | ||
return parent | ||
} | ||
|
||
return WithValue(parent, ConnectionKey, connectionID) | ||
} | ||
|
||
// WithConnectionOption returns a function that sets the given Connection on a context | ||
func WithConnectionOption(connection string) func(ctx context.Context) context.Context { | ||
return func(ctx context.Context) context.Context { | ||
if len(Connection(ctx)) == 0 { | ||
ctx = WithConnection(ctx, connection) | ||
} | ||
return ctx | ||
} | ||
} | ||
|
||
// Connection returns the request ID or an empty string | ||
func Connection(ctx Context) string { | ||
return Value[string](ctx, ConnectionKey) | ||
} | ||
|
||
// RequireConnection returns the request ID or an error if not set | ||
func RequireConnection(ctx Context) (string, error) { | ||
return RequireValue[string](ctx, ConnectionKey, errors.Invalid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.