Repository: olingo-odata4-js Updated Branches: refs/heads/OLINGO-324 ab2587ef3 -> b45ddd74d
[OLINGO-324] modify context type detection and first tests for this II Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/commit/b45ddd74 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/tree/b45ddd74 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/diff/b45ddd74 Branch: refs/heads/OLINGO-324 Commit: b45ddd74d4390c66f37bd30dc03d9d1e8006c476 Parents: ab2587e Author: Sven Kobler <[email protected]> Authored: Tue Jun 24 10:58:07 2014 +0200 Committer: Sven Kobler <[email protected]> Committed: Tue Jun 24 10:58:19 2014 +0200 ---------------------------------------------------------------------- datajs/src/lib/odata/json-light.js | 234 ++++++++++------------------ datajs/tests/odata-json-parse-tests.js | 49 +++--- 2 files changed, 113 insertions(+), 170 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/b45ddd74/datajs/src/lib/odata/json-light.js ---------------------------------------------------------------------- diff --git a/datajs/src/lib/odata/json-light.js b/datajs/src/lib/odata/json-light.js index 12c5d84..eddd58c 100644 --- a/datajs/src/lib/odata/json-light.js +++ b/datajs/src/lib/odata/json-light.js @@ -56,6 +56,7 @@ var lookupComplexType = oDataUtils.lookupComplexType; var lookupDefaultEntityContainer = oDataUtils.lookupDefaultEntityContainer; var lookupEntityContainer = oDataUtils.lookupEntityContainer; var lookupEntitySet = oDataUtils.lookupEntitySet; +var lookupSingleton = oDataUtils.lookupSingleton; var lookupEntityType = oDataUtils.lookupEntityType; var lookupFunctionImport = oDataUtils.lookupFunctionImport; var lookupNavigationPropertyType = oDataUtils.lookupNavigationPropertyType; @@ -66,7 +67,6 @@ var parseBool = oDataUtils.parseBool; var parseDateTime = oDataUtils.parseDateTime; var parseDateTimeOffset = oDataUtils.parseDateTimeOffset; var parseDuration = oDataUtils.parseDuration; - // CONTENT START var PAYLOADTYPE_FEED = "f"; @@ -1055,62 +1055,15 @@ var jsonLightMakePayloadInfo = function (kind, type) { /// <summary>Creates an object containing information for the context</summary> /// ... /// <returns type="Object">Object with type information -/// attribute detectedPayloadKind: see constants starting with PAYLOADTYPE_ -/// attribute deltaKind: deltainformation, one of the following valus DELTATYPE_FEED | DELTATYPE_DELETED_ENTRY | DELTATYPE_LINK | DELTATYPE_DELETED_LINK -/// attribute typeName: name of the type -/// attribute type: object containing type information for entity- and complex-types +/// attribute detectedPayloadKind(optional): see constants starting with PAYLOADTYPE_ +/// attribute deltaKind(optional): deltainformation, one of the following valus DELTATYPE_FEED | DELTATYPE_DELETED_ENTRY | DELTATYPE_LINK | DELTATYPE_DELETED_LINK +/// attribute typeName(optional): name of the type +/// attribute type(optional): object containing type information for entity- and complex-types ( null if a typeName is a primitive) /// </returns> var parseContextUriFragment = function( fragment, model ) { var ret = {}; - ret.deltaKind = undefined; - if (utils.endsWith(fragment, '/$delta')) { - ret.deltaKind = DELTATYPE_FEED; - fragment = fragment.substring(fragment.lenght - 7); - } else if (utils.endsWith(fragment, '/$deletedEntity')) { - ret.deltaKind = DELTATYPE_DELETED_ENTRY; - fragment = fragment.substring(fragment.lenght - 15); - } else if (utils.endsWith(fragment, '/$link')) { - ret.deltaKind = DELTATYPE_LINK; - fragment = fragment.substring(fragment.lenght - 6); - } else if (utils.endsWith(fragment, '/$deletedLink')) { - ret.deltaKind = DELTATYPE_DELETED_LINK; - fragment = fragment.substring(fragment.lenght - 13); - } - - if (utils.endsWith(fragment,")")) { - //remove the query function, cut fragment to matching '(' - var index = fragment.lenght - 2 ; - for ( var rCount = 1; rcount > 0 && index > 0; --index) { - if ( fragment.charAt(index)=='(') { - rCount ++; - } else if ( fragment.charAt(index)==')') { - rCount --; - } - } - - if (index === 0) { - //TODO throw error - } - - var previous = fragment.substring(0, index + 1); - - - if (previous !== 'Collection') { // Don't treat Collection(Edm.Type) as SelectExpand segment - var selectExpandStr = fragment.substring(index+2, fragment.length - 1); - var keyPattern = /^(?:-{0,1}\d+?|\w*'.+?'|[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}|.+?=.+?)$/; - var matches = keyPattern.exec(selectExpandStr); - if ( matches ) { - //TODO throw error - } - - ret.selectQueryOptionString = selectExpandStr; - fragment = previous; - } - - } - - ret.detectedPayloadKind = undefined; + if (fragment.indexOf('/') === -1 ) { if (fragment.length === 0) { // Capter 10.1 @@ -1118,7 +1071,7 @@ var parseContextUriFragment = function( fragment, model ) { return ret; } else if (fragment === 'Edm.Null') { // Capter 10.15 - ret.detectedPayloadKind = PAYLOADTYPE_PROPERTY; + ret.detectedPayloadKind = PAYLOADTYPE_VALUE; ret.isNullProperty = true; return ret; } else if (fragment === 'Collection($ref)') { @@ -1134,34 +1087,54 @@ var parseContextUriFragment = function( fragment, model ) { } } - ret.lastType = undefined; - ret.lastTypeName = undefined; - ret.detectedPayloadKind = undefined; + ret.type = undefined; + ret.typeName = undefined; - // split fragment at '/' - //TODO changes var fragmentParts = fragment.split("/"); - for(var i = 0; i < fragmentParts.length; ++i) { var fragment = fragmentParts[i]; - if (ret.lastTypeName === undefined) { + if (ret.typeName === undefined) { //preparation - var startPharen = fragment.indexOf('(') - var inPharenthesis = undefined; - if ( startPharen !== -1 ) { + if ( fragment.indexOf('(') !== -1 ) { + //remove the query function, cut fragment to matching '(' + var index = fragment.length - 2 ; + for ( var rCount = 1; rCount > 0 && index > 0; --index) { + if ( fragment.charAt(index)=='(') { + rCount --; + } else if ( fragment.charAt(index)==')') { + rCount ++; + } + } + + if (index === 0) { + //TODO throw error + } + //remove the projected entity from the fragment; TODO decide if we want to store the projected entity - inPharenthesis = fragment.substring(startPharen+1,fragment.length - 1); - // projection: Capter 10.7, 10.8 and 10.9 - fragment = fragment.substring(0,startPharen); + var inPharenthesis = fragment.substring(index+2,fragment.length - 1); + fragment = fragment.substring(0,index+1); if (utils.startsWith(fragment, 'Collection')) { ret.detectedPayloadKind = PAYLOADTYPE_COLLECTION; // Capter 10.14 - ret.lastTypeName = inPharenthesis; + ret.typeName = inPharenthesis; - ret.lastType = lookupEntityType( ret.lastTypeName, model); + var type = lookupEntityType(ret.typeName, model); + if ( type !== null) { + ret.type = type; + continue; + } + type = lookupComplexType(ret.typeName, model); + if ( type !== null) { + ret.type = type; + continue; + } + + ret.type = null;//in case of #Collection(Edm.String) only lastTypeName is filled + continue; } else { + // projection: Capter 10.7, 10.8 and 10.9 ret.projection = inPharenthesis; } } @@ -1170,9 +1143,9 @@ var parseContextUriFragment = function( fragment, model ) { //check for entity var entitySet = lookupEntitySet(container.entitySet, fragment); - if ( entitySet !== undefined) { - ret.lastTypeName = entitySet.entityType; - ret.lastType = lookupEntityType( ret.lastTypeName, model); + if ( entitySet !== null) { + ret.typeName = entitySet.entityType; + ret.type = lookupEntityType( ret.typeName, model); ret.detectedPayloadKind = PAYLOADTYPE_FEED; // Capter 10.2 continue; @@ -1180,14 +1153,22 @@ var parseContextUriFragment = function( fragment, model ) { //check for singleton var singleton = lookupSingleton(container.singleton, fragment); - if ( singleton !== undefined) { - ret.lastTypeName = singleton.entityType; - ret.lastType = lookupEntityType( ret.lastTypeName, model); + if ( singleton !== null) { + ret.typeName = singleton.entityType; + ret.type = lookupEntityType( ret.typeName, model); ret.detectedPayloadKind = PAYLOADTYPE_ENTRY; // Capter 10.4 continue; } + if (jsonLightIsPrimitiveType(fragment)) { + ret.typeName = fragment; + ret.type = null; + ret.detectedPayloadKind = PAYLOADTYPE_VALUE; + continue; + } + + //TODO throw ERROR } else { //check for $entity if (utils.endsWith(fragment, '$entity') && (ret.detectedPayloadKind === PAYLOADTYPE_FEED)) { @@ -1199,99 +1180,50 @@ var parseContextUriFragment = function( fragment, model ) { //check for derived types if (fragment.indexOf('.') !== -1) { // Capter 10.6 - ret.lastTypeName = fragment; - var type = lookupEntityType(ret.lastTypeName, model); - if ( entitySet !== undefined) { - ret.lastType = type; + ret.typeName = fragment; + var type = lookupEntityType(ret.typeName, model); + if ( type !== null) { + ret.type = type; continue; } - type = lookupComplexType(ret.lastTypeName, model); - if ( entitySet !== undefined) { - ret.lastType = type; + type = lookupComplexType(ret.typeName, model); + if ( type !== null) { + ret.type = type; continue; } - //ERROR invalid type + //TODO throw ERROR invalid type } //check for property value if ( ret.detectedPayloadKind === PAYLOADTYPE_FEED || ret.detectedPayloadKind === PAYLOADTYPE_ENTRY) { - var property = lookupProperty(ret.lastType.properties, fragment); - if (property !== undefined) { - ret.lastTypeName = property.type; - if (!jsonLightIsPrimitiveType(ret.lastTypeName)) { - ret.lastType = lookupComplexType(ret.lastTypeName, model); - } else { - ret.lastType = undefined; - } - ret.detectedPayloadKind === PAYLOADTYPE_PROPERTY; + var property = lookupProperty(ret.type.property, fragment); + if (property !== null) { + ret.typeName = property.type; + ret.type = lookupComplexType(ret.typeName, model); + ret.detectedPayloadKind = PAYLOADTYPE_PROPERTY; // Capter 10.15 } continue; } - } - } - return ret; - -/* - var qualifiedName = fragmentParts[0]; - var typeCast = fragmentParts[1]; - - if () { - ret.detectedPayloadKind = PAYLOADTYPE_VALUE; - return ret; - } - - if (isCollectionType(qualifiedName)) { - return ret; - } - - var entityType = typeCast; - var entitySet, functionImport, containerName; - if (!typeCast) { - var nsEnd = qualifiedName.lastIndexOf("."); - var simpleName = qualifiedName.substring(nsEnd + 1); - var container = (simpleName === qualifiedName) ? - lookupDefaultEntityContainer(model) : - lookupEntityContainer(qualifiedName.substring(0, nsEnd), model); - - if (container) { - entitySet = lookupEntitySet(container.entitySet, simpleName); - functionImport = container.functionImport; - containerName = container.name; - entityType = !!entitySet ? entitySet.entityType : null; - } - } - - var info; - if (d > 0) { - info = jsonLightMakePayloadInfo(PAYLOADTYPE_OBJECT, entityType); - info.entitySet = entitySet; - info.functionImport = functionImport; - info.containerName = containerName; - return info; - } - if (entityType) { - info = jsonLightMakePayloadInfo(PAYLOADTYPE_FEED, entityType); - info.entitySet = entitySet; - info.functionImport = functionImport; - info.containerName = containerName; - return info; - } - - if (isArray(data.value) && !lookupComplexType(qualifiedName, model)) { - var item = data.value[0]; - if (!isPrimitive(item)) { - if (jsonLightIsEntry(item) || !inferFeedAsComplexType) { - return jsonLightMakePayloadInfo(PAYLOADTYPE_FEED, null); - } + if (fragment === '$delta') { + ret.deltaKind = DELTATYPE_FEED; + continue; + } else if (utils.endsWith(fragment, '/$deletedEntity')) { + ret.deltaKind = DELTATYPE_DELETED_ENTRY; + continue; + } else if (utils.endsWith(fragment, '/$link')) { + ret.deltaKind = DELTATYPE_LINK; + continue; + } else if (utils.endsWith(fragment, '/$deletedLink')) { + ret.deltaKind = DELTATYPE_DELETED_LINK; + continue; } + //TODO throw ERROr } - - return jsonLightMakePayloadInfo(PAYLOADTYPE_OBJECT, qualifiedName); } -*/ + return ret; }; var jsonLightPayloadInfo = function (data, model) { http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/b45ddd74/datajs/tests/odata-json-parse-tests.js ---------------------------------------------------------------------- diff --git a/datajs/tests/odata-json-parse-tests.js b/datajs/tests/odata-json-parse-tests.js index c2dd33a..eaeefa8 100644 --- a/datajs/tests/odata-json-parse-tests.js +++ b/datajs/tests/odata-json-parse-tests.js @@ -24,8 +24,6 @@ } function runWithMetadata(metaDatasuccess) { - - var oHeaders = { 'Accept': 'text/html,application/xhtml+xml,application/xml,application/json;odata.metadata=full', "Odata-Version": "4.0", @@ -43,57 +41,70 @@ }; djstest.addTest(function test1() { - var checkLastTypeName = function (metadata, input, expected) { + var checkAll = function (metadata, input, expected) { + var info = OData.jsonLight.jsonLightPayloadInfo({ "@odata.context" : input}, metadata) + djstest.assertAreEqual(info,expected, "Test context fragment: "+ input); + }; + + var checkLastTypeName = function (metadata, input, expectedKind, expectedLastTypeName) { var info = OData.jsonLight.jsonLightPayloadInfo({ "@odata.context" : input}, metadata) - djstest.assertAreEqual(info.lastTypeName,expected, "Test context fragment: "+ input); + djstest.assertAreEqual(info.detectedPayloadKind,expectedKind, "Test context fragment: "+ input); + djstest.assertAreEqual(info.typeName,expectedLastTypeName, "Test context fragment: "+ input); + }; + var checkProjection = function (metadata, input, expectedKind, expectedLastTypeName, projection) { + var info = OData.jsonLight.jsonLightPayloadInfo({ "@odata.context" : input}, metadata) + djstest.assertAreEqual(info.detectedPayloadKind,expectedKind, "Test context fragment: "+ input); + djstest.assertAreEqual(info.typeName,expectedLastTypeName, "Test context fragment: "+ input); + djstest.assertAreEqual(info.projection,projection, "Test context fragment: "+ input); }; var checkKind = function (metadata, input, expected) { var info = OData.jsonLight.jsonLightPayloadInfo({ "@odata.context" : input}, metadata) djstest.assertAreEqual(info.detectedPayloadKind,expected, "Test context fragment: "+ input); }; + var success = function(metadata){ //Chapter 10.1 checkKind(metadata, '#', 's'); //Chapter 10.2 - checkKind(metadata, '#Foods', 'f'); + checkLastTypeName(metadata, '#Foods', 'f', 'DataJS.Tests.V4.Food'); //Chapter 10.3 - checkKind(metadata, '#Foods/$entity', 'e'); + checkLastTypeName(metadata, '#Foods/$entity', 'e', 'DataJS.Tests.V4.Food'); //Chapter 10.4 //checkKind(metadata, '#Singleton', ''); //Chapter 10.5 - checkKind(metadata, '#Foods/DataJS.Tests.V4.Food', 'f'); + checkLastTypeName(metadata, '#Foods/DataJS.Tests.V4.Food', 'f', 'DataJS.Tests.V4.Food'); //Chapter 10.6 - checkKind(metadata, '#Foods/DataJS.Tests.V4.Food/$entity', 'e'); + checkLastTypeName(metadata, '#Foods/DataJS.Tests.V4.Food/$entity', 'e', 'DataJS.Tests.V4.Food'); //Chapter 10.7 - checkKind(metadata, '#Foods(FoodID,Name)', 'f'); + checkProjection(metadata, '#Foods(FoodID,Name)', 'f', 'DataJS.Tests.V4.Food','FoodID,Name'); //Chapter 10.8 - checkKind(metadata, '#Foods(FoodID,Name)/$entity', 'e'); + checkProjection(metadata, '#Foods(FoodID,Name)/$entity', 'e', 'DataJS.Tests.V4.Food','FoodID,Name'); //Chapter 10.9 - checkKind(metadata, '#Foods(FoodID,Name,Category,Category+(CategoryID,Name))', 'f'); + checkProjection(metadata, '#Foods(FoodID,Name,Category,Category+(CategoryID,Name))', 'f', + 'DataJS.Tests.V4.Food','FoodID,Name,Category,Category+(CategoryID,Name)'); //Chapter 10.10 - checkKind(metadata, '#Foods(FoodID,Name,Category,Category+(CategoryID,Name))/$entity', 'e'); + checkProjection(metadata, '#Foods(FoodID,Name,Category,Category+(CategoryID,Name))/$entity', 'e', + 'DataJS.Tests.V4.Food','FoodID,Name,Category,Category+(CategoryID,Name)'); //Chapter 10.11 checkKind(metadata, '#Collection($ref)', 'erls'); //Chapter 10.12 checkKind(metadata, '#$ref', 'erl'); //Chapter 10.13 - checkKind(metadata, '#Foods(0)/Packaging', 'p'); + checkKind(metadata, '#Foods(0)/Packaging', 'p', 'DataJS.Tests.V4.Package'); //Chapter 10.14 - checkKind(metadata, '#Collection(Edm.String)', 'c'); + checkKind(metadata, '#Collection(Edm.String)', 'c', 'Edm.String'); //Chapter 10.15 checkKind(metadata, '#Edm.String', 'v'); - //TODO add tests for delta tokens - - - checkLastTypeName(metadata, '#Foods', 'DataJS.Tests.V4.Food'); + checkKind(metadata, '#Edm.Null', 'v'); + //TODO add tests for delta tokens djstest.done(); }; runWithMetadata(success); - },'simple'); + },'test jsonLightPayloadInfo'); })(this);
