mqswag

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2017 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

This package handles swagger.json file parsing

Index

Constants

View Source
const (
	TypeDef        = "d"
	TypeOp         = "o"
	FieldSeparator = "?"
)

The type code we use in DAGNode's name. e.g. a node that represents definitions/User will have the name of "d:User"

View Source
const (
	MethodGet     = "get"
	MethodPut     = "put"
	MethodPost    = "post"
	MethodDelete  = "delete"
	MethodHead    = "head"
	MethodPatch   = "patch"
	MethodOptions = "options"
)
View Source
const (
	FlagSuccess = 1 << iota
	FlagFail
	FlagWeak
)
View Source
const (
	DAGDepth = 1000
)

Variables

Functions

func AddDef

func AddDef(name string, schema *Schema, swagger *Swagger, dag *DAG) error

func AddOperation

func AddOperation(pathName string, pathItem *spec.PathItem, method string, swagger *Swagger, dag *DAG, setPriority bool) error

func CollectParamDependencies

func CollectParamDependencies(params []spec.Parameter, swagger *Swagger, dag *DAG, dep *Dependencies) error

func CollectResponseDependencies

func CollectResponseDependencies(responses *spec.Responses, swagger *Swagger, dag *DAG, dep *Dependencies) error

func CollectSchemaDependencies

func CollectSchemaDependencies(schema *Schema, swagger *Swagger, dag *DAG, dep *Dependencies) error

collects all the objects referred to by the schema. All the object names are put into the specified map.

func CopyWithoutClass

func CopyWithoutClass(src map[string]map[string]interface{}, className string) map[string]map[string]interface{}

func GetDAGName

func GetDAGName(t string, n string, m string) string

func MatchAlways

func MatchAlways(criteria interface{}, existing interface{}) bool

Types

type DAG

type DAG struct {
	NameMap    map[string]*DAGNode // DAGNode name to node mapping.
	WeightList [DAGDepth]NodeList  // List ordered by DAGNodes' weights. Max of 1000 levels in DAG depth.
}

We expect a single thread on the server would handle the DAG creation and traversing. So no mutex for now.

func NewDAG

func NewDAG() *DAG

func (*DAG) AddNode

func (dag *DAG) AddNode(node *DAGNode) error

func (*DAG) AdjustNodeWeight

func (dag *DAG) AdjustNodeWeight(node *DAGNode, newWeight int, depList []*DAGNode) error

func (*DAG) CheckWeight

func (dag *DAG) CheckWeight()

func (*DAG) Init

func (dag *DAG) Init()

func (*DAG) IterateByWeight

func (dag *DAG) IterateByWeight(f DAGIterFunc) error

func (*DAG) IterateWeight

func (dag *DAG) IterateWeight(weight int, f DAGIterFunc) error

func (*DAG) NewNode

func (dag *DAG) NewNode(name string, data interface{}) (*DAGNode, error)

func (*DAG) Sort

func (dag *DAG) Sort()

sort the dag by priority and name

type DAGIterFunc

type DAGIterFunc func(previous *DAGNode, current *DAGNode) error

type DAGNode

type DAGNode struct {
	Name     string
	Weight   int // The weight determines which level it goes to in global DAG
	Priority int // The priority determines the sorting order within the same weight
	Data     interface{}
	Children NodeList
	// contains filtered or unexported fields
}

The traversal order is from this node to children. The children depend on the parent. The children's weight would be bigger than the parent.

func (*DAGNode) AddChild

func (node *DAGNode) AddChild(child *DAGNode) error

func (*DAGNode) AddDependencies

func (node *DAGNode) AddDependencies(dag *DAG, tags map[string]interface{}, asChild bool) error

AddDependencies adds the nodes named in the tags map either as child or parent of this node

func (*DAGNode) AdjustChildrenWeight

func (node *DAGNode) AdjustChildrenWeight(depList []*DAGNode) error

AdjustWeight changes the children's weight to be at least this node's weight + 1

func (*DAGNode) CheckChildrenWeight

func (node *DAGNode) CheckChildrenWeight() bool

func (*DAGNode) GetMethod

func (node *DAGNode) GetMethod() string

func (*DAGNode) GetName

func (node *DAGNode) GetName() string

func (*DAGNode) GetType

func (node *DAGNode) GetType() string

func (*DAGNode) ToString

func (node *DAGNode) ToString() string

type DB

type DB struct {
	Swagger *Swagger
	// contains filtered or unexported fields
}
var ObjDB DB

DB holds schema name to Schema mapping.

func (*DB) CloneSchema

func (db *DB) CloneSchema() *DB

Clone the db but not the objects

func (*DB) Delete

func (db *DB) Delete(name string, criteria interface{}, associations map[string]map[string]interface{},
	matches MatchFunc, desiredCount int) int

func (*DB) Find

func (db *DB) Find(name string, criteria interface{}, associations map[string]map[string]interface{},
	matches MatchFunc, desiredCount int) []interface{}

func (*DB) FindMatchingSchema

func (db *DB) FindMatchingSchema(obj interface{}) (string, *spec.Schema)

FindMatchingSchema finds the schema that matches the obj.

func (*DB) GetSchema

func (db *DB) GetSchema(name string) *Schema

func (*DB) Init

func (db *DB) Init(s *Swagger)

TODO it seems that if an object is not being used as a parameter to any operation, we don't need to track it in DB. This will save some resources. We can do this by adding swagger to a dag, then iterate through all the objects, and find those that doesn't have any oepration as a child.

func (*DB) Insert

func (db *DB) Insert(name string, obj interface{}, associations map[string]map[string]interface{}) error

func (*DB) Update

func (db *DB) Update(name string, criteria interface{}, associations map[string]map[string]interface{},
	matches MatchFunc, newObj map[string]interface{}, desiredCount int, patch bool) int

type DBEntry

type DBEntry struct {
	Data         map[string]interface{}            // The object itself.
	Associations map[string]map[string]interface{} // The objects associated with this object. Class to object map.
}

func (*DBEntry) Matches

func (entry *DBEntry) Matches(criteria interface{}, associations map[string]map[string]interface{}, matches MatchFunc) bool

type Dependencies

type Dependencies struct {
	Produces map[string]interface{}
	Consumes map[string]interface{}
	Default  map[string]interface{}
	IsPost   bool
}

Dependencies keeps track of what this operation consumes and produces. It also keeps track of what the default dependency is when there is no tag. Default always point to either "Produces" or "Consumes"

func (*Dependencies) CollectFromTag

func (dep *Dependencies) CollectFromTag(tag *MeqaTag) string

CollectFromTag collects from the tag. It returns the classname being collected.

type MatchFunc

type MatchFunc func(criteria interface{}, existing interface{}) bool

MatchFunc checks whether the input criteria and an input object matches.

type MeqaTag

type MeqaTag struct {
	Class     string
	Property  string
	Operation string
	Flags     int64
}

func GetMeqaTag

func GetMeqaTag(desc string) *MeqaTag

GetMeqaTag extracts the <meqa > tags. Example. for <meqa Pet.Name.update>, return Pet, Name, update

func (*MeqaTag) Equals

func (t *MeqaTag) Equals(o *MeqaTag) bool

func (*MeqaTag) ToString

func (t *MeqaTag) ToString() string

type NodeList

type NodeList []*DAGNode

func (NodeList) Len

func (n NodeList) Len() int

func (NodeList) Less

func (n NodeList) Less(i, j int) bool

func (NodeList) Swap

func (n NodeList) Swap(i, j int)

type Schema

type Schema spec.Schema

Schema is the swagger spec schema.

func CreateSchemaFromSimple

func CreateSchemaFromSimple(s *spec.SimpleSchema, v *spec.CommonValidations) *Schema

func (*Schema) Contains

func (schema *Schema) Contains(name string, swagger *Swagger) bool

func (*Schema) GetProperties

func (schema *Schema) GetProperties(swagger *Swagger) map[string]spec.Schema

Returns all the first level property names for this schema. We will follow the $refs until we hit a map.

func (*Schema) Iterate

func (schema *Schema) Iterate(iterFunc SchemaIterator, context interface{}, swagger *Swagger, followWeak bool) error

IterateSchema descends down the starting schema and call the iterator function for all the child schemas. The iteration order is parent first then children. It will abort on error. The followWeak flag indicates whether we should follow weak references when iterating.

func (*Schema) Matches

func (schema *Schema) Matches(object interface{}, swagger *Swagger) bool

Matches checks if the Schema matches the input interface. In proper swagger.json Enums should have types as well. So we don't check for untyped enums. TODO check format, handle AllOf, AnyOf, OneOf

func (*Schema) Parses

func (schema *Schema) Parses(name string, object interface{}, collection map[string][]interface{}, followRef bool, swagger *Swagger) error

Prases the object against this schema. If the obj and schema doesn't match return an error. Otherwise parse all the objects identified by the schema into the map indexed by the object class name.

type SchemaDB

type SchemaDB struct {
	Name      string
	Schema    *Schema
	NoHistory bool
	Objects   []*DBEntry
}

SchemaDB is our in-memory DB. It is organized around Schemas. Each schema maintains a list of objects that matches the schema. We don't build indexes and do linear search. This keeps the searching flexible for now.

func (*SchemaDB) CloneSchema

func (db *SchemaDB) CloneSchema() *SchemaDB

Clone this one but not the objects.

func (*SchemaDB) Delete

func (db *SchemaDB) Delete(criteria interface{}, associations map[string]map[string]interface{}, matches MatchFunc, desiredCount int) int

Delete deletes the specified number of elements that match the criteria. Input -1 for delete all. Returns the number of elements deleted.

func (*SchemaDB) Find

func (db *SchemaDB) Find(criteria interface{}, associations map[string]map[string]interface{}, matches MatchFunc, desiredCount int) []interface{}

Find finds the specified number of objects that match the input criteria.

func (*SchemaDB) Insert

func (db *SchemaDB) Insert(obj interface{}, associations map[string]map[string]interface{}) error

Insert inserts an object into the schema's object list.

func (*SchemaDB) Update

func (db *SchemaDB) Update(criteria interface{}, associations map[string]map[string]interface{},
	matches MatchFunc, newObj map[string]interface{}, desiredCount int, patch bool) int

Update finds the matching object, then update with the new one.

type SchemaIterator

type SchemaIterator func(swagger *Swagger, schemaName string, schema *Schema, context interface{}) error

type Swagger

type Swagger spec.Swagger

func CreateSwaggerFromURL

func CreateSwaggerFromURL(path string, meqaPath string) (*Swagger, error)

Init from a file

func (*Swagger) AddToDAG

func (swagger *Swagger) AddToDAG(dag *DAG) error

func (*Swagger) FindSchemaByName

func (swagger *Swagger) FindSchemaByName(name string) *Schema

FindSchemaByName finds the schema defined by name in the swagger document.

func (*Swagger) GetReferredSchema

func (swagger *Swagger) GetReferredSchema(schema *Schema) (string, *Schema, error)

GetReferredSchema returns what the schema refers to, and nil if it doesn't refer to any.

func (*Swagger) GetSchemaRootType

func (swagger *Swagger) GetSchemaRootType(schema *Schema, parentTag *MeqaTag) (*MeqaTag, *Schema)

GetSchemaRootType gets the real object type fo the specified schema. It only returns meaningful data for object and array of object type of parameters. If the parameter is a basic type it returns nil

Jump to

Keyboard shortcuts

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