Created
June 21, 2023 12:44
-
-
Save chmouel/d1cd71c98e8e35c221e412dff8fe3ed6 to your computer and use it in GitHub Desktop.
This file contains 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" | |
"log" | |
"regexp" | |
) | |
type Option interface { | |
Validate(opt any) error | |
} | |
type String struct{} | |
func (s String) Validate(opt any) error { | |
if _, ok := opt.(string); !ok { | |
return fmt.Errorf("not a string") | |
} | |
return nil | |
} | |
type Bool struct{} | |
func (b Bool) Validate(opt any) error { | |
if _, ok := opt.(bool); !ok { | |
return fmt.Errorf("not a bool") | |
} | |
return nil | |
} | |
type Int struct{} | |
func (b Int) Validate(opt any) error { | |
if _, ok := opt.(int); !ok { | |
return fmt.Errorf("not a int") | |
} | |
return nil | |
} | |
type Regexp struct{} | |
func (b Regexp) Validate(opt any) error { | |
if _, err := regexp.Compile(opt.(string)); err != nil { | |
return fmt.Errorf("not a regexp: %w", err) | |
} | |
return nil | |
} | |
type Base struct { | |
thetype Option | |
name string | |
defaultv any | |
value any | |
} | |
func (o *Base) SetName(name string) *Base { | |
o.name = name | |
return o | |
} | |
func (o *Base) SetDefault(value any) error { | |
if err := o.Validate(value); err != nil { | |
return err | |
} | |
o.defaultv = value | |
o.value = value | |
return nil | |
} | |
func (o *Base) Name() string { | |
return o.name | |
} | |
func (o *Base) Value() any { | |
switch o.thetype.(type) { | |
case Bool: | |
return o.value.(bool) | |
case Int: | |
return o.value.(int) | |
case Regexp: | |
return regexp.MustCompile(o.value.(string)) | |
case String: | |
return o.value.(string) | |
} | |
return nil | |
} | |
func (o *Base) Validate(opt any) error { | |
return o.thetype.Validate(opt) | |
} | |
func (o *Base) SetValue(value string) *Base { | |
o.value = value | |
return o | |
} | |
func (o *Base) SetType(thetype Option) *Base { | |
o.thetype = thetype.(Option) | |
return o | |
} | |
func main() { | |
astr := Base{} | |
if err := astr.SetName("anstr").SetType(String{}).SetDefault("foo bar"); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("astr.Value(): %v\n", astr.Value()) | |
abool := Base{} | |
if err := abool.SetName("boolopt").SetType(Bool{}).SetDefault(true); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("abool.Value(): %v\n", abool.Value()) | |
aint := Base{} | |
if err := aint.SetName("intopt").SetType(Int{}).SetDefault(100); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("aint.Value(): %v\n", aint.Value()) | |
aregexp := Base{} | |
if err := aregexp.SetName("aregexp").SetType(Regexp{}).SetDefault(`.+\w+\s\d{1,}`); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("are.Value(): %v\n", aregexp.Value()) | |
} | |
// -*- mode: compilation; default-directory: "~/Sync/goplay/casetting-at-2023-06-21-133939/" -*- | |
// Compilation started at Wed Jun 21 14:43:06 | |
// go run ./ | |
// astr.Value(): foo bar | |
// abool.Value(): true | |
// aint.Value(): 100 | |
// are.Value(): .+\w+\s\d{1,} | |
// Compilation finished at Wed Jun 21 14:43:06 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment