Skip to content

Releases: coregx/opt

v0.3.0 — Option[T] rename, driver.Valuer fix

Choose a tag to compare

@kolkov kolkov released this 05 Jul 06:13
4cb5c91

Breaking Changes

  • Value[T] renamed to Option[T] — fixes driver.Valuer shadow bug where all concrete types failed to implement driver.Valuer due to Go selector rules
  • Field.ToValue() renamed to Field.ToOption()

New

  • FieldFromOption[T]() — lossless conversion from Option[T] to Field[T] for PATCH workflows
  • Compile-time interface checks — verifies driver.Valuer, sql.Scanner, json.Marshaler on all 16 concrete types

Fixed

  • All concrete types now correctly implement driver.Valuer via method promotion
  • pgx v5 INSERT/Exec works without explicit forwarding methods
  • doc.go corrected: driver.Valuer claim is now accurate

Migration from v0.2.0

opt.Value[T]        →  opt.Option[T]
.Value field access  →  .Option
.ToValue()          →  .ToOption()

Concrete types (opt.String, opt.Int, etc.) and constructors (New, From, FromPtr, OrNull) are unchanged.

v0.2.0 — OrNull constructors

Choose a tag to compare

@kolkov kolkov released this 04 Jul 17:42
0f3d14f

OrNull — zero value → null mapping

New constructors that treat zero values as "not set" → null. Eliminates boilerplate helpers in DB/API projects.

Added

  • Generic OrNull[T]opt.OrNull(0) → null, opt.OrNull(42) → valid
  • Typed constructorsStringOrNull, IntOrNull, Int32OrNull, Int16OrNull, FloatOrNull, BoolOrNull, ByteOrNull, TimeOrNull

Usage

// Before (boilerplate in every project)
func optStr(s string) opt.String {
    return opt.NewString(s, s != "")
}
row.City = optStr(c.City())

// After (library API)
row.City = opt.StringOrNull(c.City())

Constructor Guide

Constructor Semantics
From(v) Always valid — zero IS a value
OrNull(v) Zero → null — zero means "not set"
FromPtr(p) nil → null
New(v, valid) Manual control

Install

go get github.com/coregx/opt@v0.2.0

Full docs: README | pkg.go.dev

v0.1.0 — Optional types for Go

Choose a tag to compare

@kolkov kolkov released this 04 Jul 15:55

First Release

opt — Go-idiomatic Option<T> with full SQL + JSON integration.

Highlights

  • Generic Value[T] — foundation on sql.Null[T] (Go 1.24+)
  • 9 concrete types — String, Int, Int32, Int16, Float, Bool, Byte, Time
  • Field[T] — three-state (absent/null/value) for PATCH API — unique in Go ecosystem
  • Functional APIMap, FlatMap, Equal
  • zero/ subpackage — alternative semantics (zero value = null)
  • Zero-allocation unmarshal, Bool marshal <1ns
  • encoding/json/v2 compatible
  • Stdlib only — zero external dependencies

Install

go get github.com/coregx/opt@v0.1.0

Quick Start

name := opt.StringFrom("Alice")
age := opt.NewInt(0, false) // null

fmt.Println(name.Or("unknown")) // "Alice"
fmt.Println(age.OrZero())       // 0

data, _ := json.Marshal(struct {
    Name opt.String `json:"name"`
    Age  opt.Int    `json:"age"`
}{name, age})
// {"name":"Alice","age":null}

Full documentation: README | pkg.go.dev