omit

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2025 License: Apache-2.0 Imports: 4 Imported by: 1

README

Go Reference Go Report Go Version License omit Version Discord

omit

omit is a simple library to handle optional and nullable struct field values for json serialization and deserialization in golang.

Installation

go get github.com/disgoorg/omit

Usage

package main

import (
	"encoding/json"
	"fmt"

	"github.com/disgoorg/omit"
)

type User struct {
	ID    int                `json:"id"`
	Name  string             `json:"name"`
	Email omit.Omit[*string] `json:"email,omitzero"`
}

func main() {
	u := User{
		ID:    1,
		Name:  "John Doe",
		Email: omit.NewPtr("[email protected]"),
	}

	// Marshal
	data, err := json.Marshal(u)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(data))
}

Documentation

Documentation is wip and can be found under:

  • Go Reference

Troubleshooting

For help feel free to open an issue or reach out on Discord

Contributing

Contributions are welcomed but for bigger changes we recommend first reaching out via Discord or create an issue to discuss your problems, intentions and ideas.

License

Distributed under the License. See LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to the given value.

Types

type Omit

type Omit[T any] struct {
	Value T
	OK    bool
}

Omit is a type that can be used to represent a value which may or may not be set. This is useful for omitting the value in JSON. The zero value of Omit is not set.

func New

func New[T any](v T) Omit[T]

New creates a new Omit with the given value set.

func NewNilPtr

func NewNilPtr[T any]() Omit[*T]

NewNilPtr creates a new Omit with the value set to nil.

func NewPtr

func NewPtr[T any](v T) Omit[*T]

NewPtr creates a new Omit with the given pointer value set.

func NewZero

func NewZero[T any]() Omit[T]

NewZero creates a new Omit with the value not set.

func (Omit[T]) IsZero

func (o Omit[T]) IsZero() bool

IsZero returns true if the value is not set. This is useful for omitting the value in JSON.

func (Omit[T]) MarshalBinary

func (o Omit[T]) MarshalBinary() ([]byte, error)

MarshalBinary marshals the value if it is set, otherwise it returns nil.

func (Omit[T]) MarshalJSON

func (o Omit[T]) MarshalJSON() ([]byte, error)

MarshalJSON marshals the value if it is set, otherwise it returns nil.

func (Omit[T]) MarshalText

func (o Omit[T]) MarshalText() ([]byte, error)

MarshalText marshals the value if it is set, otherwise it returns nil.

func (Omit[T]) Or

func (o Omit[T]) Or(def T) T

Or returns the value if it is set, otherwise it returns the default value.

func (Omit[T]) String

func (o Omit[T]) String() string

String returns the string representation of the value if it is set, otherwise it returns "<omitted>".

func (*Omit[T]) UnmarshalBinary

func (o *Omit[T]) UnmarshalBinary(data []byte) error

UnmarshalBinary unmarshals the value if it is set, otherwise it returns nil.

func (*Omit[T]) UnmarshalJSON

func (o *Omit[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the value if it is set, otherwise it returns nil.

func (*Omit[T]) UnmarshalText

func (o *Omit[T]) UnmarshalText(data []byte) error

UnmarshalText unmarshals the value if it is set, otherwise it returns nil.

Jump to

Keyboard shortcuts

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