Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Finished removing async.waterfalls
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Apr 8, 2015
1 parent 4899efb commit 9394086
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 108 deletions.
55 changes: 26 additions & 29 deletions src/formats/json.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ inspect = require '../inspect'
# object with both representation in JSON and optionally also
# some additional info.
resolveProperty = (prop, inherited, cb) ->
async.waterfall [
(next) -> handleElement prop, inherited, next
(repr, next) ->
next null,
name: inspect.findPropertyName prop
repr: repr
], cb
handleElement prop, inherited, (err, repr) ->
return cb err if err

cb null,
name: inspect.findPropertyName prop
repr: repr

# Turns *Element* node containing oneOf into an array
# of 'resolved property' objects with both representation in JSON and
Expand Down Expand Up @@ -76,21 +74,20 @@ handleObjectElement = (objectElement, resolvedType, inherited, cb) ->
heritage = inspect.getHeritage fixed, resolvedType
props = inspect.listProperties objectElement

async.waterfall [
(next) -> resolveProperties props, heritage, next
(resolvedProps, next) -> buildObjectRepr {resolvedProps}, next
], cb
resolveProperties props, heritage, (err, resolvedProps) ->
return cb err if err

buildObjectRepr {resolvedProps}, cb


# Turns *Element* node containing array or enum item into a 'resolved item'
# object with both representation in JSON and optionally also
# some additional info.
resolveItem = (item, inherited, cb) ->
async.waterfall [
(next) -> handleElement item, inherited, next
(repr, next) -> next null, {repr} # no additional info needed in this case
], cb
handleElement item, inherited, (err, repr) ->
return cb err if err

cb null, {repr}

# Turns a list of *Element* nodes containing array items into an array
# of 'resolved item' objects with both representation in JSON and
Expand Down Expand Up @@ -132,6 +129,7 @@ buildArrayRepr = ({arrayElement, resolvedItems, resolvedType, fixed}, cb) ->
vals = inspect.listValues arrayElement
else
vals = inspect.listValuesOrSamples arrayElement

async.mapSeries vals, (val, next) ->
coerceNestedLiteral val.literal, resolvedType.nested, next
, cb
Expand All @@ -144,10 +142,10 @@ handleArrayElement = (arrayElement, resolvedType, inherited, cb) ->
heritages = inspect.listPossibleHeritages fixed, resolvedType
items = inspect.listItems arrayElement

async.waterfall [
(next) -> resolveArrayItems items, heritages, next
(resolvedItems, next) -> buildArrayRepr {arrayElement, resolvedItems, resolvedType, fixed}, next
], cb
resolveArrayItems items, heritages, (err, resolvedItems) ->
return cb err if err

buildArrayRepr {arrayElement, resolvedItems, resolvedType, fixed}, cb


# Resolves items as enum values. Produces only one 'resolved item' object or
Expand Down Expand Up @@ -180,10 +178,10 @@ handleEnumElement = (enumElement, resolvedType, inherited, cb) ->
heritage = inspect.getHeritage fixed, resolvedType
items = inspect.listItems enumElement

async.waterfall [
(next) -> resolveEnumItems items, heritage, next
(resolvedItem, next) -> buildEnumRepr {enumElement, resolvedItem, resolvedType}, next
], cb
resolveEnumItems items, heritage, (err, resolvedItem) ->
return cb err if err

buildEnumRepr {enumElement, resolvedItem, resolvedType}, cb


# Generates JSON representation for given *Element* node containing a primitive
Expand Down Expand Up @@ -211,12 +209,11 @@ createElementHandler = (resolvedType) ->

# Generates JSON representation for given *Element* node.
handleElement = (element, inherited, cb) ->
async.waterfall [
(next) -> resolveType element, inherited.typeName, next
(resolvedType, next) ->
handle = createElementHandler resolvedType
handle element, resolvedType, inherited, next
], cb
resolveType element, inherited.typeName, (err, resolvedType) ->
return cb err if err

handle = createElementHandler resolvedType
handle element, resolvedType, inherited, cb


# Transforms given MSON AST into JSON.
Expand Down
130 changes: 65 additions & 65 deletions src/formats/jsonschema-v4.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ addDefault = (element, resolvedType, repr, cb) ->
# object with both representation in JSON Schema and optionally also
# some additional info.
resolveProperty = (prop, inherited, cb) ->
async.waterfall [
(next) -> handleElement prop, inherited, next
(repr, next) ->
next null,
name: inspect.findPropertyName prop
variableName: inspect.hasVariablePropertyName prop
repr: repr
required: inspect.isRequired prop, inherited
], cb
handleElement prop, inherited, (err, repr) ->
return cb err if err

cb null,
name: inspect.findPropertyName prop
variableName: inspect.hasVariablePropertyName prop
repr: repr
required: inspect.isRequired prop, inherited

# Turns multiple *Element* nodes containing object properties into
# 'resolved property' objects with both representation in JSON Schema
Expand Down Expand Up @@ -81,25 +79,26 @@ buildObjectRepr = ({resolvedProps, fixed}, cb) ->
repr.additionalProperties = false if fixed

if resolvedProps.length
async.waterfall [
(next) -> groupResolvedProperties resolvedProps, next
({regularProps, variableProps, requiredProps}, next) ->
if regularProps.length
propsRepr = {}
propsRepr[rp.name] = rp.repr for rp in regularProps
repr.properties = propsRepr
groupResolvedProperties resolvedProps, (err, allProps) ->
return cb err if err

{regularProps, variableProps, requiredProps} = allProps

if regularProps.length
propsRepr = {}
propsRepr[rp.name] = rp.repr for rp in regularProps
repr.properties = propsRepr

if variableProps.length is 1
repr.patternProperties = {'': variableProps[0].repr}
if variableProps.length is 1
repr.patternProperties = {'': variableProps[0].repr}

else if variableProps.length > 1
repr.patternProperties = {'': {}}
else if variableProps.length > 1
repr.patternProperties = {'': {}}

if requiredProps.length
repr.required = (rp.name for rp in requiredProps)
if requiredProps.length
repr.required = (rp.name for rp in requiredProps)

cb null, repr
]
cb null, repr
else
cb null, repr

Expand All @@ -111,23 +110,21 @@ handleObjectElement = (objectElement, resolvedType, inherited, cb) ->
heritage = inspect.getHeritage fixed
props = inspect.listProperties objectElement

async.waterfall [
(next) -> resolveProperties props, heritage, next
(resolvedProps, next) -> buildObjectRepr {resolvedProps, fixed}, next
], cb
resolveProperties props, heritage, (err, resolvedProps) ->
return cb err if err

buildObjectRepr {resolvedProps, fixed}, cb


# Turns *Element* node containing array or enum item into a 'resolved item'
# object with both representation in JSON Schema and optionally also
# some additional info.
resolveItem = (item, inherited, cb) ->
async.waterfall [
(next) -> handleElement item, inherited, next
(repr, next) ->
next null,
repr: repr
fixed: inspect.isFixed item
], cb
handleElement item, inherited, (err, repr) ->
return cb err if err
cb null,
repr: repr
fixed: inspect.isFixed item


# Turns multiple *Element* nodes containing array or enum item into
Expand Down Expand Up @@ -215,10 +212,10 @@ handleArrayElement = (arrayElement, resolvedType, inherited, cb) ->
heritage = inspect.getHeritage fixed, resolvedType
items = inspect.listItems arrayElement

async.waterfall [
(next) -> resolveItems items, heritage, next
(resolvedItems, next) -> buildArrayRepr {arrayElement, resolvedItems, resolvedType, fixed}, next
], cb
resolveItems items, heritage, (err, resolvedItems) ->
return cb err if err

buildArrayRepr {arrayElement, resolvedItems, resolvedType, fixed}, cb


# Builds JSON Schema representation for a group of items with primitive types
Expand Down Expand Up @@ -295,15 +292,17 @@ groupItemsByPrimitiveTypes = (items, resolvedTypes, cb) ->
# Helper function to inspect inline enum *Element* nodes. Groups items by their
# primitive type and gets information about how to render these groups.
inspectEnumItems = (items, nestedTypeName, cb) ->
async.waterfall [
(next) -> resolveTypes items, nestedTypeName, next
(resolvedTypes, next) -> groupItemsByPrimitiveTypes items, resolvedTypes, next
(groups, nonPrimitiveItems, next) ->
resolveTypes items, nestedTypeName, (err, resolvedTypes) ->
return cb err if err

groupItemsByPrimitiveTypes items, resolvedTypes, (err, groups, nonPrimitiveItems) ->
return cb err if err

for group in groups
hasSamples = inspect.haveVariableValues group.items
group.strategy = if hasSamples then 'singleType' else 'values'
next null, {inline: false, groups, nonPrimitiveItems}
], cb

cb null, {inline: false, groups, nonPrimitiveItems}


# Helper function to inspect inline enum *Element* nodes. Creates one mostly
Expand Down Expand Up @@ -353,12 +352,11 @@ handleEnumElement = (enumElement, resolvedType, inherited, cb) ->
fixed = inspect.isOrInheritsFixed enumElement, inherited
heritage = inspect.getHeritage fixed, resolvedType

async.waterfall [
(next) -> inspectEnum enumElement, resolvedType, next
(context, next) ->
context.inherited = heritage
buildEnumRepr context, next
], cb
inspectEnum enumElement, resolvedType, (err, context) ->
return cb err if err

context.inherited = heritage
buildEnumRepr context, cb


# Generates JSON Schema representation for given *Element* node containing
Expand Down Expand Up @@ -391,16 +389,18 @@ createElementHandler = (resolvedType) ->

# Generates JSON Schema representation for given *Element* node.
handleElement = (element, inherited, cb) ->
async.waterfall [
(next) -> resolveType element, inherited.typeName, next
(resolvedType, next) ->
handle = createElementHandler resolvedType
async.waterfall [
(done) -> handle element, resolvedType, inherited, done
(repr, done) -> addDescription element, repr, done
(repr, done) -> addDefault element, resolvedType, repr, done
], next
], cb
resolveType element, inherited.typeName, (err, resolvedType) ->
return cb err if err

handle = createElementHandler resolvedType

handle element, resolvedType, inherited, (err, repr) ->
return cb err if err

addDescription element, repr, (err, repr) ->
return cb err if err

addDefault element, resolvedType, repr, cb


# Adds JSON Schema declaration to given representation object.
Expand All @@ -411,10 +411,10 @@ addSchemaDeclaration = (repr, cb) ->

# Transforms given MSON AST into JSON Schema.
transform = (ast, cb) ->
async.waterfall [
(next) -> handleElement inspect.getAsElement(ast), {}, next
addSchemaDeclaration
], cb
handleElement inspect.getAsElement(ast), {}, (err, repr) ->
return cb err if err

addSchemaDeclaration repr, cb


module.exports = {
Expand Down
14 changes: 5 additions & 9 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ represent = ({ast, contentType}, cb) ->

{lib, serialize} = formats[selectedContentType]

async.waterfall [
(next) ->
lib.transform ast, next
,
(obj, next) ->
serialize obj, next

], (err, repr) ->
cb err, repr, selectedContentType
lib.transform ast, (err, obj) ->
return cb err if err

serialize obj, (err, repr) ->
cb err, repr, selectedContentType


module.exports = {
Expand Down
10 changes: 5 additions & 5 deletions src/typeresolution.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ simplifyTypeSpecification = (typeSpec, cb) ->
name = inspect.findTypeName typeSpec
return cb null, null if not name # no type name? return null...

async.waterfall [
(next) -> ensureBaseType name, next
(next) -> simplifyNestedTypes typeSpec, next
], (err, nested) ->
cb err, ({name, nested} unless err)
ensureBaseType name, (err) ->
return cb err if err

simplifyNestedTypes typeSpec, (err, nested) ->
cb err, ({name, nested} unless err)


# Helps to identify whether given *Element* node contains an implicit array.
Expand Down

0 comments on commit 9394086

Please sign in to comment.