OptionalType is a generic type wrapper in Go, designed to help you better handle optional values, especially when dealing with JSON data, enabling you to distinguish between unassigned and null cases.
import "optional"This package requires Go 1.18.
- Provides a generic
OptionalType[T]struct that allows you to store a value of any typeT. - Contains methods to check whether the value is assigned (
IsAssigned) and whether it isnull(IsNull). - Supports custom
UnmarshalJSONandMarshalJSONmethods to correctly handle unassigned andnullcases in JSON data.
type OptionalType[T any] struct {
value T
isAssigned bool
isNull bool
}value: Stores the actual value of type T.isAssigned: Indicates whether the value has been assigned, i.e., whether it has been set.isNull: Indicates whether the value is null.
Here is a simple example showing how to use OptionalType to handle JSON data:
package main
import (
"encoding/json"
"fmt"
"log"
"optional"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
// Example 1: Unmarshal from a JSON string with valid data
jsonString1 := `{"name":"John","age":30}`
var personOpt1 optional.OptionalType[Person]
if err := json.Unmarshal([]byte(jsonString1), &personOpt1); err!= nil {
log.Fatal(err)
}
fmt.Printf("Example 1: %+v\n", personOpt1)
// Output: Example 1: {value:{Name:John Age:30} isAssigned:true isNull:false}
// Example 2: Unmarshal from a JSON string with null value
jsonString2 := `null`
var personOpt2 optional.OptionalType[Person]
if err := json.Unmarshal([]byte(jsonString2), &personOpt2); err!= nil {
log.Fatal(err)
}
fmt.Printf("Example 2: %+v\n", personOpt2)
// Output: Example 2: {value:{Name: Age:0} isAssigned:true isNull:true}
// Example 3: Unmarshal from an empty JSON object
jsonString3 := `{}`
var personOpt3 optional.OptionalType[Person]
if err := json.Unmarshal([]byte(jsonString3), &personOpt3); err!= nil {
log.Fatal(err)
}
fmt.Printf("Example 3: %+v\n", personOpt3)
// Output: Example 3: {value:{Name: Age:0} isAssigned:true isNull:false}
}func NewOptional[T any](value T, options...bool) OptionalType[T]- Creates a new
OptionalType[T]instance. - Parameters:
value: The initial value.options(optional):- The first boolean value (if provided) indicates
isAssigned. - The second boolean value (if provided) indicates
isNull.
- The first boolean value (if provided) indicates
func (opt OptionalType[T]) IsAssigned() bool- Checks whether the value of
OptionalTypeis assigned.
func (opt OptionalType[T]) IsNull() bool- Checks whether the value of
OptionalTypeisnull.
func (opt OptionalType[T]) GetOptioanlValue() *T- Returns the pointer of the value stored in
OptionalType. - Returns nil if
isNullis true.
func (opt OptionalType[T]) GetValue() T- Returns the actual value stored in
OptionalType.
func (opt *OptionalType[T]) SetValue(value T)- Sets the value of
OptionalType. - Sets
isAssignedtotrue,isNulltofalse, and stores the value.
func (opt *OptionalType[T]) SetNull()- Sets
OptionalTypeto null. - Sets
isAssignedtotrueandisNulltotrue.
This package is licensed under the MIT License.