xmidt-org / wrp-go

go implementation of the Web Routing Protocol
Apache License 2.0
4 stars 7 forks source link

Add a wrpcontext subpackage #114

Closed johnabass closed 1 year ago

johnabass commented 1 year ago

We need a standard way to add wrp messages to a context.Context. This standard should support not only wrp.Message but also all the other message types, such as wrp.SimpleRequestResponse.

The basic API, from a client's point of view, can look like the de facto standard way of doing this for golang:


// putting a message into a context
var msg *wrp.Message
ctx := wrpcontext.Set(ctx, msg)

// getting a wrp.Message from a context
var msg *wrp.Message = wrpcontext.Get(ctx) // nil means no message

// getting a message from a context using semantics from the errors package,
// which supports 
var msg *wrp.Message
if !wrpcontext.GetAs(ctx, &msg) {
    // no message, or the message was not of type *wrp.Message
}
johnabass commented 1 year ago

We can also leverage generics, too. This might be a clearer API for supporting multiple types:

// the generic type is just in the Get implementation like errors.As
msg := wrpcontext.Get[wrp.Message](ctx)