namer

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package namer provides the Namer interface for naming dependencies in braider's dependency injection system.

Implementations of Namer are used with inject.Named[N] and provide.Named[N] to register dependencies under specific names, enabling multiple instances of the same type to be distinguished by name.

The braider analyzer validates at analysis time that Name() returns a hardcoded string literal. Computed values, concatenations, variable references, and function calls are rejected with a diagnostic error.

Example implementation:

type PrimaryDBName struct{}

func (PrimaryDBName) Name() string { return "primaryDB" }

Usage with inject.Named:

type PrimaryDB struct {
    annotation.Injectable[inject.Named[PrimaryDBName]]
}

Usage with provide.Named:

var _ = annotation.Provide[provide.Named[PrimaryDBName]](NewPrimaryDB)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Namer

type Namer interface {
	Name() string
}

Namer provides a Name method for naming dependencies.

The Name method must return a hardcoded string literal in its return statement. The braider analyzer performs AST analysis on the Name() method body to verify this requirement. Non-literal return values (such as concatenations, variables, or function calls) will cause a diagnostic error during analysis.

Example:

// Valid: returns a hardcoded string literal
type PrimaryDBName struct{}
func (PrimaryDBName) Name() string { return "primaryDB" }

// Invalid: computed value (will cause analyzer error)
type BadName struct{ prefix string }
func (b BadName) Name() string { return b.prefix + "DB" }
Example

ExampleNamer demonstrates implementing the Namer interface. The Name() method must return a hardcoded string literal. The braider analyzer validates this at analysis time and rejects computed values, concatenations, or variable references.

package main

import (
	"fmt"

	"github.com/miyamo2/braider/pkg/annotation/namer"
)

// ExampleNamer demonstrates implementing the Namer interface.
// The Name() method must return a hardcoded string literal.
// The braider analyzer validates this at analysis time and rejects
// computed values, concatenations, or variable references.
func main() {
	type PrimaryDBName struct{}
	// Name returns a hardcoded string literal.
	// This is the only pattern accepted by the braider analyzer.
	// func (PrimaryDBName) Name() string { return "primaryDB" }

	// Valid: hardcoded string literal
	n := primaryDBNamer{}
	fmt.Println(n.Name())
}

// primaryDBNamer is a valid Namer implementation for examples.
type primaryDBNamer struct{}

func (primaryDBNamer) Name() string { return "primaryDB" }

// Compile-time assertion that primaryDBNamer implements namer.Namer.
var _ namer.Namer = primaryDBNamer{}
Output:

primaryDB

Jump to

Keyboard shortcuts

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