ref

package
v1.4.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: GPL-3.0 Imports: 0 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ref provides utility functions for creating references to values.

Overview

The ref package contains helper functions to create pointers to values, which is essential for dsco's pointer-based configuration system. This eliminates the need for intermediate variables when creating configuration structures with explicit pointer fields.

Primary Function

The main utility function is R, which returns a pointer to any value:

// Instead of this verbose approach:
host := "localhost"
port := 8080
config := &Config{
	Host: &host,
	Port: &port,
}

// Use the convenient R function:
config := &Config{
	Host: ref.R("localhost"),
	Port: ref.R(8080),
}

Generic Support

The R function uses Go generics to work with any type, providing type safety while maintaining convenience:

// Works with all basic types
stringPtr := ref.R("hello")        // *string
intPtr := ref.R(42)                // *int
boolPtr := ref.R(true)             // *bool
durationPtr := ref.R(time.Second)  // *time.Duration

// Works with complex types
slicePtr := ref.R([]string{"a", "b"})    // *[]string
mapPtr := ref.R(map[string]int{"x": 1})  // *map[string]int

// Works with custom structs
type Custom struct{ Value int }
customPtr := ref.R(Custom{Value: 123})   // *Custom

Zero Values

The function correctly handles zero values, creating pointers to them:

zeroInt := ref.R(0)          // *int pointing to 0
emptyString := ref.R("")     // *string pointing to ""
falseBool := ref.R(false)    // *bool pointing to false

This is important for dsco's explicit configuration model, where zero values must be distinguishable from nil (unconfigured) values.

Testing Coverage

This package maintains 100% test coverage, including: - All Go basic types (string, int, bool, float64) - Zero value handling for each type - Complex types (structs, slices, maps) - Generic type parameter validation - Memory safety verification

The tests ensure the function works correctly across all supported Go types and maintains the expected pointer relationships.

Usage in dsco

This package is primarily used within dsco configuration structs:

type ServiceConfig struct {
	Database *DatabaseConfig `yaml:"database"`
	Server   *ServerConfig   `yaml:"server"`
}

type DatabaseConfig struct {
	Host     *string `yaml:"host"`
	Port     *int    `yaml:"port"`
	Timeout  *int    `yaml:"timeout"`
}

// Clean configuration creation
defaults := &ServiceConfig{
	Database: &DatabaseConfig{
		Host:    ref.R("localhost"),
		Port:    ref.R(5432),
		Timeout: ref.R(30),
	},
	Server: &ServerConfig{
		Port:    ref.R(8080),
		Timeout: ref.R(60),
	},
}

The ref.R function is aliased as dsco.R for convenience in the main package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func R

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

R returns a reference to any value thanks to generics in go1.18.

Types

This section is empty.

Jump to

Keyboard shortcuts

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