Documentation
¶
Overview ¶
Package config provides configuration parsing for sql-http-proxy.
Index ¶
- Variables
- type AcceptType
- type AcceptTypes
- type CORSConfig
- type CSVConfig
- type Config
- type DatabaseConfig
- type DatabaseInit
- type EntityType
- type GlobalHelpers
- type HTTPConfig
- type Mock
- func (m *Mock) GetJS() string
- func (m *Mock) IsArraySource() bool
- func (m *Mock) IsEmpty() bool
- func (m *Mock) IsObjectSource() bool
- func (m *Mock) UnmarshalYAML(value *yaml.Node) error
- func (m *Mock) Validate() error
- func (m *Mock) ValidateForTypeMany() error
- func (m *Mock) ValidateForTypeOne() error
- type Mutation
- type MutationType
- type OpType
- type PostTransform
- type Query
- type QueryType
- type Transform
Constants ¶
This section is empty.
Variables ¶
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 (*Config) CORSEnabled ¶
CORSEnabled returns true if CORS is configured.
func (*Config) GetCORS ¶
func (cfg *Config) GetCORS() *CORSConfig
GetCORS returns the CORS configuration or nil if not enabled.
func (*Config) RequiresDB ¶
RequiresDB returns true if any query or mutation requires a database connection.
func (*Config) ValidateTransforms ¶
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) IsArraySource ¶
IsArraySource returns true if this mock uses an array source.
func (*Mock) IsObjectSource ¶
IsObjectSource returns true if this mock uses an object source.
func (*Mock) UnmarshalYAML ¶
UnmarshalYAML implements custom YAML unmarshaling for Mock. Accepts either a boolean (for type: none) or an object (for type: one/many).
func (*Mock) Validate ¶
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 ¶
ValidateForTypeMany validates mock configuration for type: many. Object sources are not allowed.
func (*Mock) ValidateForTypeOne ¶
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 [].
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 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 [].
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.