Updated to latest jsonld.js and swapped @coerce key/values.
authorDave Longley <dlongley@digitalbazaar.com>
Thu, 20 Oct 2011 19:43:53 -0400
changeset 226 f9ef74ccab86
parent 225 412de64fe08d
child 227 7430f2765082
child 229 ce16d57ea9ca
Updated to latest jsonld.js and swapped @coerce key/values.
playground/jsonld.js
playground/playground-examples.js
--- a/playground/jsonld.js	Sun Oct 16 18:57:13 2011 -0400
+++ b/playground/jsonld.js	Thu Oct 20 19:43:53 2011 -0400
@@ -416,21 +416,21 @@
  */
 jsonld.mergeContexts = function(ctx1, ctx2)
 {
-   // copy contexts
+   // copy context to merged output
    var merged = _clone(ctx1);
-   var copy = _clone(ctx2);
 
    // if the new context contains any IRIs that are in the merged context,
    // remove them from the merged context, they will be overwritten
-   for(var key in copy)
+   for(var key in ctx2)
    {
       // ignore special keys starting with '@'
       if(key.indexOf('@') !== 0)
       {
          for(var mkey in merged)
          {
-            if(merged[mkey] === copy[key])
+            if(merged[mkey] === ctx2[key])
             {
+               // FIXME: update related @coerce rules
                delete merged[mkey];
                break;
             }
@@ -438,101 +438,30 @@
       }
    }
 
-   // @coerce must be specially-merged, remove from contexts
-   var coerceExists = ('@coerce' in merged) || ('@coerce' in copy);
-   if(coerceExists)
+   // merge contexts
+   for(var key in ctx2)
    {
-      var c1 = ('@coerce' in merged) ? merged['@coerce'] : {};
-      var c2 = ('@coerce' in copy) ? copy['@coerce'] : {};
-      delete merged['@coerce'];
-      delete copy['@coerce'];
-   }
-
-   // merge contexts
-   for(var key in copy)
-   {
-      merged[key] = copy[key];
+      // skip @coerce, to be merged below
+      if(key !== '@coerce')
+      {
+         merged[key] = _clone(ctx2[key]);
+      }
    }
    
-   // special-merge @coerce
-   if(coerceExists)
+   // merge @coerce
+   if('@coerce' in ctx2)
    {
-      for(var type in c1)
+      if(!('@coerce' in merged))
       {
-         // append existing-type properties that don't already exist
-         if(type in c2)
+         merged['@coerce'] = _clone(ctx2['@coerce']);
+      }
+      else
+      {
+         for(var key in ctx2['@coerce'])
          {
-            var p1 = c1[type];
-            var p2 = c2[type];
-            
-            // normalize props in c2 to array for single-code-path iterating
-            if(p2.constructor !== Array)
-            {
-               p2 = [p2];
-            }
-            
-            // add unique properties from p2 to p1
-            for(var i in p2)
-            {
-               var p = p2[i];
-               if((p1.constructor !== Array && p1 !== p) ||
-                  (p1.constructor === Array && p1.indexOf(p) == -1))
-               {
-                  if(p1.constructor === Array)
-                  {
-                     p1.push(p);
-                  }
-                  else
-                  {
-                     p1 = c1[type] = [p1, p];
-                  }
-               }
-            }
+            merged['@coerce'][key] = ctx2['@coerce'][key];
          }
       }
-      
-      // add new types from new @coerce
-      for(var type in c2)
-      {
-         if(!(type in c1))
-         {
-            c1[type] = c2[type]; 
-         }
-      }
-      
-      // ensure there are no property duplicates in @coerce
-      var unique = {};
-      var dups = [];
-      for(var type in c1)
-      {
-         var p = c1[type];
-         if(p.constructor === String)
-         {
-            p = [p];
-         }
-         for(var i in p)
-         {
-            if(!(p[i] in unique))
-            {
-               unique[p[i]] = true;
-            }
-            else if(dups.indexOf(p[i]) == -1)
-            {
-               dups.push(p[i]);
-            }
-         }
-      }
-
-      if(dups.length > 0)
-      {
-         throw {
-            message: 'Invalid type coercion specification. More than one ' +
-               'type specified for at least one property.',
-            duplicates: dups
-         };
-      }
-      
-      merged['@coerce'] = c1;
    }
 
    return merged;
@@ -1089,48 +1018,20 @@
    // check type coercion for property
    else if('@coerce' in ctx)
    {
-      // force compacted property
+      // look up compacted property in coercion map
       p = _compactIri(ctx, p, null);
-      
-      for(var type in ctx['@coerce'])
+      if(p in ctx['@coerce'])
       {
-         // get coerced properties (normalize to an array)
-         var props = ctx['@coerce'][type];
-         if(props.constructor !== Array)
+         // property found, return expanded type
+         var type = ctx['@coerce'][p];
+         rval = _expandTerm(ctx, type, usedCtx);
+         if(usedCtx !== null)
          {
-            props = [props];
-         }
-         
-         // look for the property in the array
-         for(var i in props)
-         {
-            // property found
-            if(props[i] === p)
+            if(!('@coerce' in usedCtx))
             {
-               rval = _expandTerm(ctx, type, usedCtx);
-               if(usedCtx !== null)
-               {
-                  if(!('@coerce' in usedCtx))
-                  {
-                     usedCtx['@coerce'] = {};
-                  }
-                  
-                  if(!(type in usedCtx['@coerce']))
-                  {
-                     usedCtx['@coerce'][type] = p;
-                  }
-                  else
-                  {
-                     var c = usedCtx['@coerce'][type];
-                     if((c.constructor === Array && c.indexOf(p) == -1) ||
-                        (c.constructor === String && c !== p))
-                     {
-                        _setProperty(usedCtx['@coerce'], type, p);
-                     }
-                  }
-               }
-               break;
+               usedCtx['@coerce'] = {};
             }
+            usedCtx['@coerce'][p] = type;
          }
       }
    }
--- a/playground/playground-examples.js	Sun Oct 16 18:57:13 2011 -0400
+++ b/playground/playground-examples.js	Thu Oct 20 19:43:53 2011 -0400
@@ -24,7 +24,7 @@
          "xsd": "http://www.w3.org/2001/XMLSchema#",
          "@coerce":
          {
-            "@iri": ["homepage"]
+            "homepage": "@iri"
          }
       }
    };
@@ -51,8 +51,9 @@
          "xsd": "http://www.w3.org/2001/XMLSchema#",
          "@coerce":
          {
-            "@iri": ["image"],
-            "xsd:float": ["latitude", "longitude"]
+            "image": "@iri",
+            "latitude": "xsd:float",
+            "longitude": "xsd:float"
          }
       }
    };
@@ -69,7 +70,7 @@
          "xsd": "http://www.w3.org/2001/XMLSchema#",
          "@coerce":
          {
-            "xsd:dateTime": ["ical:dtstart"]
+            "ical:dtstart": "xsd:dateTime"
          }
       }
    };
@@ -102,8 +103,10 @@
          "xsd": "http://www.w3.org/2001/XMLSchema#",
          "@coerce":
          {
-            "@iri": ["foaf:page", "gr:acceptedPaymentMethods", "gr:hasBusinessFunction"],
-            "xsd:float": ["gr:hasCurrencyValue"]
+            "foaf:page": "@iri",
+            "gr:acceptedPaymentMethods": "@iri",
+            "gr:hasBusinessFunction": "@iri",
+            "gr:hasCurrencyValue": "xsd:float"
          }
       }
    };
@@ -150,7 +153,7 @@
          "xsd": "http://www.w3.org/2001/XMLSchema#",
          "@coerce":
          {
-            "xsd:integer": ["step"]
+            "step": "xsd:integer"
          }
       }
    };
@@ -182,7 +185,7 @@
       {
          "@coerce": 
          {
-            "@iri": "ex:contains"
+            "ex:contains": "@iri"
          },
          "dc": "http://purl.org/dc/elements/1.1/",
          "ex": "http://example.org/vocab#",