config

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package config provides configuration parsing for sql-http-proxy.

Index

Constants

This section is empty.

Variables

View Source
var DefaultAcceptTypes = AcceptTypes{AcceptJSON, AcceptForm}

DefaultAcceptTypes is the default list when accepts is not specified.

Functions

This section is empty.

Types

type AcceptType

type AcceptType string

AcceptType represents accepted content types.

const (
	AcceptJSON AcceptType = "json"
	AcceptForm AcceptType = "form"
)

type AcceptTypes

type AcceptTypes []AcceptType

AcceptTypes represents a list of accepted content types. Can be unmarshaled from either a string or array of strings.

func (*AcceptTypes) UnmarshalYAML

func (a *AcceptTypes) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements custom YAML unmarshaling for AcceptTypes. Accepts either a string or an array of strings.

type CORSConfig

type CORSConfig struct {
	AllowedOrigins   []string `yaml:"allowed_origins,omitempty"`
	AllowCredentials bool     `yaml:"allow_credentials,omitempty"`
	MaxAge           int      `yaml:"max_age,omitempty"`
}

CORSConfig represents CORS configuration. Can be unmarshaled from either a boolean or an object.

func (*CORSConfig) IsPermissive

func (c *CORSConfig) IsPermissive() bool

IsPermissive returns true if CORS allows all origins.

func (*CORSConfig) UnmarshalYAML

func (c *CORSConfig) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements custom YAML unmarshaling for CORSConfig. Accepts either a boolean (true = permissive) or an object.

type CSVConfig

type CSVConfig struct {
	ValueParser string `yaml:"value_parser,omitempty"`
}

CSVConfig defines global CSV parsing options.

type Config

type Config struct {
	GlobalHelpers *GlobalHelpers  `yaml:"global_helpers,omitempty"`
	CSV           *CSVConfig      `yaml:"csv,omitempty"`
	Database      *DatabaseConfig `yaml:"database,omitempty"`
	HTTP          *HTTPConfig     `yaml:"http,omitempty"`
	Queries       []Query         `yaml:"queries"`
	Mutations     []Mutation      `yaml:"mutations"`
}

Config represents the application configuration.

func Parse

func Parse(data []byte) (Config, error)

Parse parses configuration from YAML bytes.

func ParseFile

func ParseFile(filename string) (Config, error)

ParseFile parses configuration from a YAML file.

func (*Config) CORSEnabled

func (cfg *Config) CORSEnabled() bool

CORSEnabled returns true if CORS is configured.

func (*Config) DSN

func (cfg *Config) DSN() string

DSN returns the database connection string.

func (*Config) Driver

func (cfg *Config) Driver() (string, error)

Driver returns the database driver name from the DSN.

func (*Config) GetCORS

func (cfg *Config) GetCORS() *CORSConfig

GetCORS returns the CORS configuration or nil if not enabled.

func (*Config) RequiresDB

func (cfg *Config) RequiresDB() bool

RequiresDB returns true if any query or mutation requires a database connection.

func (*Config) ValidateTransforms

func (cfg *Config) ValidateTransforms() error

ValidateTransforms validates all transforms and mocks are valid. Returns all errors joined, not just the first one.

type DatabaseConfig

type DatabaseConfig struct {
	DSN  string        `yaml:"dsn"`
	Init *DatabaseInit `yaml:"init,omitempty"`
}

DatabaseConfig represents database connection configuration.

type DatabaseInit

type DatabaseInit struct {
	SQL      string   `yaml:"sql,omitempty"`
	SQLFiles []string `yaml:"sql_files,omitempty"`
}

DatabaseInit defines SQL to execute on database connection. Can be a simple string (sql code) or an object with sql and/or sql_files.

func (*DatabaseInit) UnmarshalYAML

func (d *DatabaseInit) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements custom YAML unmarshaling for DatabaseInit. Accepts either a string (shorthand for sql:) or an object.

type EntityType

type EntityType string

EntityType represents the entity type (query or mutation).

const (
	// EntityQuery represents a query operation.
	EntityQuery EntityType = "query"
	// EntityMutation represents a mutation operation.
	EntityMutation EntityType = "mutation"
)

type GlobalHelpers

type GlobalHelpers struct {
	JS      string   `yaml:"js,omitempty"`
	JSFiles []string `yaml:"js_files,omitempty"`
}

GlobalHelpers defines global JavaScript helpers available in all JS contexts. Can be a simple string (js code) or an object with js and/or js_files.

func (*GlobalHelpers) UnmarshalYAML

func (g *GlobalHelpers) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements custom YAML unmarshaling for GlobalHelpers. Accepts either a string (shorthand for js:) or an object.

type HTTPConfig

type HTTPConfig struct {
	CORS *CORSConfig `yaml:"cors,omitempty"`
}

HTTPConfig represents HTTP server configuration.

type Mock

type Mock struct {
	// Enabled is set to true when mock is specified as `mock: true` (for type: none)
	Enabled bool `yaml:"-"`

	// Object sources (for type: one - returns single object directly)
	Object         map[string]any `yaml:"object,omitempty"`
	ObjectJSON     string         `yaml:"object_json,omitempty"`
	ObjectJSONFile string         `yaml:"object_json_file,omitempty"`
	ObjectJS       string         `yaml:"object_js,omitempty"`

	// Array sources (for type: many, or type: one with filter)
	Array         []any  `yaml:"array,omitempty"`
	ArrayJSON     string `yaml:"array_json,omitempty"`
	ArrayJSONFile string `yaml:"array_json_file,omitempty"`
	ArrayJS       string `yaml:"array_js,omitempty"`
	CSV           string `yaml:"csv,omitempty"`
	CSVFile       string `yaml:"csv_file,omitempty"`
	JSONL         string `yaml:"jsonl,omitempty"`
	JSONLFile     string `yaml:"jsonl_file,omitempty"`

	// Filter for array sources (JS code: receives row, input params, ctx free var; returns boolean)
	// Required for type: one with array sources, optional for type: many
	Filter string `yaml:"filter,omitempty"`
}

Mock represents mock data source configuration. Contains all possible data source fields for both type: one and type: many. Only one data source field should be set (mutually exclusive). For type: none, mock can be just `true` (marker for mock mode).

func (*Mock) GetJS

func (m *Mock) GetJS() string

GetJS returns the JavaScript code if present.

func (*Mock) IsArraySource

func (m *Mock) IsArraySource() bool

IsArraySource returns true if this mock uses an array source.

func (*Mock) IsEmpty

func (m *Mock) IsEmpty() bool

IsEmpty returns true if no mock source is configured.

func (*Mock) IsObjectSource

func (m *Mock) IsObjectSource() bool

IsObjectSource returns true if this mock uses an object source.

func (*Mock) UnmarshalYAML

func (m *Mock) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements custom YAML unmarshaling for Mock. Accepts either a boolean (for type: none) or an object (for type: one/many).

func (*Mock) Validate

func (m *Mock) Validate() error

Validate checks that only one mock source is specified. Does NOT check filter requirement - that depends on query type and is validated elsewhere.

func (*Mock) ValidateForTypeMany

func (m *Mock) ValidateForTypeMany() error

ValidateForTypeMany validates mock configuration for type: many. Object sources are not allowed.

func (*Mock) ValidateForTypeOne

func (m *Mock) ValidateForTypeOne() error

ValidateForTypeOne validates mock configuration for type: one. Array sources require filter.

type Mutation

type Mutation struct {
	Type      MutationType `yaml:"type"`
	Method    string       `yaml:"method,omitempty"` // HTTP method (default: POST)
	Path      string       `yaml:"path"`
	SQL       string       `yaml:"sql,omitempty"`
	Mock      *Mock        `yaml:"mock,omitempty"`    // Mock data source (alternative to SQL)
	Accepts   *AcceptTypes `yaml:"accepts,omitempty"` // Accepted content types for body (nil: default, empty: none)
	Transform *Transform   `yaml:"transform,omitempty"`
	Delay     string       `yaml:"delay,omitempty"` // Artificial delay before response (e.g., "100ms", "1s")
}

Mutation represents a single mutation configuration.

func (Mutation) GetAccepts

func (m Mutation) GetAccepts() AcceptTypes

GetAccepts returns the accepted content types. Returns default [json, form] if not specified, or empty slice if explicitly set to [].

func (Mutation) GetMethod

func (m Mutation) GetMethod() string

GetMethod returns the HTTP method for the mutation (default: POST).

func (Mutation) HasMock

func (m Mutation) HasMock() bool

HasMock returns true if this mutation uses mock data.

type MutationType

type MutationType string

MutationType represents the type of mutation result.

const (
	// MutationTypeOne returns a single row (via RETURNING).
	MutationTypeOne MutationType = "one"
	// MutationTypeMany returns multiple rows (via RETURNING).
	MutationTypeMany MutationType = "many"
	// MutationTypeNone returns no content (204).
	MutationTypeNone MutationType = "none"
)

type OpType

type OpType string

OpType represents the operation type for result handling.

const (
	// OpTypeOne returns a single row.
	OpTypeOne OpType = "one"
	// OpTypeMany returns multiple rows.
	OpTypeMany OpType = "many"
	// OpTypeNone returns no content (mutations only).
	OpTypeNone OpType = "none"
)

type PostTransform

type PostTransform struct {
	Each string `yaml:"each,omitempty"` // Transform each row individually
	All  string `yaml:"all,omitempty"`  // Transform entire result array (default for many)
}

PostTransform represents post-transform configuration. Can be a simple string or an object with each/all.

func (*PostTransform) UnmarshalYAML

func (p *PostTransform) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements custom YAML unmarshaling for PostTransform. Accepts either a string (defaults to All) or an object with each/all.

type Query

type Query struct {
	Type           QueryType    `yaml:"type"`
	Method         string       `yaml:"method,omitempty"` // HTTP method (default: GET)
	Path           string       `yaml:"path"`
	SQL            string       `yaml:"sql,omitempty"`
	Mock           *Mock        `yaml:"mock,omitempty"`             // Mock data source (alternative to SQL)
	Accepts        *AcceptTypes `yaml:"accepts,omitempty"`          // Accepted content types for body (nil: default, empty: none)
	HandleNotFound bool         `yaml:"handle_not_found,omitempty"` // Pass null to post-transform instead of 404 (type: one only)
	Transform      *Transform   `yaml:"transform,omitempty"`
	Delay          string       `yaml:"delay,omitempty"` // Artificial delay before response (e.g., "100ms", "1s")
}

Query represents a single query configuration.

func (Query) GetAccepts

func (q Query) GetAccepts() AcceptTypes

GetAccepts returns the accepted content types. Returns default [json, form] if not specified, or empty slice if explicitly set to [].

func (Query) GetMethod

func (q Query) GetMethod() string

GetMethod returns the HTTP method for the query (default: GET).

func (Query) HasMock

func (q Query) HasMock() bool

HasMock returns true if this query uses mock data.

type QueryType

type QueryType string

QueryType represents the type of query result.

const (
	// QueryTypeOne returns a single row.
	QueryTypeOne QueryType = "one"
	// QueryTypeMany returns multiple rows.
	QueryTypeMany QueryType = "many"
)

type Transform

type Transform struct {
	Pre  string        `yaml:"pre,omitempty"`
	Post PostTransform `yaml:"post,omitempty"`
}

Transform defines pre/post JavaScript transformations. Note: mock is now at query/mutation level, not inside transform.

Jump to

Keyboard shortcuts

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