Documentation ¶
Overview ¶
This package handles swagger.json file parsing
Index ¶
- Constants
- Variables
- func AddDef(name string, schema *Schema, swagger *Swagger, dag *DAG) error
- func AddOperation(pathName string, pathItem *spec.PathItem, method string, swagger *Swagger, ...) error
- func CollectParamDependencies(params []spec.Parameter, swagger *Swagger, dag *DAG, dep *Dependencies) error
- func CollectResponseDependencies(responses *spec.Responses, swagger *Swagger, dag *DAG, dep *Dependencies) error
- func CollectSchemaDependencies(schema *Schema, swagger *Swagger, dag *DAG, dep *Dependencies) error
- func CopyWithoutClass(src map[string]map[string]interface{}, className string) map[string]map[string]interface{}
- func GetDAGName(t string, n string, m string) string
- func MatchAlways(criteria interface{}, existing interface{}) bool
- type DAG
- func (dag *DAG) AddNode(node *DAGNode) error
- func (dag *DAG) AdjustNodeWeight(node *DAGNode, newWeight int, depList []*DAGNode) error
- func (dag *DAG) CheckWeight()
- func (dag *DAG) Init()
- func (dag *DAG) IterateByWeight(f DAGIterFunc) error
- func (dag *DAG) IterateWeight(weight int, f DAGIterFunc) error
- func (dag *DAG) NewNode(name string, data interface{}) (*DAGNode, error)
- func (dag *DAG) Sort()
- type DAGIterFunc
- type DAGNode
- func (node *DAGNode) AddChild(child *DAGNode) error
- func (node *DAGNode) AddDependencies(dag *DAG, tags map[string]interface{}, asChild bool) error
- func (node *DAGNode) AdjustChildrenWeight(depList []*DAGNode) error
- func (node *DAGNode) CheckChildrenWeight() bool
- func (node *DAGNode) GetMethod() string
- func (node *DAGNode) GetName() string
- func (node *DAGNode) GetType() string
- func (node *DAGNode) ToString() string
- type DB
- func (db *DB) CloneSchema() *DB
- func (db *DB) Delete(name string, criteria interface{}, ...) int
- func (db *DB) Find(name string, criteria interface{}, ...) []interface{}
- func (db *DB) FindMatchingSchema(obj interface{}) (string, *spec.Schema)
- func (db *DB) GetSchema(name string) *Schema
- func (db *DB) Init(s *Swagger)
- func (db *DB) Insert(name string, obj interface{}, associations map[string]map[string]interface{}) error
- func (db *DB) Update(name string, criteria interface{}, ...) int
- type DBEntry
- type Dependencies
- type MatchFunc
- type MeqaTag
- type NodeList
- type Schema
- func (schema *Schema) Contains(name string, swagger *Swagger) bool
- func (schema *Schema) GetProperties(swagger *Swagger) map[string]spec.Schema
- func (schema *Schema) Iterate(iterFunc SchemaIterator, context interface{}, swagger *Swagger, ...) error
- func (schema *Schema) Matches(object interface{}, swagger *Swagger) bool
- func (schema *Schema) Parses(name string, object interface{}, collection map[string][]interface{}, ...) error
- type SchemaDB
- func (db *SchemaDB) CloneSchema() *SchemaDB
- func (db *SchemaDB) Delete(criteria interface{}, associations map[string]map[string]interface{}, ...) int
- func (db *SchemaDB) Find(criteria interface{}, associations map[string]map[string]interface{}, ...) []interface{}
- func (db *SchemaDB) Insert(obj interface{}, associations map[string]map[string]interface{}) error
- func (db *SchemaDB) Update(criteria interface{}, associations map[string]map[string]interface{}, ...) int
- type SchemaIterator
- type Swagger
Constants ¶
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"
const ( MethodGet = "get" MethodPut = "put" MethodPost = "post" MethodDelete = "delete" MethodHead = "head" MethodPatch = "patch" MethodOptions = "options" )
const ( FlagSuccess = 1 << iota FlagFail FlagWeak )
const (
DAGDepth = 1000
)
Variables ¶
var MethodAll []string = []string{MethodGet, MethodPut, MethodPost, MethodDelete, MethodHead, MethodPatch, MethodOptions}
Functions ¶
func AddOperation ¶
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 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 (*DAG) AdjustNodeWeight ¶
func (*DAG) CheckWeight ¶
func (dag *DAG) CheckWeight()
func (*DAG) IterateByWeight ¶
func (dag *DAG) IterateByWeight(f DAGIterFunc) error
func (*DAG) IterateWeight ¶
func (dag *DAG) IterateWeight(weight int, f DAGIterFunc) error
type DAGIterFunc ¶
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) AddDependencies ¶
AddDependencies adds the nodes named in the tags map either as child or parent of this node
func (*DAGNode) AdjustChildrenWeight ¶
AdjustWeight changes the children's weight to be at least this node's weight + 1
func (*DAGNode) CheckChildrenWeight ¶
type DB ¶
type DB struct { Swagger *Swagger // contains filtered or unexported fields }
var ObjDB DB
DB holds schema name to Schema mapping.
func (*DB) FindMatchingSchema ¶
FindMatchingSchema finds the schema that matches the obj.
func (*DB) Init ¶
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.
type DBEntry ¶
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 ¶
func GetMeqaTag ¶
GetMeqaTag extracts the <meqa > tags. Example. for <meqa Pet.Name.update>, return Pet, Name, update
type Schema ¶
Schema is the swagger spec schema.
func CreateSchemaFromSimple ¶
func CreateSchemaFromSimple(s *spec.SimpleSchema, v *spec.CommonValidations) *Schema
func (*Schema) GetProperties ¶
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 ¶
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 ¶
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 ¶
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.
type SchemaIterator ¶
type Swagger ¶
func CreateSwaggerFromURL ¶
Init from a file
func (*Swagger) FindSchemaByName ¶
FindSchemaByName finds the schema defined by name in the swagger document.
func (*Swagger) GetReferredSchema ¶
GetReferredSchema returns what the schema refers to, and nil if it doesn't refer to any.
func (*Swagger) GetSchemaRootType ¶
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