@@ -45,7 +45,7 @@ var InvalidKeyError = createErrorClass('InvalidKey', function(opts) {
4545entity . KEY_SYMBOL = Symbol ( 'KEY' ) ;
4646
4747/**
48- * Build a Datastore Double object.
48+ * Build a Datastore Double object. For long doubles, a string can be provided.
4949 *
5050 * @constructor
5151 * @param {number } value - The double value.
@@ -60,16 +60,16 @@ function Double(value) {
6060entity . Double = Double ;
6161
6262/**
63- * Build a Datastore Int object.
63+ * Build a Datastore Int object. For long integers, a string can be provided.
6464 *
6565 * @constructor
66- * @param {number } value - The integer value.
66+ * @param {number|string } value - The integer value.
6767 *
6868 * @example
6969 * var anInt = new Int(7);
7070 */
7171function Int ( value ) {
72- this . value = value ;
72+ this . value = value . toString ( ) ;
7373}
7474
7575entity . Int = Int ;
@@ -116,8 +116,8 @@ function Key(options) {
116116 if ( options . path . length % 2 === 0 ) {
117117 var identifier = options . path . pop ( ) ;
118118
119- if ( is . number ( identifier ) ) {
120- this . id = identifier ;
119+ if ( is . number ( identifier ) || identifier instanceof entity . Int ) {
120+ this . id = identifier . value || identifier ;
121121 } else if ( is . string ( identifier ) ) {
122122 this . name = identifier ;
123123 }
@@ -467,11 +467,15 @@ function keyFromKeyProto(keyProto) {
467467 }
468468
469469 keyProto . path . forEach ( function ( path , index ) {
470- var id = path . name || Number ( path . id ) ;
471-
472470 keyOptions . path . push ( path . kind ) ;
473471
474- if ( id ) {
472+ var id = path [ path . id_type ] ;
473+
474+ if ( path . id_type === 'id' ) {
475+ id = new entity . Int ( id ) ;
476+ }
477+
478+ if ( is . defined ( id ) ) {
475479 keyOptions . path . push ( id ) ;
476480 } else if ( index < keyProto . path . length - 1 ) {
477481 throw new InvalidKeyError ( {
@@ -503,7 +507,7 @@ entity.keyFromKeyProto = keyFromKeyProto;
503507 * // }
504508 */
505509function keyToKeyProto ( key ) {
506- if ( ! is . string ( key . path [ 0 ] ) ) {
510+ if ( is . undefined ( key . kind ) ) {
507511 throw new InvalidKeyError ( {
508512 code : 'MISSING_KIND'
509513 } ) ;
@@ -519,28 +523,31 @@ function keyToKeyProto(key) {
519523 } ;
520524 }
521525
522- for ( var i = 0 ; i < key . path . length ; i += 2 ) {
523- var pathElement = {
524- kind : key . path [ i ]
525- } ;
526+ var numKeysWalked = 0 ;
526527
527- var value = key . path [ i + 1 ] ;
528-
529- if ( value ) {
530- if ( is . number ( value ) ) {
531- pathElement . id = value ;
532- } else {
533- pathElement . name = value ;
534- }
535- } else if ( i < key . path . length - 2 ) {
528+ // Reverse-iterate over the Key objects.
529+ do {
530+ if ( numKeysWalked > 0 && is . undefined ( key . id ) && is . undefined ( key . name ) ) {
536531 // This isn't just an incomplete key. An ancestor key is incomplete.
537532 throw new InvalidKeyError ( {
538533 code : 'MISSING_ANCESTOR_ID'
539534 } ) ;
540535 }
541536
542- keyProto . path . push ( pathElement ) ;
543- }
537+ var pathElement = {
538+ kind : key . kind
539+ } ;
540+
541+ if ( is . defined ( key . id ) ) {
542+ pathElement . id = key . id ;
543+ }
544+
545+ if ( is . defined ( key . name ) ) {
546+ pathElement . name = key . name ;
547+ }
548+
549+ keyProto . path . unshift ( pathElement ) ;
550+ } while ( ( key = key . parent ) && ++ numKeysWalked ) ;
544551
545552 return keyProto ;
546553}
0 commit comments