schemahcl

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2022 License: Apache-2.0 Imports: 24 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Marshal = MarshalerFunc(New().MarshalSpec)

Marshal returns the Atlas HCL encoding of v.

Functions

func RawExprValue added in v0.8.2

func RawExprValue(x *RawExpr) cty.Value

RawExprValue is a helper method for constructing a cty.Value that capsules a raw expression.

func Register

func Register(name string, ext any)

Register records the type of ext in the global extension registry. If Register is called twice with the same name or if ext is nil, it panics.

Types

type Attr

type Attr struct {
	K string
	V cty.Value
}

Attr is an attribute of a Resource.

func BoolAttr added in v0.8.2

func BoolAttr(k string, v bool) *Attr

BoolAttr is a helper method for constructing *schemahcl.Attr instances that contain a boolean value.

func Int64Attr added in v0.8.2

func Int64Attr(k string, v int64) *Attr

Int64Attr is a helper method for constructing *schemahcl.Attr instances that contain int64 value.

func IntAttr added in v0.8.2

func IntAttr(k string, v int) *Attr

IntAttr is a helper method for constructing *schemahcl.Attr instances that contain int64 value.

func RawAttr added in v0.8.2

func RawAttr(k string, x string) *Attr

RawAttr is a helper method for constructing *schemahcl.Attr instances that contain RawExpr value.

func RefAttr added in v0.8.2

func RefAttr(k string, v *Ref) *Attr

RefAttr is a helper method for constructing *schemahcl.Attr instances that contain a Ref value.

func RefsAttr added in v0.8.2

func RefsAttr(k string, refs ...*Ref) *Attr

RefsAttr is a helper method for constructing *schemahcl.Attr instances that contain list references.

func StringAttr added in v0.8.2

func StringAttr(k string, v string) *Attr

StringAttr is a helper method for constructing *schemahcl.Attr instances that contain string value.

func StringsAttr added in v0.8.2

func StringsAttr(k string, vs ...string) *Attr

StringsAttr is a helper method for constructing *schemahcl.Attr instances that contain list strings.

func (*Attr) Bool

func (a *Attr) Bool() (b bool, err error)

Bool returns a boolean from the Value of the Attr. If The value is not a LiteralValue or the value cannot be converted to a boolean an error is returned.

func (*Attr) Bools

func (a *Attr) Bools() (vs []bool, err error)

Bools returns a slice of bools from the Value of the Attr. If The value is not a ListValue or its values cannot be converted to bools an error is returned.

func (*Attr) Int

func (a *Attr) Int() (int, error)

Int returns an int from the Value of the Attr. If The value is not a LiteralValue or the value cannot be converted to an integer an error is returned.

func (*Attr) Int64

func (a *Attr) Int64() (i int64, err error)

Int64 returns an int64 from the Value of the Attr. If The value is not a LiteralValue or the value cannot be converted to an integer an error is returned.

func (*Attr) IsRawExpr added in v0.8.2

func (a *Attr) IsRawExpr() bool

IsRawExpr indicates if the attribute is a RawExpr type.

func (*Attr) IsRef added in v0.8.2

func (a *Attr) IsRef() bool

IsRef indicates if the attribute is a reference type.

func (*Attr) IsType added in v0.8.2

func (a *Attr) IsType() bool

IsType indicates if the attribute is a type spec.

func (*Attr) RawExpr added in v0.8.2

func (a *Attr) RawExpr() (*RawExpr, error)

RawExpr extracts the RawExpr from the Attr.

func (*Attr) Ref

func (a *Attr) Ref() (string, error)

Ref extracts the reference from the Value of the Attr.

func (*Attr) Refs added in v0.6.5

func (a *Attr) Refs() ([]*Ref, error)

Refs returns a slice of references.

func (*Attr) String

func (a *Attr) String() (s string, err error)

String returns a string from the Value of the Attr. If The value is not a LiteralValue an error is returned. String values are expected to be quoted. If the value is not properly quoted an error is returned.

func (*Attr) Strings

func (a *Attr) Strings() (vs []string, err error)

Strings returns a slice of strings from the Value of the Attr. If The value is not a ListValue or its values cannot be converted to strings an error is returned.

func (*Attr) Type added in v0.8.2

func (a *Attr) Type() (*Type, error)

Type extracts the Type from the Attr.

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config configures an unmarshaling.

type DefaultExtension

type DefaultExtension struct {
	Extra Resource
}

DefaultExtension can be embedded in structs that need basic default behavior. For instance, DefaultExtension implements Remainer, and has a private *Resource field that can store additional attributes and children that do not match the structs fields.

func (*DefaultExtension) Attr

func (d *DefaultExtension) Attr(name string) (*Attr, bool)

Attr returns the Attr by the provided name and reports whether it was found.

func (*DefaultExtension) Remain

func (d *DefaultExtension) Remain() *Resource

Remain implements the Remainer interface.

type EvalFunc

type EvalFunc func(*hclparse.Parser, any, map[string]cty.Value) error

EvalFunc is an adapter that allows the use of an ordinary function as an Evaluator.

func (EvalFunc) Eval

func (f EvalFunc) Eval(p *hclparse.Parser, i any, input map[string]cty.Value) error

Eval implements the Evaluator interface.

type Evaluator

type Evaluator interface {
	// Eval evaluates parsed HCL files using input variables into a schema.Realm.
	Eval(*hclparse.Parser, any, map[string]cty.Value) error
}

Evaluator is the interface that wraps the Eval function.

type Marshaler

type Marshaler interface {
	// MarshalSpec marshals the provided input into a valid Atlas HCL document.
	MarshalSpec(any) ([]byte, error)
}

Marshaler is the interface that wraps the MarshalSpec function.

type MarshalerFunc

type MarshalerFunc func(any) ([]byte, error)

MarshalerFunc is the function type that is implemented by the MarshalSpec method of the Marshaler interface.

func (MarshalerFunc) MarshalSpec

func (f MarshalerFunc) MarshalSpec(v any) ([]byte, error)

MarshalSpec implements Marshaler.

type Option

type Option func(*Config)

Option configures a Config.

func WithDataSource added in v0.8.0

func WithDataSource(name string, h func(*hcl.EvalContext, *hclsyntax.Block) (cty.Value, error)) Option

WithDataSource registers a data source name and its corresponding handler. e.g., the example below registers a data source named "text" that returns the string defined in the data source block.

WithDataSource("text", func(ctx *hcl.EvalContext, b *hclsyntax.Block) (cty.Value, hcl.Diagnostics) {
	attrs, diags := b.Body.JustAttributes()
	if diags.HasErrors() {
		return cty.NilVal, diags
	}
	v, diags := attrs["value"].Expr.Value(ctx)
	if diags.HasErrors() {
		return cty.NilVal, diags
	}
	return cty.ObjectVal(map[string]cty.Value{"output": v}), nil
}),

data "text" "hello" {
  value = "hello world"
}

func WithScopedEnums

func WithScopedEnums(path string, enums ...string) Option

WithScopedEnums configured a list of allowed ENUMs to be used in the given context, block or attribute. For example, the following option allows setting HASH or BTREE to the "using" attribute in "index" block.

WithScopedEnums("table.index.type", "HASH", "BTREE")

table "t" {
	...
	index "i" {
		type = HASH     // Allowed.
		type = INVALID  // Not Allowed.
	}
}

func WithTypes

func WithTypes(typeSpecs []*TypeSpec) Option

WithTypes configures the list of given types as identifiers in the unmarshaling context.

type RawExpr

type RawExpr struct {
	X string
}

RawExpr implements Value and represents any raw expression.

type Ref

type Ref struct {
	V string
}

Ref implements Value and represents a reference to another Resource. The path to a Resource under the root Resource is expressed as "$<type>.<name>..." recursively. For example, a resource of type "table" that is named "users" and is a direct child of the root Resource's address shall be "$table.users". A child resource of that table of type "column" and named "id", shall be referenced as "$table.users.$column.id", and so on.

type Remainer

type Remainer interface {
	// Remain returns a resource representing any extra children and attributes
	// that are related to the struct but were not mapped to any of its fields.
	Remain() *Resource
}

Remainer is the interface that is implemented by types that can store additional attributes and children resources.

type Resource

type Resource struct {
	Name      string
	Qualifier string
	Type      string
	Attrs     []*Attr
	Children  []*Resource
}

Resource is a generic container for resources described in configurations.

func (*Resource) As

func (r *Resource) As(target any) error

As reads the attributes and children resources of the resource into the target struct.

func (*Resource) Attr

func (r *Resource) Attr(name string) (*Attr, bool)

Attr returns the Attr by the provided name and reports whether it was found.

func (*Resource) FinalName

func (r *Resource) FinalName() (string, error)

FinalName returns the final name for the resource by examining the struct tags for the extension of the Resource's type. If no such extension is registered or the extension struct does not have a name field, an error is returned.

func (*Resource) Resource

func (r *Resource) Resource(t string) (*Resource, bool)

Resource returns the first child Resource by its type and reports whether it was found.

func (*Resource) Scan

func (r *Resource) Scan(ext any) error

Scan reads the Extension into the Resource. Scan will override the Resource name or type if they are set for the extension.

func (*Resource) SetAttr

func (r *Resource) SetAttr(attr *Attr)

SetAttr sets the Attr on the Resource. If r is nil, a zero value Resource is initialized. If an Attr with the same key exists, it is replaced by attr.

type State

type State struct {
	// contains filtered or unexported fields
}

State is used to evaluate and marshal Atlas HCL documents and stores a configuration for these operations.

func New

func New(opts ...Option) *State

New returns a State configured with options.

func (*State) Eval

func (s *State) Eval(parsed *hclparse.Parser, v any, input map[string]cty.Value) error

Eval evaluates the parsed HCL documents using the input variables and populates v using the result.

func (*State) EvalBytes

func (s *State) EvalBytes(data []byte, v any, input map[string]cty.Value) error

EvalBytes evaluates the data byte-slice as an Atlas HCL document using the input variables and stores the result in v.

func (*State) EvalFiles

func (s *State) EvalFiles(paths []string, v any, input map[string]cty.Value) error

EvalFiles evaluates the files in the provided paths using the input variables and populates v with the result.

func (*State) MarshalSpec

func (s *State) MarshalSpec(v any) ([]byte, error)

MarshalSpec implements Marshaler for Atlas HCL documents.

type Type

type Type struct {
	T     string
	Attrs []*Attr
	IsRef bool
}

Type represents the type of the field in a schema.

type TypeAttr

type TypeAttr struct {
	// Name should be a snake_case of related the schema.Type struct field.
	Name     string
	Kind     reflect.Kind
	Required bool
}

TypeAttr describes an attribute of a TypeSpec, for example `varchar` fields can have a `size` attribute.

func SizeTypeAttr

func SizeTypeAttr(required bool) *TypeAttr

SizeTypeAttr returns a TypeAttr for a size attribute.

type TypeRegistry

type TypeRegistry struct {
	// contains filtered or unexported fields
}

TypeRegistry is a collection of *schemahcl.TypeSpec.

func NewRegistry

func NewRegistry(opts ...TypeRegistryOption) *TypeRegistry

NewRegistry creates a new *TypeRegistry, registers the provided types and panics if an error occurs.

func (*TypeRegistry) Convert

func (r *TypeRegistry) Convert(typ schema.Type) (*Type, error)

Convert converts the schema.Type to a *schemahcl.Type.

func (*TypeRegistry) PrintType

func (r *TypeRegistry) PrintType(typ *Type) (string, error)

PrintType returns the string representation of a column type which can be parsed by the driver into a schema.Type.

func (*TypeRegistry) Register

func (r *TypeRegistry) Register(specs ...*TypeSpec) error

Register adds one or more TypeSpec to the registry.

func (*TypeRegistry) Specs

func (r *TypeRegistry) Specs() []*TypeSpec

Specs returns the TypeSpecs in the registry.

func (*TypeRegistry) Type

func (r *TypeRegistry) Type(typ *Type, extra []*Attr) (schema.Type, error)

Type converts a *schemahcl.Type into a schema.Type.

type TypeRegistryOption

type TypeRegistryOption func(*TypeRegistry) error

TypeRegistryOption configures a TypeRegistry.

func WithFormatter

func WithFormatter(f func(schema.Type) (string, error)) TypeRegistryOption

WithFormatter configures the registry to use a formatting function for printing schema.Type as string.

func WithParser

func WithParser(parser func(string) (schema.Type, error)) TypeRegistryOption

WithParser configures the registry to use a parsing function for converting a string to a schema.Type.

func WithSpecFunc

func WithSpecFunc(spec func(schema.Type) (*Type, error)) TypeRegistryOption

WithSpecFunc configures the registry to use the given function for converting a schema.Type to schemahcl.Type

func WithSpecs

func WithSpecs(specs ...*TypeSpec) TypeRegistryOption

WithSpecs configures the registry to register the given list of type specs.

type TypeSpec

type TypeSpec struct {
	// Name is the identifier for the type in an Atlas DDL document.
	Name string

	// T is the database identifier for the type.
	T          string
	Attributes []*TypeAttr

	// RType is the reflect.Type of the schema.Type used to describe the TypeSpec.
	// This field is optional and used to determine the TypeSpec in cases where the
	// schema.Type does not have a `T` field.
	RType reflect.Type

	// Format is an optional formatting function.
	// If exists, it will be used instead the registry one.
	Format func(*Type) (string, error)

	// FromSpec is an optional function that can be attached
	// to the type spec and allows converting the schema spec
	// type to a schema type (from document to databse).
	FromSpec func(*Type) (schema.Type, error)

	// ToSpec is an optional function that can be attached
	// to the type spec and allows converting the schema type
	// to a schema spec type (from database to document).
	ToSpec func(schema.Type) (*Type, error)
}

TypeSpec represents a specification for defining a Type.

func AliasTypeSpec

func AliasTypeSpec(name, dbType string, opts ...TypeSpecOption) *TypeSpec

AliasTypeSpec returns a TypeSpec with the provided name.

func NewTypeSpec

func NewTypeSpec(name string, opts ...TypeSpecOption) *TypeSpec

NewTypeSpec returns a TypeSpec with the provided name.

func (*TypeSpec) Attr

func (s *TypeSpec) Attr(name string) (*TypeAttr, bool)

Attr returns a TypeAttr by name and reports if one was found.

type TypeSpecOption

type TypeSpecOption func(*TypeSpec)

TypeSpecOption configures a schemahcl.TypeSpec.

func WithAttributes

func WithAttributes(attrs ...*TypeAttr) TypeSpecOption

WithAttributes returns an attributes TypeSpecOption.

func WithFromSpec

func WithFromSpec(f func(*Type) (schema.Type, error)) TypeSpecOption

WithFromSpec allows configuring the FromSpec convert function using functional options.

func WithToSpec

func WithToSpec(f func(schema.Type) (*Type, error)) TypeSpecOption

WithToSpec allows configuring the ToSpec convert function using functional options.

func WithTypeFormatter

func WithTypeFormatter(f func(*Type) (string, error)) TypeSpecOption

WithTypeFormatter allows overriding the Format function for the Type.

Jump to

Keyboard shortcuts

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