core

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultErrorTypePrefix = "urn:hclapi:error:"

DefaultErrorTypePrefix is the standard URN namespace prefix for built-in error types.

Variables

This section is empty.

Functions

func DefaultErrorHandler

func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, problem ProblemDetailsError)

DefaultErrorHandler returns a ProblemDetailsError with default values.

func ProblemType

func ProblemType(slug string) string

ProblemType returns a standard URN identifier for a given error slug.

func ResolveRelativePath

func ResolveRelativePath(raw, baseDir string) string

ResolveRelativePath resolves a relative file path or file-based DSN against baseDir.

Types

type ByteSize

type ByteSize int64

ByteSize represents a quantity of bytes that can be unmarshaled from text.

const (
	B   ByteSize = 1
	KB  ByteSize = 1000 * B
	KiB ByteSize = 1024 * B
	MB  ByteSize = 1000 * KB
	MiB ByteSize = 1024 * KiB
	GB  ByteSize = 1000 * MB
	GiB ByteSize = 1024 * MiB
	TB  ByteSize = 1000 * GB
	TiB ByteSize = 1024 * GiB
)

ByteSize constants represent byte sizes.

func ParseByteSize

func ParseByteSize(s string) (ByteSize, error)

ParseByteSize parses human-readable byte strings into a ByteSize.

func (ByteSize) Bytes

func (b ByteSize) Bytes() int64

Bytes returns the byte size as an integer.

func (ByteSize) MarshalText

func (b ByteSize) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (ByteSize) String

func (b ByteSize) String() string

String returns the byte size as a human-readable string.

func (*ByteSize) UnmarshalText

func (b *ByteSize) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Connection

type Connection struct {
	Driver string
	Name   string
	URL    string
	Pool   PoolConfig
}

Connection represents a resolved connection configuration with driver metadata and pool limits.

func (Connection) Key

func (c Connection) Key() string

Key returns the unique identifier for the connection pool (e.g. "postgres.main").

func (Connection) Reference

func (c Connection) Reference() string

Reference returns the full HCL reference path (e.g. "connection.postgres.main").

type Context

type Context struct {
	Request        *RequestState             `json:"request"`
	Steps          map[string]map[string]any `json:"steps"`
	TimestampEpoch int64                     `json:"timestamp_epoch"`
	IngressTime    time.Time                 `json:"-"`
	Server         Server                    `json:"-"`
	RawRequest     *http.Request             `json:"-"`
}

Context encapsulates the runtime execution state for a single HTTP request lifecycle.

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request, opts ...ContextOption) (*Context, error)

NewContext parses the HTTP request, enforcing max body size and decoding payloads.

func (*Context) Context

func (c *Context) Context() context.Context

Context returns the underlying request context, or context.Background() if RawRequest is nil.

func (*Context) WithContext

func (c *Context) WithContext(ctx context.Context) *Context

WithContext returns a shallow copy of Context with an updated underlying request context.

type ContextOption

type ContextOption func(*contextConfig)

ContextOption configures optional behavior during Context creation.

func WithPathParams

func WithPathParams(paramNames []string) ContextOption

WithPathParams configures route parameter names to extract from the request.

func WithServer

func WithServer(server Server) ContextOption

WithServer attaches the resolved server configuration to the request context.

type Duration

type Duration time.Duration

Duration wraps a time.Duration with universal text deserialization.

func (Duration) Duration

func (d Duration) Duration() time.Duration

Duration returns the duration as a time.Duration.

func (Duration) MarshalText

func (d Duration) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Duration) String

func (d Duration) String() string

String returns the duration as a human-readable string.

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type ErrorHandler

type ErrorHandler func(w http.ResponseWriter, r *http.Request, problem ProblemDetailsError)

ErrorHandler defines the contract for customizing API error serialization.

type Field

type Field struct {
	Name        string
	Type        string // "string", "int", "float", "bool", "any", "list(string)", etc.
	Required    bool
	Default     any // Evaluated static default or nil
	Description string
	Enum        []any  // Evaluated allowed values or nil
	Format      string // "email", "uuid", "date-time", etc.
	Pattern     string // Regex pattern or empty
	MinLength   *int
	MaxLength   *int
	Min         *float64
	Max         *float64
	MinItems    *int
	MaxItems    *int
	UniqueItems bool
}

Field represents a compiled, type-safe schema field rule.

type InvalidParam

type InvalidParam struct {
	Name   string `json:"name"`
	Reason string `json:"reason"`
}

InvalidParam represents a single field validation failure.

type OpenAPIConfig

type OpenAPIConfig struct {
	Title       string
	Version     string
	Description string
	Servers     []OpenAPIServer
	Tags        []OpenAPITag
	Contact     *OpenAPIContact
	License     *OpenAPILicense
}

OpenAPIConfig holds global OpenAPI 3.1 document header metadata.

type OpenAPIContact

type OpenAPIContact struct {
	Name  string
	Email string
	URL   string
}

OpenAPIContact defines API contact details.

type OpenAPILicense

type OpenAPILicense struct {
	Name string
	URL  string
}

OpenAPILicense defines API licensing information.

type OpenAPIServer

type OpenAPIServer struct {
	URL         string
	Description string
}

OpenAPIServer defines a target server deployment in the OpenAPI spec.

type OpenAPITag

type OpenAPITag struct {
	Name        string
	Description string
}

OpenAPITag defines an operation category tag.

type Options

type Options struct {
	// ConfigPath is a file or directory of .hcl definitions.
	ConfigPath string

	// StrictTyping enforces request schema validation on all endpoints.
	StrictTyping bool

	// ErrorHandler formats error responses. If nil, RFC 9457 defaults are used.
	ErrorHandler ErrorHandler

	// Logger receives operational logs. If nil, logging is discarded.
	Logger *slog.Logger
}

Options defines the configuration options for the hclapi engine.

type PoolConfig

type PoolConfig struct {
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime Duration
	IdleTimeout     Duration
	Size            int
}

PoolConfig defines connection pool sizing and lifecycle settings.

func DefaultPoolConfig

func DefaultPoolConfig() PoolConfig

DefaultPoolConfig returns baseline production connection pool settings.

type ProblemDetailsError

type ProblemDetailsError struct {
	Type          string         `json:"type,omitempty"`
	Title         string         `json:"title"`
	Status        int            `json:"status"`
	Detail        string         `json:"detail,omitempty"`
	Instance      string         `json:"instance,omitempty"`
	Step          string         `json:"step,omitempty"`
	InvalidParams []InvalidParam `json:"invalid_params,omitempty"`
	Extensions    map[string]any `json:"extensions,omitempty"`
}

ProblemDetailsError represents an RFC 9457 compliant error object.

func (ProblemDetailsError) Error

func (p ProblemDetailsError) Error() string

Error implements the standard error interface.

type RequestState

type RequestState struct {
	Method  string            `json:"method"`
	Path    map[string]string `json:"path"`
	Query   map[string]string `json:"query"`
	Headers map[string]string `json:"headers"`
	Body    any               `json:"body"`
}

RequestState represents normalized HTTP request metadata extracted at runtime.

type Schema

type Schema struct {
	Name        string
	Description string
	Fields      []Field
}

Schema represents a compiled, named validation schema.

type Server

type Server struct {
	Host         string
	Port         int
	ReadTimeout  Duration
	WriteTimeout Duration
	IdleTimeout  Duration
	MaxBodySize  ByteSize
	ErrorBaseURL string // TODO: validate introducing a new block to put stuff like this
	OpenAPI      OpenAPIConfig
}

Server defines the resolved HTTP server configuration.

func DefaultServer

func DefaultServer() Server

DefaultServer returns baseline production configuration values.

func (Server) ProblemType

func (s Server) ProblemType(slug string) string

ProblemType returns the error URI using ErrorBaseURL or the default URN prefix.

func (Server) WithDefaults

func (s Server) WithDefaults() Server

WithDefaults returns a copy of Server with any zero values replaced by baseline defaults.

type StepHandler

type StepHandler func(ctx *Context, args map[string]any) (any, error)

StepHandler defines the signature for custom native Go step callbacks.

type StepResult

type StepResult = map[string]any

StepResult represents arbitrary step-specific outputs.

Jump to

Keyboard shortcuts

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