| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of gcloud.db; | 5 part of gcloud.db; |
| 6 | 6 |
| 7 /// An implementation of [ModelDB] based on model class annotations. | 7 /// An implementation of [ModelDB] based on model class annotations. |
| 8 /// | 8 /// |
| 9 /// The two constructors will scan loaded dart libraries for classes with a | 9 /// The two constructors will scan loaded dart libraries for classes with a |
| 10 /// [Kind] annotation. | 10 /// [Kind] annotation. |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 'The class $type was not associated with a kind.'); | 137 'The class $type was not associated with a kind.'); |
| 138 } | 138 } |
| 139 return kind; | 139 return kind; |
| 140 } | 140 } |
| 141 | 141 |
| 142 /// Returns the name of the property corresponding to the kind [kind] and | 142 /// Returns the name of the property corresponding to the kind [kind] and |
| 143 /// [fieldName]. | 143 /// [fieldName]. |
| 144 String fieldNameToPropertyName(String kind, String fieldName) { | 144 String fieldNameToPropertyName(String kind, String fieldName) { |
| 145 var modelDescription = _kind2ModelDesc[kind]; | 145 var modelDescription = _kind2ModelDesc[kind]; |
| 146 if (modelDescription == null) { | 146 if (modelDescription == null) { |
| 147 throw new ArgumentError('The kind $kind is unknown.'); | 147 throw new ArgumentError('The kind "$kind" is unknown.'); |
| 148 } | 148 } |
| 149 return modelDescription.fieldNameToPropertyName(fieldName); | 149 return modelDescription.fieldNameToPropertyName(fieldName); |
| 150 } | 150 } |
| 151 | 151 |
| 152 /// Converts [value] according to the [Property] named [name] in [type]. |
| 153 Object toDatastoreValue(String kind, String fieldName, Object value) { |
| 154 var modelDescription = _kind2ModelDesc[kind]; |
| 155 if (modelDescription == null) { |
| 156 throw new ArgumentError('The kind "$kind" is unknown.'); |
| 157 } |
| 158 return modelDescription.encodeField(this, fieldName, value); |
| 159 } |
| 152 | 160 |
| 153 Iterable<_ModelDescription> get _modelDescriptions { | 161 Iterable<_ModelDescription> get _modelDescriptions { |
| 154 return _modelDesc2Type.values; | 162 return _modelDesc2Type.values; |
| 155 } | 163 } |
| 156 | 164 |
| 157 Map<String, Property> _propertiesForModel( | 165 Map<String, Property> _propertiesForModel( |
| 158 _ModelDescription modelDescription) { | 166 _ModelDescription modelDescription) { |
| 159 return _modelDesc2Properties[modelDescription]; | 167 return _modelDesc2Properties[modelDescription]; |
| 160 } | 168 } |
| 161 | 169 |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 } | 444 } |
| 437 | 445 |
| 438 String fieldNameToPropertyName(String fieldName) { | 446 String fieldNameToPropertyName(String fieldName) { |
| 439 return _field2PropertyName[fieldName]; | 447 return _field2PropertyName[fieldName]; |
| 440 } | 448 } |
| 441 | 449 |
| 442 String propertyNameToFieldName(ModelDBImpl db, String propertySearchName) { | 450 String propertyNameToFieldName(ModelDBImpl db, String propertySearchName) { |
| 443 return _property2FieldName[propertySearchName]; | 451 return _property2FieldName[propertySearchName]; |
| 444 } | 452 } |
| 445 | 453 |
| 446 Object encodeField(ModelDBImpl db, String fieldName, Object value) { | 454 Object encodeField(ModelDBImpl db, String fieldName, Object value, |
| 455 {bool enforceFieldExists: true}) { |
| 447 Property property = db._propertiesForModel(this)[fieldName]; | 456 Property property = db._propertiesForModel(this)[fieldName]; |
| 448 if (property != null) return property.encodeValue(db, value); | 457 if (property != null) { |
| 458 return property.encodeValue(db, value); |
| 459 } |
| 460 if (enforceFieldExists) { |
| 461 throw new ArgumentError( |
| 462 'A field named "$fieldName" does not exist in kind "$kind".'); |
| 463 } |
| 449 return null; | 464 return null; |
| 450 } | 465 } |
| 451 } | 466 } |
| 452 | 467 |
| 453 // NOTE/TODO: | 468 // NOTE/TODO: |
| 454 // Currently expanded properties are only | 469 // Currently expanded properties are only |
| 455 // * decoded if there are no clashes in [usedNames] | 470 // * decoded if there are no clashes in [usedNames] |
| 456 // * encoded if there are no clashes in [usedNames] | 471 // * encoded if there are no clashes in [usedNames] |
| 457 // We might want to throw an error if there are clashes, because otherwise | 472 // We might want to throw an error if there are clashes, because otherwise |
| 458 // - we may end up removing properties after a read-write cycle | 473 // - we may end up removing properties after a read-write cycle |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 | 524 |
| 510 String propertyNameToFieldName(ModelDBImpl db, String propertyName) { | 525 String propertyNameToFieldName(ModelDBImpl db, String propertyName) { |
| 511 String fieldName = super.propertyNameToFieldName(db, propertyName); | 526 String fieldName = super.propertyNameToFieldName(db, propertyName); |
| 512 // If the ModelDescription doesn't know about [propertyName], it's an | 527 // If the ModelDescription doesn't know about [propertyName], it's an |
| 513 // expanded property, where propertyName == fieldName. | 528 // expanded property, where propertyName == fieldName. |
| 514 if (fieldName == null) fieldName = propertyName; | 529 if (fieldName == null) fieldName = propertyName; |
| 515 return fieldName; | 530 return fieldName; |
| 516 } | 531 } |
| 517 | 532 |
| 518 Object encodeField(ModelDBImpl db, String fieldName, Object value) { | 533 Object encodeField(ModelDBImpl db, String fieldName, Object value) { |
| 519 Object primitiveValue = super.encodeField(db, fieldName, value); | 534 Object primitiveValue = super.encodeField(db, fieldName, value, |
| 535 enforceFieldExists: false); |
| 520 // If superclass can't encode field, we return value here (and assume | 536 // If superclass can't encode field, we return value here (and assume |
| 521 // it's primitive) | 537 // it's primitive) |
| 522 // NOTE: Implicit assumption: | 538 // NOTE: Implicit assumption: |
| 523 // If value != null then superclass will return != null. | 539 // If value != null then superclass will return != null. |
| 524 // TODO: Ensure [value] is primitive in this case. | 540 // TODO: Ensure [value] is primitive in this case. |
| 525 if (primitiveValue == null) primitiveValue = value; | 541 if (primitiveValue == null) primitiveValue = value; |
| 526 return primitiveValue; | 542 return primitiveValue; |
| 527 } | 543 } |
| 528 } | 544 } |
| OLD | NEW |