-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* start of adding more error codes * adding more common error codes * adding call options for client requests * adding call options for rpc's * adding doc comment for HeaderCallOption * fixing linting errors
- Loading branch information
Showing
10 changed files
with
137 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package stormrpc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
type ctxKey int | ||
|
||
const ( | ||
headerContextKey ctxKey = iota | ||
) | ||
|
||
// HeadersFromContext retrieves RPC headers from the given context. | ||
func HeadersFromContext(ctx context.Context) nats.Header { | ||
h, ok := ctx.Value(headerContextKey).(nats.Header) | ||
if !ok { | ||
return make(nats.Header) | ||
} | ||
|
||
return h | ||
} | ||
|
||
// newContextWithHeaders creates a new context with Header information stored in it. | ||
func newContextWithHeaders(ctx context.Context, headers nats.Header) context.Context { | ||
return context.WithValue(ctx, headerContextKey, headers) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,34 @@ | ||
package stormrpc | ||
|
||
// CallOption configures an RPC to perform actions before it starts or after | ||
// the RPC has completed. | ||
type CallOption interface { | ||
// before is called before the RPC is sent to any server. | ||
// If before returns a non-nil error, the RPC fails with that error. | ||
before(*callOptions) error | ||
|
||
// after is called after the RPC has completed after cannot return an error. | ||
after(*callOptions) | ||
} | ||
|
||
// callOptions contains all configuration for an RPC. | ||
type callOptions struct { | ||
headers map[string]string | ||
} | ||
|
||
// HeaderCallOption is used to configure which headers to append to the outgoing RPC. | ||
type HeaderCallOption struct { | ||
Headers map[string]string | ||
} | ||
|
||
func (o *HeaderCallOption) before(c *callOptions) error { | ||
c.headers = o.Headers | ||
return nil | ||
} | ||
|
||
func (o *HeaderCallOption) after(c *callOptions) {} | ||
|
||
// WithHeaders returns a CallOption that appends the given headers to the request. | ||
func WithHeaders(h map[string]string) CallOption { | ||
return &HeaderCallOption{Headers: h} | ||
} |
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,41 @@ | ||
package stormrpc | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestWithHeaders(t *testing.T) { | ||
type args struct { | ||
h map[string]string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want CallOption | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "add some headers", | ||
args: args{ | ||
h: map[string]string{ | ||
"Authorization": "Bearer ey.xyz", | ||
"X-Request-Id": "abc", | ||
}, | ||
}, | ||
want: &HeaderCallOption{ | ||
Headers: map[string]string{ | ||
"Authorization": "Bearer ey.xyz", | ||
"X-Request-Id": "abc", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := WithHeaders(tt.args.h); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("WithHeaders() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
go install ./protoc-gen-stormrpc | ||
protoc --proto_path prototest -I=. prototest/test.proto \ | ||
--stormrpc_out=./prototest/gen_out --go_out=./prototest | ||
--stormrpc_out=./prototest/gen_out --go_out=./prototest |
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