optional

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2025 License: MIT Imports: 7 Imported by: 1

README

optional

Go Reference License Build Status Go Report Card codecov Mentioned in Awesome Go

Type-safe optional values for Go - marshal/unmarshal to/from JSON, YAML, SQL and more.

Installation

go get github.com/kazhuravlev/optional

Quick Start

package main

import (
	"encoding/json"
	"fmt"
	"github.com/kazhuravlev/optional"
)

type User struct {
	Name      string               `json:"name"`
	AvatarURL optional.Val[string] `json:"avatar_url,omitempty"`
}

func main() {
	// JSON with optional field
	data := []byte(`{"name": "Alice", "avatar_url": "https://example.com/avatar.jpg"}`)
	
	var user User
	json.Unmarshal(data, &user)
	
	// Check if value exists
	if avatarURL, ok := user.AvatarURL.Get(); ok {
		fmt.Printf("Avatar URL: %s\n", avatarURL)
	}
	
	// Or use a default
	url := user.AvatarURL.ValDefault("https://example.com/default.jpg")
	fmt.Printf("URL with default: %s\n", url)
}

Key Features

  • Type-safe: Generic implementation prevents runtime type errors
  • Zero value friendly: Missing values are not mistaken for zero values
  • Multiple formats: Works with JSON, YAML, SQL, and custom marshalers
  • Simple API: Intuitive methods like Get(), Set(), HasVal(), and Reset()

API Overview

var opt optional.Val[string]

// Set a value
opt.Set("hello")

// Check and get
if val, ok := opt.Get(); ok {
    fmt.Println(val) // "hello"
}

// Get with default
val := opt.ValDefault("default") // "hello"

// Reset to empty
opt.Reset()

// Convert to pointer (nil if empty)
ptr := opt.AsPointer() // *string

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Val

type Val[T any] struct {
	// contains filtered or unexported fields
}

Val contains value and flag that mark this value as empty.

func Empty

func Empty[T any]() Val[T]

Empty create container without value.

func New

func New[T any](val T) Val[T]

func NewFromPointer

func NewFromPointer[T any](val *T) Val[T]

NewFromPointer create Val from pointer to some type. Nil pointer means that value not provided.

func (Val[T]) AsPointer

func (v Val[T]) AsPointer() *T

AsPointer adapt value to pointer. It will return nil when value not provided.

func (Val[T]) Get

func (v Val[T]) Get() (T, bool)

func (Val[T]) HasVal

func (v Val[T]) HasVal() bool

HasVal return value of flag hasVal.

func (Val[T]) MarshalJSON

func (v Val[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Val[T]) MarshalYAML

func (v Val[T]) MarshalYAML() (interface{}, error)

MarshalYAML implements the yaml.Marshaler interface.

func (*Val[T]) Reset added in v0.2.0

func (v *Val[T]) Reset()

Reset will clear value and mark this as empty.

func (*Val[T]) Scan

func (v *Val[T]) Scan(value any) error

Scan implements the Scanner interface.

func (*Val[T]) Set

func (v *Val[T]) Set(val T)

Set will set the value and mark that value is presented.

func (*Val[T]) UnmarshalJSON

func (v *Val[T]) UnmarshalJSON(buf []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Val[T]) UnmarshalYAML

func (v *Val[T]) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

func (Val[T]) Val

func (v Val[T]) Val() T

Val return internal value.

func (Val[T]) ValDefault

func (v Val[T]) ValDefault(defaultVal T) T

ValDefault returns value, if presented or defaultVal in other case.

func (Val[T]) Value

func (v Val[T]) Value() (driver.Value, error)

Value implements the driver Valuer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL