From 939408698a11bb6563f2433a58a82717fbbc6fb9 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Wed, 8 Apr 2015 22:40:51 +0530 Subject: [PATCH] Finished removing async.waterfalls --- src/formats/json.coffee | 55 +++++++------ src/formats/jsonschema-v4.coffee | 130 +++++++++++++++---------------- src/index.coffee | 14 ++-- src/typeresolution.coffee | 10 +-- 4 files changed, 101 insertions(+), 108 deletions(-) diff --git a/src/formats/json.coffee b/src/formats/json.coffee index a9352ae..43b584d 100644 --- a/src/formats/json.coffee +++ b/src/formats/json.coffee @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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. diff --git a/src/formats/jsonschema-v4.coffee b/src/formats/jsonschema-v4.coffee index b8d2295..7f1d7c8 100644 --- a/src/formats/jsonschema-v4.coffee +++ b/src/formats/jsonschema-v4.coffee @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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. @@ -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 = { diff --git a/src/index.coffee b/src/index.coffee index e91f4db..6155c22 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -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 = { diff --git a/src/typeresolution.coffee b/src/typeresolution.coffee index eab7a24..53e65f0 100644 --- a/src/typeresolution.coffee +++ b/src/typeresolution.coffee @@ -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.