union

package
v0.0.0-...-406b1e7 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package union contains shared code for union types.

In Go, one could use any to store values of various types. Union is different in a few important ways:

  • Only a restricted set of types is admitted, whereas any can hold any type.
  • Union types serialize in a way that makes it possible to distinguish among the possible types. Currently this is supported for types that serialize as strings; a possible development is to support types that serialize as other kinds of JSON nodes.
  • Union types are indexable by Limestone (implement indices.IndexKey).

Go isn't flexible enough to make creation of new union types fully automatic, so this package only contains a common foundation for building your own union type. See iam/entities/irealm for an example of how to define a union type based on this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Union

type Union struct {
	// contains filtered or unexported fields
}

A Union is a definition of a union type

func New

func New(variants map[string]any) Union

New creates a new Union based on the given variants. The keys of the given map should be tags, and the values should be examples of variant type values.

func (Union) IndexKey

func (u Union) IndexKey(v any) ([]byte, bool)

IndexKey is a helper for implementing IndexKey expected by Limestone indices for union types

func (Union) MarshalText

func (u Union) MarshalText(v any) ([]byte, error)

MarshalText is a helper for implementing encoding.TextMarshaler for union types

func (Union) String

func (u Union) String(v any) string

String is a helper for implementing fmt.Stringer for union types

func (Union) StringValue

func (u Union) StringValue(v any) string

StringValue returns a string representation of the value without the type tag (useful for API representation)

func (Union) Type

func (u Union) Type(v any) string

Type returns the type tag for the given value

func (Union) UnmarshalText

func (u Union) UnmarshalText(text []byte) (any, error)

UnmarshalText is a helper for implementing encoding.TextUnmarshaler for union types

func (Union) Validate

func (u Union) Validate(v any) error

Validate checks if the given value has one of the allowed types

Jump to

Keyboard shortcuts

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