Documentation
¶
Overview ¶
Package null provides nullable types that can be used either for json marshaling/unmarshaling (even with non-default encoding/json) or sql/database interaction
Index ¶
- Variables
- type CustomJsonMarshaler
- type CustomJsonUnmarshaler
- type SupportedTypes
- type Type
- func (s *Type[T]) Equal(v Type[T]) bool
- func (s *Type[T]) IsNull() bool
- func (s Type[T]) MarshalJSON() ([]byte, error)
- func (s *Type[T]) RawValue() (T, bool)
- func (s *Type[T]) Scan(src any) error
- func (s *Type[T]) SetNull()
- func (s *Type[T]) SetValue(v T)
- func (s *Type[T]) UnmarshalJSON(b []byte) error
- func (s Type[T]) Value() (driver.Value, error)
- func (s *Type[T]) ValueOrZero() T
Constants ¶
This section is empty.
Variables ¶
var ( // JsonMarshaler is used for json marshaling. Default marshaler uses encoding/json package with no custom options. // Is called only when parameter value is not null JsonMarshaler CustomJsonMarshaler = func(value any) ([]byte, error) { return json.Marshal(value) } // JsonUnmarshaler is used for json unmarshaling. Default unmarshaler uses encoding/json package with no custom options. // Is called only when parameter b not equals to `null` JsonUnmarshaler CustomJsonUnmarshaler = func(b []byte, dst any) error { return json.Unmarshal(b, dst) } )
Functions ¶
This section is empty.
Types ¶
type CustomJsonMarshaler ¶
CustomJsonMarshaler is defined in order to allow you custom (even with not encoding/json) json marshaling
Parameter src is always one of the following types: int64, float64, bool, string, time.Time, so type switch may take place.
Default one is JsonMarshaler ¶
Example of implementing custom marshaler:
import "encoding/json"
import "time"
func Marshaler(value any) ([]byte, error) {
switch V := value.(type) {
case time.Time:
return []byte(V.Format("\"02.01.2006\"")), nil
default:
return json.Marshal(value)
}
}
type CustomJsonUnmarshaler ¶
CustomJsonUnmarshaler is defined in order to allow you custom (even with not encoding/json) json unmarshaling
Parameter dst is always one of the following types: *int64, *float64, *bool, *string, *time.Time, so type switch may take place.
Default one is JsonUnmarshaler ¶
Example of implementing custom unmarshaler:
import "encoding/json"
import "time"
func Unmarshaler(b []byte, dst any) error {
switch V := dst.(type) {
case *time.Time:
t, err := time.Parse("\"02.01.2006\"", string(b))
if err != nil {
return err
}
*V = t
return nil
default:
return json.Unmarshal(b, dst)
}
}
type SupportedTypes ¶
SupportedTypes provides types that can be used for generic Type
type Type ¶
type Type[T SupportedTypes] struct { // contains filtered or unexported fields }
Type represents a nullable type that can used either for json marshaling/unmarshaling or sql/database interaction
func NewType
deprecated
func NewType[T SupportedTypes](value T, ok bool) Type[T]
NewType provides typed Type with given value if ok is true, otherwise value is considered as null
Deprecated: second parameter is useless in current semantics. Function will be removed in future releases. Try NewTypeWithValue instead
func NewTypeWithValue ¶ added in v0.1.0
func NewTypeWithValue[T SupportedTypes](value T) Type[T]
NewTypeWithValue provides not null value of type T
func (*Type[T]) Equal ¶
Equal checks if v is equal to current Type. Returns true if both of Type are null or have equal not null values
func (Type[T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Uses JsonMarshaler for marshaling not null values
func (*Type[T]) RawValue ¶
RawValue returns actual value and true if Type is not null, otherwise default value for T and false
func (*Type[T]) SetNull ¶
func (s *Type[T]) SetNull()
SetNull sets Type to null. Existing value will be overwritten with default one
func (*Type[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. Uses JsonUnmarshaler for unmarshaling not null values
func (*Type[T]) ValueOrZero ¶
func (s *Type[T]) ValueOrZero() T
ValueOrZero returns either actual value or default value for type T depending on whether Type is null