Skip to content

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.

Notifications You must be signed in to change notification settings

JsForLife/optional-type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OptionalType Package

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.

Features

  • Provides a generic OptionalType[T] struct that allows you to store a value of any type T.
  • Contains methods to check whether the value is assigned (IsAssigned) and whether it is null (IsNull).
  • Supports custom UnmarshalJSON and MarshalJSON methods to correctly handle unassigned and null cases in JSON data.

Struct Definition

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.

Basic Usage

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}
}

Functions and Methods

NewOptional

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.

IsAssigned

func (opt OptionalType[T]) IsAssigned() bool
  • Checks whether the value of OptionalType is assigned.

IsNull

func (opt OptionalType[T]) IsNull() bool
  • Checks whether the value of OptionalType is null.

GetOptioanlValue

func (opt OptionalType[T]) GetOptioanlValue() *T
  • Returns the pointer of the value stored in OptionalType.
  • Returns nil if isNull is true.

GetValue

func (opt OptionalType[T]) GetValue() T
  • Returns the actual value stored in OptionalType.

SetValue

func (opt *OptionalType[T]) SetValue(value T)
  • Sets the value of OptionalType.
  • Sets isAssigned to true, isNull to false, and stores the value.

SetNull

func (opt *OptionalType[T]) SetNull()
  • Sets OptionalType to null.
  • Sets isAssigned to true and isNull to true.

License

This package is licensed under the MIT License.

About

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.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages