smithy

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2021 License: Apache-2.0 Imports: 13 Imported by: 1

README

smithy

A Go-based tool to work with Smithy API Specifications.

This code is extracted from SADL, and extended to handle more Smithy use cases.

The tool reads multiple files, assembles them, and then outputs the resulting model. The input files can be any mix of Smithy IDL or Smithy AST files in JSON. The default output is the "unparsing" of the assembled model to IDL into a file per namespace. Alternate generators may be specified (see usage line), notably "ast", which just dumps the model as JSON.

This work is an independent implementation of the 1.0 Smithy Specification. For more information about Smithy, its specification, and its supported tooling, see https://awslabs.github.io/smithy/.

Documentation

Overview

Decimal is a big.Float equivalent that marshals to/from JSON.

Index

Constants

View Source
const BLACK = "\033[0;0m"
View Source
const BLUE = "\033[94m"
View Source
const DecimalPrecision = uint(250)

we establish this "reasonable" max precision

View Source
const GREEN = "\033[92m"
View Source
const IndentAmount = "    "

Fix me: the unparser needs to unparse to a set of files, not a single file. One file for each namespace. When generating to stdio, these files should be concatenated, one per

View Source
const RED = "\033[0;31m"
View Source
const SmithyVersion = "1.0"
View Source
const UnspecifiedNamespace = "example"
View Source
const UnspecifiedVersion = "0.0"
View Source
const YELLOW = "\033[0;33m"

Variables

View Source
var ImportFileExtensions = map[string][]string{
	".smithy": []string{"smithy"},

	".json": []string{"smithy"},
}
View Source
var ImportFormats = []string{
	"smithy",
}
View Source
var ToolVersion string = "development version"
View Source
var Verbose bool

Functions

func AsArray

func AsArray(v interface{}) []interface{}

func AsBool

func AsBool(v interface{}) bool

func AsFloat64

func AsFloat64(v interface{}) float64

func AsInt

func AsInt(v interface{}) int

func AsInt64

func AsInt64(v interface{}) int64

func AsMap

func AsMap(v interface{}) map[string]interface{}

func AsString

func AsString(v interface{}) string

func AsStringArray

func AsStringArray(v interface{}) []string

func BaseFileName

func BaseFileName(path string) string

func Capitalize

func Capitalize(s string) string

func Debug

func Debug(args ...interface{})

func Equivalent

func Equivalent(obj1 interface{}, obj2 interface{}) bool

func FormatComment

func FormatComment(indent, prefix, comment string, maxcol int, extraPad bool) string

func FormattedAnnotation

func FormattedAnnotation(filename string, source string, prefix string, msg string, tok *Token, color string, contextSize int) string

func Get

func Get(m map[string]interface{}, key string) interface{}

func GetArray

func GetArray(m map[string]interface{}, key string) []interface{}

func GetBool

func GetBool(m map[string]interface{}, key string) bool

func GetInt

func GetInt(m map[string]interface{}, key string) int

func GetInt64

func GetInt64(m map[string]interface{}, key string) int64

func GetMap

func GetMap(m map[string]interface{}, key string) map[string]interface{}

func GetString

func GetString(m map[string]interface{}, key string) string

func GetStringArray

func GetStringArray(m map[string]interface{}, key string) []string

func IsDigit

func IsDigit(ch rune) bool

func IsLetter

func IsLetter(ch rune) bool

func IsLowercaseLetter

func IsLowercaseLetter(ch rune) bool

func IsPreludeType

func IsPreludeType(name string) bool

func IsSymbolChar

func IsSymbolChar(ch rune, first bool) bool

func IsUppercaseLetter

func IsUppercaseLetter(ch rune) bool

func IsWhitespace

func IsWhitespace(ch rune) bool

func Json

func Json(obj interface{}) string

func Kind

func Kind(v interface{}) string

func Pretty

func Pretty(obj interface{}) string

func StripNamespace

func StripNamespace(target string) string

func ToString

func ToString(obj interface{}) string

func Uncapitalize

func Uncapitalize(s string) string

Types

type AST

type AST struct {
	Smithy   string  `json:"smithy"`
	Metadata *Struct `json:"metadata,omitempty"`
	Shapes   *Shapes `json:"shapes,omitempty"`
}

func (*AST) ExternalRefs

func (ast *AST) ExternalRefs(ns string) []string

func (*AST) GetShape

func (ast *AST) GetShape(id string) *Shape

func (*AST) IDL

func (ast *AST) IDL(ns string) string

Generate Smithy IDL to describe the Smithy model for a specified namespace

func (*AST) Merge

func (ast *AST) Merge(src *AST) error

func (*AST) NamespaceAndServiceVersion

func (ast *AST) NamespaceAndServiceVersion() (string, string, string)

ASTs don't have a preferred namespac, but IDL files do. When going back to IDL, getting the preferred namespace is desirable This project's parser emits metadata for the preferred namespace, but if that is missing, we have to guess. The algorithm here is to prefer the first service's namespace, if present, or the first non-non-smithy, non-aws namespace encountered.

func (*AST) PutShape

func (ast *AST) PutShape(id string, shape *Shape)

func (*AST) Validate

func (ast *AST) Validate() error

type AstGenerator

type AstGenerator struct {
	BaseGenerator
}

func (*AstGenerator) Generate

func (gen *AstGenerator) Generate(model *Model, outdir string) error

type BaseGenerator

type BaseGenerator struct {
	Model          *Model
	OutDir         string
	ForceOverwrite bool

	Err error
	// contains filtered or unexported fields
}

func (*BaseGenerator) FileExists

func (gen *BaseGenerator) FileExists(path string) bool

func (*BaseGenerator) FileName

func (gen *BaseGenerator) FileName(ns string, suffix string) string

func (*BaseGenerator) WriteFile

func (gen *BaseGenerator) WriteFile(path string, content string) error

type Decimal

type Decimal struct {
	big.Float
}

func AsDecimal

func AsDecimal(v interface{}) *Decimal

func DecimalValue

func DecimalValue(val *Decimal, defval interface{}) *Decimal

func GetDecimal

func GetDecimal(m map[string]interface{}, key string) *Decimal

func NewDecimal

func NewDecimal(val float64) *Decimal

func ParseDecimal

func ParseDecimal(text string) (*Decimal, error)

func (*Decimal) AsBigFloat

func (d *Decimal) AsBigFloat() *big.Float

func (*Decimal) AsFloat64

func (d *Decimal) AsFloat64() float64

func (*Decimal) AsInt

func (d *Decimal) AsInt() int

func (*Decimal) AsInt32

func (d *Decimal) AsInt32() int32

func (*Decimal) AsInt64

func (d *Decimal) AsInt64() int64

func (Decimal) MarshalJSON

func (d Decimal) MarshalJSON() ([]byte, error)

Encode as a JSON number. The JSON spec allows for arbitrary precision, so this is the correct thing to do.

func (*Decimal) String

func (d *Decimal) String() string

func (*Decimal) UnmarshalJSON

func (d *Decimal) UnmarshalJSON(b []byte) error

type Generator

type Generator interface {
	Generate(model *Model, out string) error
}

type IdlGenerator

type IdlGenerator struct {
	BaseGenerator
}

func (*IdlGenerator) Generate

func (gen *IdlGenerator) Generate(model *Model, outdir string) error

type IdlWriter

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

func (*IdlWriter) Begin

func (w *IdlWriter) Begin()

func (*IdlWriter) Emit

func (w *IdlWriter) Emit(format string, args ...interface{})

func (*IdlWriter) EmitBlobShape

func (w *IdlWriter) EmitBlobShape(name string, shape *Shape)

func (*IdlWriter) EmitBooleanShape

func (w *IdlWriter) EmitBooleanShape(name string, shape *Shape)

func (*IdlWriter) EmitBooleanTrait

func (w *IdlWriter) EmitBooleanTrait(b bool, tname, indent string)

func (*IdlWriter) EmitCollectionShape

func (w *IdlWriter) EmitCollectionShape(shapeName, name string, shape *Shape)

func (*IdlWriter) EmitCustomTrait

func (w *IdlWriter) EmitCustomTrait(k string, v interface{}, indent string)

func (*IdlWriter) EmitDeprecatedTrait

func (w *IdlWriter) EmitDeprecatedTrait(v interface{}, indent string)

func (*IdlWriter) EmitDocumentation

func (w *IdlWriter) EmitDocumentation(doc, indent string)

func (*IdlWriter) EmitEnumTrait

func (w *IdlWriter) EmitEnumTrait(v interface{}, indent string)

func (*IdlWriter) EmitExamplesTrait

func (w *IdlWriter) EmitExamplesTrait(opname string, raw interface{})

func (*IdlWriter) EmitHttpErrorTrait

func (w *IdlWriter) EmitHttpErrorTrait(rv interface{}, indent string)

func (*IdlWriter) EmitHttpTrait

func (w *IdlWriter) EmitHttpTrait(rv interface{}, indent string)

func (*IdlWriter) EmitLengthTrait

func (w *IdlWriter) EmitLengthTrait(v interface{}, indent string)

func (*IdlWriter) EmitMapShape

func (w *IdlWriter) EmitMapShape(name string, shape *Shape)

func (*IdlWriter) EmitNumericShape

func (w *IdlWriter) EmitNumericShape(shapeName, name string, shape *Shape)

func (*IdlWriter) EmitOperationShape

func (w *IdlWriter) EmitOperationShape(name string, shape *Shape)

func (*IdlWriter) EmitPaginatedTrait

func (w *IdlWriter) EmitPaginatedTrait(d interface{})

func (*IdlWriter) EmitRangeTrait

func (w *IdlWriter) EmitRangeTrait(v interface{}, indent string)

func (*IdlWriter) EmitResourceShape

func (w *IdlWriter) EmitResourceShape(name string, shape *Shape)

func (*IdlWriter) EmitServiceShape

func (w *IdlWriter) EmitServiceShape(name string, shape *Shape)

func (*IdlWriter) EmitShape

func (w *IdlWriter) EmitShape(name string, shape *Shape)

func (*IdlWriter) EmitSimpleShape

func (w *IdlWriter) EmitSimpleShape(shapeName, name string)

func (*IdlWriter) EmitStringShape

func (w *IdlWriter) EmitStringShape(name string, shape *Shape)

func (*IdlWriter) EmitStringTrait

func (w *IdlWriter) EmitStringTrait(v, tname, indent string)

func (*IdlWriter) EmitStructureShape

func (w *IdlWriter) EmitStructureShape(name string, shape *Shape)

func (*IdlWriter) EmitTagsTrait

func (w *IdlWriter) EmitTagsTrait(v interface{}, indent string)

func (*IdlWriter) EmitTimestampShape

func (w *IdlWriter) EmitTimestampShape(name string, shape *Shape)

func (*IdlWriter) EmitTraitTrait

func (w *IdlWriter) EmitTraitTrait(v interface{})

func (*IdlWriter) EmitTraits

func (w *IdlWriter) EmitTraits(traits *Struct, indent string)

func (*IdlWriter) EmitUnionShape

func (w *IdlWriter) EmitUnionShape(name string, shape *Shape)

func (*IdlWriter) End

func (w *IdlWriter) End() string

type Member

type Member struct {
	Target string `json:"target"`
	//	Traits map[string]interface{} `json:"traits,omitempty"`
	Traits *Struct `json:"traits,omitempty"`
}

type Model

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

func AssembleModel

func AssembleModel(paths []string) (*Model, error)

func (*Model) Generate

func (model *Model) Generate(genName string, outdir string) error

func (*Model) Generator

func (model *Model) Generator(genName string) (Generator, error)

func (*Model) Namespaces

func (model *Model) Namespaces() []string

func (*Model) String

func (model *Model) String() string

type Parser

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

func (*Parser) EndOfFileError

func (p *Parser) EndOfFileError() error

func (*Parser) Error

func (p *Parser) Error(msg string) error

func (*Parser) ExpectIdentifier

func (p *Parser) ExpectIdentifier() (string, error)

func (*Parser) ExpectIdentifierArray

func (p *Parser) ExpectIdentifierArray() ([]string, error)

func (*Parser) ExpectIdentifierMap

func (p *Parser) ExpectIdentifierMap() (map[string]string, error)

func (*Parser) ExpectInt

func (p *Parser) ExpectInt() (int, error)

func (*Parser) ExpectNumber

func (p *Parser) ExpectNumber() (*Decimal, error)

func (*Parser) ExpectString

func (p *Parser) ExpectString() (string, error)

func (*Parser) ExpectStringArray

func (p *Parser) ExpectStringArray() ([]string, error)

func (*Parser) GetToken

func (p *Parser) GetToken() *Token

func (*Parser) MergeComment

func (p *Parser) MergeComment(comment1 string, comment2 string) string

func (*Parser) Parse

func (p *Parser) Parse() error

func (*Parser) SyntaxError

func (p *Parser) SyntaxError() error

func (*Parser) UngetToken

func (p *Parser) UngetToken()

type Scanner

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

func NewScanner

func NewScanner(r io.Reader) *Scanner

func (*Scanner) Scan

func (s *Scanner) Scan() Token

type Shape

type Shape struct {
	Type   string  `json:"type"`
	Traits *Struct `json:"traits,omitempty"` //service, resource, operation, apply

	//List and Set
	Member *Member `json:"member,omitempty"`

	//Map
	Key   *Member `json:"key,omitempty"`
	Value *Member `json:"value,omitempty"`

	//Structure and Union
	Members map[string]*Member `json:"members,omitempty"` //keys must be case-insensitively unique. For union, len(Members) > 0,

	//Resource
	Identifiers map[string]*ShapeRef `json:"identifiers,omitempty"`
	//FIXME preserve resource identifier order?
	Create               *ShapeRef   `json:"create,omitempty"`
	Put                  *ShapeRef   `json:"put,omitempty"`
	Read                 *ShapeRef   `json:"read,omitempty"`
	Update               *ShapeRef   `json:"update,omitempty"`
	Delete               *ShapeRef   `json:"delete,omitempty"`
	List                 *ShapeRef   `json:"list,omitempty"`
	CollectionOperations []*ShapeRef `json:"collectionOperations,omitempty"`

	//Resource and Service
	Operations []*ShapeRef `json:"operations,omitempty"`
	Resources  []*ShapeRef `json:"resources,omitempty"`

	//Operation
	Input  *ShapeRef `json:"input,omitempty"`
	Output *ShapeRef `json:"output,omitempty"`
	//	Errors []*Member `json:"errors,omitempty"`
	Errors []*ShapeRef `json:"errors,omitempty"`

	//Service
	Version string `json:"version,omitempty"`
	// contains filtered or unexported fields
}

type ShapeRef

type ShapeRef struct {
	Target string `json:"target"`
}

type Shapes

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

a Shapes object is a map from Shape ID to *Shape. It preserves the order of its keys, unlike a Go map

func (*Shapes) Get

func (s *Shapes) Get(key string) *Shape

func (*Shapes) Keys

func (s *Shapes) Keys() []string

func (*Shapes) Length

func (s *Shapes) Length() int

func (Shapes) MarshalJSON

func (s Shapes) MarshalJSON() ([]byte, error)

func (*Shapes) Put

func (s *Shapes) Put(key string, val *Shape)

func (*Shapes) UnmarshalJSON

func (s *Shapes) UnmarshalJSON(data []byte) error

type Struct

type Struct struct {
	Bindings map[string]interface{}
	// contains filtered or unexported fields
}

func NewStruct

func NewStruct() *Struct

func (*Struct) Get

func (s *Struct) Get(key string) interface{}

func (*Struct) Has

func (s *Struct) Has(key string) bool

func (*Struct) Keys

func (s *Struct) Keys() []string

func (*Struct) Length

func (s *Struct) Length() int

func (Struct) MarshalJSON

func (s Struct) MarshalJSON() ([]byte, error)

func (*Struct) Put

func (s *Struct) Put(key string, val interface{})

func (*Struct) String

func (s *Struct) String() string

func (*Struct) UnmarshalJSON

func (s *Struct) UnmarshalJSON(data []byte) error

type Token

type Token struct {
	Type  TokenType
	Text  string
	Line  int
	Start int
}

func (Token) IsNumeric

func (tok Token) IsNumeric() bool

func (Token) IsText

func (tok Token) IsText() bool

func (Token) String

func (tok Token) String() string

type TokenType

type TokenType int
const (
	UNDEFINED TokenType = iota
	EOF
	LINE_COMMENT
	BLOCK_COMMENT
	SYMBOL
	NUMBER
	STRING
	COLON
	SEMICOLON
	COMMA
	AT
	DOT
	EQUALS
	DOLLAR
	QUOTE
	SLASH
	QUESTION
	OPEN_BRACE
	CLOSE_BRACE
	OPEN_BRACKET
	CLOSE_BRACKET
	OPEN_PAREN
	CLOSE_PAREN
	OPEN_ANGLE
	CLOSE_ANGLE
	NEWLINE
	HASH
	AMPERSAND
	STAR
	BACKQUOTE
	TILDE
	BANG
)

func (TokenType) String

func (tokenType TokenType) String() string

Directories

Path Synopsis
cmd
smithy command

Jump to

Keyboard shortcuts

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