{"modulePath":"github.com/CompassLabs/cli","version":"v0.1.81","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package optionalnullable\n\n## Functions\n\n```go\nfunc FromReflect(typ reflect.Type, innerPtr reflect.Value) reflect.Value\n```\nFromReflect is the reflection-based counterpart to From: it returns a new OptionalNullable value of type typ (which must satisfy IsOptionalNullableType) set to the value innerPtr points at. innerPtr must be assignable to typ's element type (\\*T); pass a nil \\*T to set the value to null.\n\n```go\nfunc IsOptionalNullableType(typ reflect.Type) bool\n```\nIsOptionalNullableType reports whether typ is an OptionalNullable\\[T] type. It is the type-level counterpart to AsOptionalNullable, for reflection-based code that only has a reflect.Type (e.g. when deciding how to decode into a zero value).\n\n## Types\n\n```go\ntype OptionalNullable[T any] map[bool]*T\n```\nOptionalNullable represents a field that can distinguish between three states: 1. Set to a value: field is present with a non-nil value 2. Set to nil: field is present but explicitly set to null in JSON 3. Unset: field is omitted from JSON entirely\n\nThis type is designed to work with JSON marshaling/unmarshaling and supports the \\`omitempty\\` struct tag to properly omit unset fields from JSON output.\n\nUsage:\n\n\ttype User struct {\n\t    Name  OptionalNullable[string] `json:\"name,omitempty\"`\n\t    Age   OptionalNullable[int]    `json:\"age,omitempty\"`\n\t    Tags  OptionalNullable[[]string] `json:\"tags,omitempty\"`\n\t}\n\n\t// Set to value\n\tname := \"John\"\n\tuser.Name = From(\u0026name)\n\n\t// Set to nil (will appear as \"name\": null in JSON)\n\tuser.Name = From[string](nil)\n\n\t// Leave unset (will be omitted from JSON with omitempty)\n\tuser := User{}\n\nWARNING: Do NOT use \\*OptionalNullable\\[T] as a field type. Always use OptionalNullable\\[T] directly. Using \\*OptionalNullable\\[T] will break the omitempty behavior and JSON marshaling.\n\nThe type is implemented as a map\\[bool]\\*T where: - nil map represents unset state - Map with true key represents set state (value may be nil)\n\n```go\nfunc From[T any](value *T) OptionalNullable[T]\n```\nFrom creates a new OptionalNullable with the given value. Pass nil to create a OptionalNullable that is set to null. Pass a pointer to a value to create a OptionalNullable with that value.\n\nExamples:\n\n\thello := \"hello\"\n\tFrom(\u0026hello)  // set to \"hello\"\n\tFrom[string](nil)       // set to null\n\n```go\nfunc (n OptionalNullable[T]) Get() (*T, bool)\n```\nGet returns the internal pointer and whether the field was set.\n\nReturn values: - (ptr, true): field was set (ptr may be nil if set to null) - (nil, false): field was unset/omitted\n\nThis method provides direct access to the internal pointer representation.\n\n```go\nfunc (n OptionalNullable[T]) GetOrZero() (T, bool)\n```\nGetOrZero returns the value and whether it was set.\n\nReturn values: - (value, true): field was set to a non-nil value - (zero, true): field was explicitly set to nil - (zero, false): field was unset/omitted\n\nExamples:\n\n\tval, ok := nullable.GetOrZero()\n\tif !ok {\n\t    // Field was unset/omitted\n\t} else if nullable.IsNull() {\n\t    // Field was explicitly set to null\n\t} else {\n\t    // Field has a value: val\n\t}\n\n```go\nfunc (n OptionalNullable[T]) GetUntyped() (interface{}, bool)\n```\nGetUntyped returns the value as interface{} and whether it was set. This is useful for reflection-based code that needs to work with the value without knowing the specific type T.\n\nReturn values: - (value, true): field was set to a non-nil value - (nil, true): field was explicitly set to nil - (nil, false): field was unset/omitted\n\n```go\nfunc (n OptionalNullable[T]) IsNull() bool\n```\nIsNull returns true if the OptionalNullable is explicitly set to nil. Returns false if the OptionalNullable is unset or has a value.\n\nNote: This differs from traditional null checks because unset fields return false, not true. Use IsSet() to check if a field was provided.\n\n```go\nfunc (n OptionalNullable[T]) IsSet() bool\n```\nIsSet returns true if the OptionalNullable has been explicitly set (to either a value or nil). Returns false if the OptionalNullable is unset (omitted from JSON).\n\nThis is the key method for distinguishing between: - Set to nil: IsSet() = true, IsNull() = true - Unset: IsSet() = false, IsNull() = false\n\n```go\nfunc (n OptionalNullable[T]) MarshalJSON() ([]byte, error)\n```\nMarshalJSON implements json.Marshaler.\n\nBehavior: - Unset fields: omitted from JSON when struct field has omitempty tag - Null fields: serialized as \"null\" - Value fields: serialized as the actual value\n\nThe omitempty behavior works because an empty map is considered a zero value by Go's JSON package.\n\n```go\nfunc (n *OptionalNullable[T]) Set(value *T)\n```\nSet sets the OptionalNullable to the given value pointer. Pass nil to set the field to null. Pass a pointer to a value to set the field to that value.\n\nExamples:\n\n\tnullable.Set(ptrFrom(\"hello\"))  // set to \"hello\"\n\tnullable.Set(nil)               // set to null\n\n```go\nfunc (n *OptionalNullable[T]) UnmarshalJSON(data []byte) error\n```\nUnmarshalJSON implements json.Unmarshaler.\n\nBehavior: - \"null\" in JSON: sets the field to null (IsSet=true, IsNull=true) - Any other value: sets the field to that value (IsSet=true, IsNull=false) - Missing from JSON: field remains unset (IsSet=false, IsNull=false)\n\n```go\nfunc (n *OptionalNullable[T]) Unset()\n```\nUnset removes the value, making the field unset/omitted. After calling Unset(), IsSet() will return false and the field will be omitted from JSON output when using omitempty.\n\n```go\ntype OptionalNullableInterface interface {\n\tGetUntyped() (interface{}, bool)\n}\n```\nNullableInterface defines the interface that all OptionalNullable\\[T] types implement. This interface provides untyped access to optional nullable values for reflection-based code.\n\n```go\nfunc AsOptionalNullable(v reflect.Value) (OptionalNullableInterface, bool)\n```\nAsOptionalNullable attempts to convert a reflect.Value to a OptionalNullableInterface. This is a helper function for reflection-based code that needs to check if a value implements the optional nullable interface pattern.\n\nReturns: - (nullable, true): if the value implements OptionalNullableInterface - (nil, false): if the value does not implement OptionalNullableInterface\n\nExample usage:\n\n\tif nullable, ok := AsOptionalNullable(reflectValue); ok {\n\t    if value, isSet := nullable.GetUntyped(); isSet {\n\t        // Handle the nullable value\n\t    }\n\t}\n\n","path":"github.com/CompassLabs/cli/internal/sdk/optionalnullable","name":"optionalnullable","synopsis":"","isRedistributable":true}
