Git Product home page Git Product logo

options's Introduction

options

Use Case

You have a constructor-type func which accepts optional funcs as parameters. Rob Pike and Dave Cheney have discussed this in 2014 and 2016, respectively.

func SpecificThingOptionFunc func(st *SpecificThing) error

func SetHost(host string) error {
  return func(st *SpecificThing) error {
    st.host = host
    return nil
  }
}

type SpecificThing struct {
  host string
}

func NewSpecificThing(options ...SpecificThingOptionFunc) *SpecificThing {
  st := &SpecificThing{}
  for o := range options {
    o(st)
  }
  return st
}

func main() {
  st := NewSpecificThing(SetHost("example.com"))
  // ...
}

If a program has many specific things, it may have some common things, using struct cmoposition.

type CommonThing struct {
  loggingLevel int
}

type SpecificThing struct {
  CommonThing
  host string
}

Continue the options func pattern, CommonThing can also have option funcs.

func CommonThingOptionFunc func(ct *CommonThing) error

func SetLoggingLevel(loggingLevel int) error {
  return func(ct *CommonThing) error {
    ct.loggingLevel = loggingLevel
    return nil
  }
}

The problem comes when the SpecificThing constructor wants to apply option to its CommontThing. There are a couple of common patterns that can address this.

Multiple sets of options

A constructor may accept arrays of options of different types by dropping the vardiac syntax:

func NewSpecificThing(ct CommonThing, ctOptions []CommonThingOptionFunc, stOptions []SpecificThingOptionFunc) {
  ct := CommonThing {}
  for o := range ctOptions {
    o(ct)
  }
  st := &SpecificThing {
    CommonThing: ct,
  }
  for o := range stOptions {
    o(st)
  }
  return st
}

This rapidly becomes inellegant and unweildy as the number of embedded structures with options grows. For example, if CommonThing also embeds another struct, and that struct embeds one, etc., the argument list rapidly grows out of control.

Dependency Injection

A constructor may require that all of its constituant parts may already exist at the time of construction:

func NewSpecificThing(ct CommonThing, options ...SpecificThingOptionFunc) {
  st := &SpecificThing {
    CommonThing: ct,
  }
  for o := range options {
    o(st)
  }
  return st
}

This pattern should scale more elegantly. However, this looks a little like dependency injection, which is not appropriate for every project.

Motivation

The goals of this project are to allow a single options func to be applicable across many embedded structs.

Usage

options github.com/object88/hoarding:Options,Suboptions github.com/object88/hoarding/internal:Options

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.