From e86446ff5b1f66b8afbbe00ffc386a46187a28ee Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Mon, 8 Aug 2022 10:05:56 -0700 Subject: [PATCH] removed the pre-generics implementation of an optional-type / option-type / maybe-type for the Go programming language --- string/gen_optiontype.go | 248 --------------------------------------- string/optiontype.go | 3 - 2 files changed, 251 deletions(-) delete mode 100644 string/gen_optiontype.go delete mode 100644 string/optiontype.go diff --git a/string/gen_optiontype.go b/string/gen_optiontype.go deleted file mode 100644 index 5bf16aa..0000000 --- a/string/gen_optiontype.go +++ /dev/null @@ -1,248 +0,0 @@ -package optstring - -/* - * CODE GENERATED AUTOMATICALLY WITH https://github.com/reiver/gogen-optiontype - * THIS FILE SHOULD NOT BE EDITED BY HAND - */ - -import ( - "bytes" - "database/sql/driver" - "encoding/json" - "errors" - "fmt" -) - -var ( - errNilReceiver = errors.New("Nil Receiver") - errNone = errors.New("optstring.None()") - errNoneNullable = errors.New("optstring.NoneNullable()") - errNull = errors.New("optstring.Null()") -) - -func (receiver *NullableType) Scan(src interface{}) error { - if nil == receiver { - return errNilReceiver - } - - if nil == src { - *receiver = Null() - return nil - } - - switch t := src.(type) { - case NullableType: - *receiver = t - return nil - case Type: - switch t { - case None(): - *receiver = NoneNullable() - default: - datum, err := t.String() - if nil != err { - return fmt.Errorf("Problem unwrapping %T: (%T) %v", t, err, err) - } - *receiver = SomeNullable(datum) - } - return nil - case string: - *receiver = SomeNullable(t) - return nil - case []byte: - s := string(t) - *receiver = SomeNullable(s) - return nil - default: - return fmt.Errorf("Cannot scan something of type %T into an %T.", src, *receiver) - } -} - -func (receiver NullableType) String() (string, error) { - if NoneNullable() == receiver { - return "", errNoneNullable - } - if Null() == receiver { - return "", errNull - } - - return receiver.value, nil -} - -type NullableType struct { - loaded bool - null bool - value string -} - -func (receiver NullableType) MarshalJSON() ([]byte, error) { - if NoneNullable() == receiver { - return nil, errNoneNullable - } - if Null() == receiver { - return json.Marshal(nil) - } - - return json.Marshal(receiver.value) -} - -func (receiver NullableType) WhenNone(fn func()) { - if NoneNullable() == receiver { - fn() - } -} - -func (receiver NullableType) WhenNull(fn func()) { - if Null() == receiver { - fn() - } -} - -func (receiver NullableType) WhenSome(fn func(string)) { - if NoneNullable() != receiver && Null() != receiver { - fn(receiver.value) - } -} - -func (receiver *NullableType) UnmarshalJSON(b []byte) error { - if nil == receiver { - return errNilReceiver - } - - if 0 == bytes.Compare(b, []byte{'n','u','l','l'}) { - *receiver = Null() - return nil - } - - var target string - - if err := json.Unmarshal(b, &target); nil != err { - return err - } - - *receiver = SomeNullable(target) - - return nil -} - -func (receiver NullableType) Value() (driver.Value, error) { - if NoneNullable() == receiver { - return nil, errNoneNullable - } - if Null() == receiver { - return nil, nil - } - - return receiver.value, nil -} - -func NoneNullable() NullableType { - return NullableType{} -} - -func SomeNullable(value string) NullableType { - return NullableType{ - value: value, - loaded: true, - } -} - -func Null() NullableType { - return NullableType{ - null: true, - loaded: true, - } -} - -func (receiver *Type) Scan(src interface{}) error { - if nil == receiver { - return errNilReceiver - } - - switch t := src.(type) { - case Type: - *receiver = t - return nil - case string: - *receiver = Some(t) - return nil - case []byte: - s := string(t) - *receiver = Some(s) - return nil - default: - return fmt.Errorf("Cannot scan something of type %T into an %T.", src, *receiver) - } -} - -func (receiver Type) String() (string, error) { - if None() == receiver { - return "", errNone - } - - return receiver.value, nil -} - -type Type struct { - loaded bool - value string -} - -func (receiver Type) MarshalJSON() ([]byte, error) { - if None() == receiver { - return nil, errNone - } - - return json.Marshal(receiver.value) -} - -func (receiver *Type) UnmarshalJSON(b []byte) error { - if nil == receiver { - return errNilReceiver - } - - if 0 == bytes.Compare(b, []byte{'n','u','l','l'}) { - return fmt.Errorf("Cannot unmarshal %q into %T.", string(b), *receiver) - } - - var target string - - if err := json.Unmarshal(b, &target); nil != err { - return err - } - - *receiver = Some(target) - - return nil -} - -func (receiver Type) WhenNone(fn func()) { - if None() == receiver { - fn() - } -} - -func (receiver Type) WhenSome(fn func(string)) { - if None() != receiver { - fn(receiver.value) - } -} - -func (receiver Type) Value() (driver.Value, error) { - if None() == receiver { - return nil, errNone - } - - return receiver.value, nil -} - -func None() Type { - return Type{} -} - -func Some(value string) Type { - return Type{ - value: value, - loaded: true, - } -} diff --git a/string/optiontype.go b/string/optiontype.go deleted file mode 100644 index a411f77..0000000 --- a/string/optiontype.go +++ /dev/null @@ -1,3 +0,0 @@ -package optstring - -//go:generate gogen-optiontype --pkg=optstring --type=string --one-file --no-tests