Created
June 3, 2019 11:26
-
-
Save vchakoshy/f34f2365dc1698ce9892f368bd206208 to your computer and use it in GitHub Desktop.
Golang Functional Constructor
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 | |
| // for more information please look at: | |
| // https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis | |
| // https://www.reddit.com/r/golang/comments/6mtbx9/useful_constructors_in_go/ | |
| // OutputComponent struct | |
| type OutputComponent struct { | |
| Title string `json:"title,omitempty"` | |
| Icon string `json:"icon,omitempty"` | |
| SubTitle string `json:"subTitle,omitempty"` | |
| Type string `json:"type,omitempty"` | |
| ResourceType string `json:"resource_type,omitempty"` | |
| Action *Action `json:"action,omitempty"` | |
| ActionTitle string `json:"actionTitle,omitempty"` | |
| } | |
| // OutputComponentOptionFunc function | |
| type OutputComponentOptionFunc func(*OutputComponent) error | |
| // NewOutputComponent return new output comment | |
| func NewOutputComponent(options ...OutputComponentOptionFunc) (*OutputComponent, error) { | |
| o := &OutputComponent{} | |
| for _, option := range options { | |
| if err := option(o); err != nil { | |
| return o, err | |
| } | |
| } | |
| return o, nil | |
| } | |
| // OutputComponentSetTitle set title of struct | |
| func OutputComponentSetTitle(title string) OutputComponentOptionFunc { | |
| return func(o *OutputComponent) error { | |
| o.Title = title | |
| return nil | |
| } | |
| } | |
| // OutputComponentSetSubTitle set subtitle of struct | |
| func OutputComponentSetSubTitle(title string) OutputComponentOptionFunc { | |
| return func(o *OutputComponent) error { | |
| o.SubTitle = title | |
| return nil | |
| } | |
| } | |
| // OutputComponentSetIcon set icon of struct | |
| func OutputComponentSetIcon(icon string) OutputComponentOptionFunc { | |
| return func(o *OutputComponent) error { | |
| o.Icon = icon | |
| return nil | |
| } | |
| } | |
| // OutputComponentSetActionTitle set actionTitle of struct | |
| func OutputComponentSetActionTitle(title string) OutputComponentOptionFunc { | |
| return func(o *OutputComponent) error { | |
| o.ActionTitle = title | |
| return nil | |
| } | |
| } | |
| // OutputComponentSetAction set action of struct | |
| func OutputComponentSetAction(action *Action) OutputComponentOptionFunc { | |
| return func(o *OutputComponent) error { | |
| if action.Type != "" { | |
| o.Action = action | |
| } | |
| return nil | |
| } | |
| } | |
| // OutputComponentSetResourceType set resource type of struct | |
| func OutputComponentSetResourceType(t string) OutputComponentOptionFunc { | |
| return func(o *OutputComponent) error { | |
| o.ResourceType = t | |
| return nil | |
| } | |
| } | |
| // OutputComponentSetType set type of struct | |
| func OutputComponentSetType(t string) OutputComponentOptionFunc { | |
| return func(o *OutputComponent) error { | |
| o.Type = t | |
| return nil | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment