From: Nick Rosbrook <rosbrookn@gmail.com>
To: George Dunlap <George.Dunlap@citrix.com>
Cc: xen-devel <xen-devel@lists.xenproject.org>,
Nick Rosbrook <rosbrookn@ainfosec.com>,
Ian Jackson <iwj@xenproject.org>, Wei Liu <wl@xen.org>
Subject: Re: [RESEND PATCH 08/12] golang/xenlight: add functional options to configure Context
Date: Fri, 18 Jun 2021 13:00:34 -0400 [thread overview]
Message-ID: <YMzRMlaHapLn7msf@FED-nrosbr-BE.crux.rad.ainfosec.com> (raw)
In-Reply-To: <8727719E-9548-40CF-A186-14E2ECA3801D@citrix.com>
On Fri, Jun 18, 2021 at 04:18:44PM +0000, George Dunlap wrote:
>
>
> > On Jun 18, 2021, at 4:08 PM, Nick Rosbrook <rosbrookn@gmail.com> wrote:
> >
> > On Fri, Jun 18, 2021 at 02:44:15PM +0000, George Dunlap wrote:
> >>
> >>
> >>> On May 24, 2021, at 9:36 PM, Nick Rosbrook <rosbrookn@gmail.com> wrote:
> >>>
> >>> Add a ContextOption type to support functional options in NewContext.
> >>> Then, add a variadic ContextOption parameter to NewContext, which allows
> >>> callers to specify 0 or more configuration options.
> >>>
> >>> For now, just add the WithLogLevel option so that callers can set the
> >>> log level of the Context's xentoollog_logger. Future configuration
> >>> options can be created by adding an appropriate field to the
> >>> contextOptions struct and creating a With<OptionName> function to return
> >>> a ContextOption
> >>>
> >>> Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
> >>> ---
> >>> tools/golang/xenlight/xenlight.go | 44 +++++++++++++++++++++++++++++--
> >>> 1 file changed, 42 insertions(+), 2 deletions(-)
> >>>
> >>> diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
> >>> index f68d7b6e97..65f93abe32 100644
> >>> --- a/tools/golang/xenlight/xenlight.go
> >>> +++ b/tools/golang/xenlight/xenlight.go
> >>> @@ -136,7 +136,7 @@ func sigchldHandler(ctx *Context) {
> >>> }
> >>>
> >>> // NewContext returns a new Context.
> >>> -func NewContext() (ctx *Context, err error) {
> >>> +func NewContext(opts ...ContextOption) (ctx *Context, err error) {
> >>> ctx = &Context{}
> >>>
> >>> defer func() {
> >>> @@ -146,8 +146,19 @@ func NewContext() (ctx *Context, err error) {
> >>> }
> >>> }()
> >>>
> >>> + // Set the default context options. These fields may
> >>> + // be modified by the provided opts.
> >>> + copts := &contextOptions{
> >>> + logLevel: LogLevelError,
> >>> + }
> >>> +
> >>> + for _, opt := range opts {
> >>> + opt.apply(copts)
> >>> + }
> >>> +
> >>> // Create a logger
> >>> - ctx.logger = C.xtl_createlogger_stdiostream(C.stderr, C.XTL_ERROR, 0)
> >>> + ctx.logger = C.xtl_createlogger_stdiostream(C.stderr,
> >>> + C.xentoollog_level(copts.logLevel), 0)
> >>>
> >>> // Allocate a context
> >>> ret := C.libxl_ctx_alloc(&ctx.ctx, C.LIBXL_VERSION, 0,
> >>> @@ -201,6 +212,35 @@ func (ctx *Context) Close() error {
> >>> return nil
> >>> }
> >>>
> >>> +type contextOptions struct {
> >>> + logLevel LogLevel
> >>> +}
> >>> +
> >>> +// ContextOption is used to configure options for a Context.
> >>> +type ContextOption interface {
> >>> + apply(*contextOptions)
> >>> +}
> >>> +
> >>> +type funcContextOption struct {
> >>> + f func(*contextOptions)
> >>> +}
> >>> +
> >>> +func (fco *funcContextOption) apply(c *contextOptions) {
> >>> + fco.f(c)
> >>> +}
> >>
> >> Why all this convolution with interfaces and such, rather than just defining ContextOption as a function pointer? Is it just to keep contextOptions out of the documentation page?
> >
> > Part of the motivation for using functional options is to abstract the
> > "options" struct, yes. This allows internal defaults to be applied more
> > easily -- if you require e.g. a ContextOptions struct to be passed by
> > the caller, how do you know if they intended to override a default, or
> > if they just didn't set the field? Additionally, using the ContextOption
> > as an interface allows variadic arguments, which are just convenient for
> > API users -- the same NewContext function can be used whether you need
> > to pass 3 options or 0.
> >
> > The reason we use ContextOption as an interface, rather than function
> > pointer of sorts is for flexibility in the signatures of ContextOption
> > implementations. E.g., we could have
> >
> > func WithLogLevel(lvl LogLevel) ContextOption
> > func WithLogContext(s string) ContextOption
> > func WithFooAndBar(s string, n int) ContextOption
> >
> > See [1] for more background on this pattern.
> >
> > Thanks,
> > NR
> >
> > [1] https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
>
> Yes, I frequently use a pattern like the one described in that blog post myself. But that blog post doesn’t use interfaces — the final slide actually has the “option function” type as an open-coded function pointer type.
>
> So my question was, why not do something like this:
>
> type ContextOption func(*contextOptions) error
>
> func WithLogLevel(level LogLevel) ContextOption {
> return func(co *contextOptions) {
> co.logLevel = level
> }
> }
>
> ATM the only advantage I can see of defining ContextOption as an interface rather than as a function pointer is that the godoc for ContextOption would look like:
>
> type ContextOption interface {
> // contains filtered or unexported fields
> }
>
> Rather than
>
> type ContextOption func(*contextOptions) error
>
> Which shows you the name of the unexported field.
>
> Is there another reason I missed?
Technically it does allow more flexibility in implementing
ContextOption, e.g. you could do...
func (lvl LogLevel) apply(co *contextOptions) { co.logLevel = lvl }
...and then pass a LogLevel directly as a ContextOption. But generally
everyone implements these things as funcs.
I will admit that when it comes to my choice of using the interface
version instead of function pointers, I am just more familiar with the
former and encounter it more often in other Go packages I use.
Thanks,
NR
next prev parent reply other threads:[~2021-06-18 17:00 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-24 20:36 [RESEND PATCH 00/12] golang/xenlight: domain life cycle support Nick Rosbrook
2021-05-24 20:36 ` [RESEND PATCH 01/12] golang/xenlight: update generated code Nick Rosbrook
2021-06-18 10:30 ` George Dunlap
2021-05-24 20:36 ` [RESEND PATCH 02/12] golang/xenlight: fix StringList toC conversion Nick Rosbrook
2021-06-18 10:51 ` George Dunlap
2021-05-24 20:36 ` [RESEND PATCH 03/12] golang/xenlight: fix string conversion in generated toC functions Nick Rosbrook
2021-06-18 11:00 ` George Dunlap
2021-06-21 16:11 ` Nick Rosbrook
2021-07-01 14:09 ` George Dunlap
2021-07-07 19:29 ` Nick Rosbrook
2021-05-24 20:36 ` [RESEND PATCH 04/12] golang/xenlight: export keyed union interface types Nick Rosbrook
2021-06-18 11:19 ` George Dunlap
2021-05-24 20:36 ` [RESEND PATCH 05/12] golang/xenlight: use struct pointers in keyed union fields Nick Rosbrook
2021-06-18 11:26 ` George Dunlap
2021-05-24 20:36 ` [RESEND PATCH 06/12] golang/xenlight: rename Ctx receivers to ctx Nick Rosbrook
2021-06-18 11:39 ` George Dunlap
2021-05-24 20:36 ` [RESEND PATCH 07/12] golang/xenlight: add logging conveniences for within xenlight Nick Rosbrook
2021-06-18 13:17 ` George Dunlap
2021-06-18 13:21 ` George Dunlap
2021-06-18 15:26 ` Nick Rosbrook
2021-06-18 16:30 ` George Dunlap
2021-06-18 15:17 ` Nick Rosbrook
2021-06-18 16:28 ` George Dunlap
2021-05-24 20:36 ` [RESEND PATCH 08/12] golang/xenlight: add functional options to configure Context Nick Rosbrook
2021-06-18 14:44 ` George Dunlap
2021-06-18 15:08 ` Nick Rosbrook
2021-06-18 16:18 ` George Dunlap
2021-06-18 17:00 ` Nick Rosbrook [this message]
2021-06-18 18:12 ` George Dunlap
2021-05-24 20:36 ` [RESEND PATCH 09/12] golang/xenlight: add DomainDestroy wrapper Nick Rosbrook
2021-06-18 14:47 ` George Dunlap
2021-05-24 20:36 ` [RESEND PATCH 10/12] golang/xenlight: add SendTrigger wrapper Nick Rosbrook
2021-06-18 14:54 ` George Dunlap
2021-05-24 20:36 ` [RESEND PATCH 11/12] golang/xenlight: do not negate ret when converting to Error Nick Rosbrook
2021-06-18 15:13 ` George Dunlap
2021-06-18 15:32 ` Nick Rosbrook
2021-05-24 20:36 ` [RESEND PATCH 12/12] golang/xenlight: add NotifyDomainDeath method to Context Nick Rosbrook
2021-06-18 18:28 ` George Dunlap
2021-06-18 19:31 ` George Dunlap
2021-06-21 21:41 ` Nick Rosbrook
2021-06-17 15:29 ` [RESEND PATCH 00/12] golang/xenlight: domain life cycle support Nick Rosbrook
2021-06-21 15:53 ` George Dunlap
2021-06-21 16:19 ` Nick Rosbrook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YMzRMlaHapLn7msf@FED-nrosbr-BE.crux.rad.ainfosec.com \
--to=rosbrookn@gmail.com \
--cc=George.Dunlap@citrix.com \
--cc=iwj@xenproject.org \
--cc=rosbrookn@ainfosec.com \
--cc=wl@xen.org \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).