Created
October 11, 2017 21:12
-
-
Save jdoliner/b115e89eefda2b53d78e264519f7347e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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