Skip to content

Instantly share code, notes, and snippets.

@jdoliner
Created October 11, 2017 21:12
Show Gist options
  • Save jdoliner/b115e89eefda2b53d78e264519f7347e to your computer and use it in GitHub Desktop.
Save jdoliner/b115e89eefda2b53d78e264519f7347e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"reflect"
)
// Option types
type option interface {
Do(opts interface{})
}
type pipelineOption struct {
getLogsTag
inspectPipelineTag
pipeline string
}
func WithPipeline(pipeline string) *pipelineOption {
return &pipelineOption{
pipeline: pipeline,
}
}
func (p *pipelineOption) Do(opts interface{}) {
reflect.ValueOf(opts).FieldByName("Pipeline").Set(reflect.ValueOf(p.pipeline))
}
// One "Options" for each API
/// GetLogs
type getLogsOptions struct {
Pipeline string
}
type getLogsOption interface {
option
isGetLogsOption()
}
type getLogsTag struct{}
func (getLogsTag) isGetLogsOption() {}
func GetLogs(ops ...getLogsOption) {
opts := &getLogsOptions{}
for _, op := range ops {
op.Do(opts)
}
fmt.Printf("GetLogs: %v\n", opts.Pipeline)
}
/// InspectPipeline
type inspectPipelineOptions struct {
Pipeline string
}
type inspectPipelineOption interface {
option
isInspectPipelineOption()
}
type inspectPipelineTag struct{}
func (inspectPipelineTag) isInspectPipelineOption() {}
func InspectPipeline(ops ...inspectPipelineOption) {
opts := &inspectPipelineOptions{}
for _, op := range ops {
op.Do(opts)
}
fmt.Printf("InspectPipeline: %v\n", opts.Pipeline)
}
func main() {
GetLogs(WithPipeline("hello"))
InspectPipeline(WithPipeline("world"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment