Package create provides a generic option pattern for creating new values of any type

create

Go Reference

Package create provides a generic option pattern for creating new values of any type.

Install

go get github.com/norunners/create

Requires Go 1.18 or higher.

Examples

The Greeting type will be created throughout the examples.

type Greeting string

New with options

Create a Greeting with earth as the noun option.

greeting, err := create.New[Greeting](WithNoun("earth"))

Hello earth!

New without options

Create a Greeting without options so the defaults are used.

greeting, err := create.New[Greeting, *GreetingBuilder]()

Hello world!

Builder

Defining GreetingBuilder as a struct allows fields to be added over time.

type GreetingBuilder struct {
	noun string
}

Builder.Default

The Default method provides sensible default values.

func (*GreetingBuilder) Default() *GreetingBuilder {
	return &GreetingBuilder{
		noun: "world",
	}
}

Options

Defining GreetingOption allows functional options on the GreetingBuilder type.
Option WithNoun assigns a value to the noun field, which is not exported.

type GreetingOption func(*GreetingBuilder)

func WithNoun(noun string) GreetingOption {
	return func(b *GreetingBuilder) {
		b.noun = noun
	}
}

Builder.Build

The Build method validates the noun field and creates a new Greeting.

func (b *GreetingBuilder) Build() (Greeting, error) {
	if b.noun == "" {
		return "", fmt.Errorf("empty noun")
	}
	return Greeting(fmt.Sprintf("Hello %s!", b.noun)), nil
}

Instantiation

This instantiates NewGreeting from create.New.
All parameterized types are required for instantiation, e.g. no type inference.

var NewGreeting = create.New[Greeting, *GreetingBuilder, GreetingOption]
greeting, err := NewGreeting(...)

This can be useful as a package scoped variable, e.g. greeting.New.

Satisfy the Builder interface

This ensures GreetingBuilder satisfies create.Builder.

var _ create.Builder[Greeting, *GreetingBuilder] = (*GreetingBuilder)(nil)

Benefits

  1. A single future-proof function to create values.
  2. Provide sensible defaults for any type.
  3. Override defaults with options.
  4. Validate values before creation.
  5. Zero dependencies.

Why?

This is a bit of an experimental exercise of generics in Go but could also be seen as a standardized way to use the option pattern.

References

License

Owner
Gopher hacker and WebAssembly fan.
null
Similar Resources

Package trn introduces a Range type with useful methods to perform complex operations over time ranges

Time Ranges Package trn introduces a Range type with useful methods to perform c

Aug 18, 2022

The Operator Pattern, in Nomad

Nomad Operator Example Repostiory to go along with my The Operator Pattern in Nomad blog post. Usage If you have tmux installed, you can run start.sh

May 12, 2022

Code generator to help implement the visitor pattern.

mkvisitor Given package example type ( Node struct{} Leaf struct{} ) run mkvisitor -type "Node,Leaf" then generate package example import "fmt" t

Oct 6, 2021

A helm v3 plugin to get values from a previous release

helm-val helm-val is a helm plugin to fetch values from a previous release. Getting started Installation To install the plugin: $ helm plugin install

Dec 11, 2022

Render helm values-files from others

helm-plugin-render-values The Helm downloader plugin with rendering templated values files Install Use helm CLI to install this plugin: $ helm plugin

Aug 1, 2022

Drone conversion for platform values

drocopla A drone.io conversion extension to set host platform as drone pipeline platform. Default drone.io behaviour: If os/arch is not set in .drone.

Dec 1, 2021

A handy utility to generate configmap and values.yaml of your application for helmifying them

Helmfig Are you tired of writing values.yaml for configmap of your project when you are helmifying them? Helmfig is a handy tool that can generate the

Dec 14, 2022

Print specified values from desktop files to stdout.

Print specified values from desktop files to stdout.

dprint Print specified values from desktop files to stdout. Look, it’s hard to describe okay? Here’s a picture of me using it with dmenu. My launcher

Dec 22, 2021

MongoDB generic REST server in Go

MongoDB generic REST server in Go

Mora - Mongo Rest API REST server for accessing MongoDB documents and meta data Documents When querying on collections those parameters are available:

Dec 17, 2022
Related tags
`runenv` create gcloud run deploy `--set-env-vars=` option and export shell environment from yaml file.

runenv runenv create gcloud run deploy --set-env-vars= option and export shell environment from yaml file. Motivation I want to manage Cloud Run envir

Feb 10, 2022
Optional is a library that provides option types for Go types.

Option Optional is a library that provides option types for Go types. Installation Golang version 1.18 + required go get -u github.com/eatmoreapple/op

Nov 9, 2022
Provider-generic-workflows - A generic provider which uses argo workflows to define the backend actions.

provider-generic-workflows provider-generic-workflows is a generic provider which uses argo workflows for managing the external resource. This will re

Jan 1, 2022
Application open new tab in chrome when your favourite youtuber add new video.

youtube-opener This application open new tab in Chrome when your favourite youtuber add new video. It checks channel every one minute. How to run go r

Jan 16, 2022
Traefik-redirect-operator is created to substitute manual effort of creating an ingress and service type External.
Traefik-redirect-operator is created to substitute manual effort of creating an ingress and service type External.

Overview Traefik Redirect Operator is used to help creating a combination of Ingress of Traefik controller along with Service's ExternalName type. The

Sep 22, 2021
The fantastic Any type implementation for Go

Any The fantastic Any type implementation for Go. Overview New type which might be used as any type in Go. Getting Started package main import ( "fm

Dec 15, 2021
Poc rsa - A simple golang scaffolding to help me to create new api projects or workers with golang on k8s

go-scaffold A simple golang scaffolding to help me to create new api projects or

Feb 3, 2022
A tool to build, deploy, and release any application on any platform.
A tool to build, deploy, and release any application on any platform.

Waypoint Website: https://www.waypointproject.io Tutorials: HashiCorp Learn Forum: Discuss Waypoint allows developers to define their application buil

Dec 28, 2022
KEDA is a Kubernetes-based Event Driven Autoscaling component. It provides event driven scale for any container running in Kubernetes
 KEDA is a Kubernetes-based Event Driven Autoscaling component. It provides event driven scale for any container running in Kubernetes

Kubernetes-based Event Driven Autoscaling KEDA allows for fine-grained autoscaling (including to/from zero) for event driven Kubernetes workloads. KED

Jan 7, 2023