Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## 1.9.2

- `FieldsFromMap`:
- `resolveFiledName`: improve field matching.

- `EntityHandler`
- `getFieldType`: added parameter `resolveFiledName: true`.
- Optimize field and types resolution.

- `GenericEntityHandler`, `ClassReflectionEntityHandler`:
- Optimize field and types resolution.

## 1.9.1

- async_events: ^1.3.0
Expand Down
2 changes: 1 addition & 1 deletion lib/src/bones_api_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef APILogger = void Function(APIRoot apiRoot, String type, String? message,
/// Bones API Library class.
class BonesAPI {
// ignore: constant_identifier_names
static const String VERSION = '1.9.1';
static const String VERSION = '1.9.2';

static bool _boot = false;

Expand Down
7 changes: 4 additions & 3 deletions lib/src/bones_api_condition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,8 @@
TypeInfo? _resolveValueType(String? keyName, TypeInfo? objType,
EntityHandler? objEntityHandler, Object? value) {
if (keyName != null) {
var type = objEntityHandler?.getFieldType(null, keyName);
var type =
objEntityHandler?.getFieldType(null, keyName, resolveFiledName: true);
if (type != null) return type;
}

Expand All @@ -1257,8 +1258,8 @@
return objType ?? TypeInfo.from(value);
}

var t = objEntityHandler?.getEntityHandler(obj: value)?.type;
return t != null ? TypeInfo.fromType(t) : null;
var t = objEntityHandler?.getEntityHandler(obj: value)?.typeInfo;

Check warning on line 1261 in lib/src/bones_api_condition.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_condition.dart#L1261

Added line #L1261 was not covered by tests
return t;
}
}

Expand Down
193 changes: 127 additions & 66 deletions lib/src/bones_api_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@

Type? _idType;

Type idType([O? o]) => _idType ??= getFieldType(o, idFieldName(o))!.type;
Type idType([O? o]) => _idType ??=
getFieldType(o, idFieldName(o), resolveFiledName: false)!.type;

String idFieldName([O? o]);

Expand Down Expand Up @@ -741,11 +742,12 @@
}

FutureOr<dynamic> resolveEntityFieldValue(O o, String key, dynamic value,
{EntityProvider? entityProvider,
{bool resolveFiledName = false,
EntityProvider? entityProvider,
EntityCache? entityCache,
EntityResolutionRules? resolutionRules}) {
entityCache ??= JsonEntityCacheSimple();
var fieldType = getFieldType(o, key);
var fieldType = getFieldType(o, key, resolveFiledName: resolveFiledName);
return resolveValueByType(fieldType, value,
entityProvider: entityProvider,
entityCache: entityCache,
Expand Down Expand Up @@ -1362,39 +1364,49 @@
return _fieldsNamesSimple!;
}

TypeInfo? getFieldType(O? o, String key);
TypeInfo? getFieldType(O? o, String key, {bool resolveFiledName = false});

Map<String, TypeInfo>? _fieldsTypes;

Map<String, TypeInfo> getFieldsTypes([O? o]) {
return Map<String, TypeInfo>.fromEntries(fieldsNames(o)
.map((key) => MapEntry<String, TypeInfo>(key, getFieldType(o, key)!)));
return _fieldsTypes ??= Map.unmodifiable(
Map<String, TypeInfo>.fromEntries(
fieldsNames(o).map(
(key) => MapEntry<String, TypeInfo>(

Check warning on line 1375 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L1373-L1375

Added lines #L1373 - L1375 were not covered by tests
key,
getFieldType(o, key, resolveFiledName: false)!,

Check warning on line 1377 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L1377

Added line #L1377 was not covered by tests
),
),
),
);
}

Map<String, TypeInfo>? _fieldsEnumTypes;

Map<String, TypeInfo> getFieldsEnumTypes([O? o]) {
var enumFields = _fieldsEnumTypes;
if (enumFields != null) return UnmodifiableMapView(enumFields);
if (enumFields != null) return enumFields;

final reflectionFactory = ReflectionFactory();

enumFields = getFieldsTypes(o)
var mapEnumFields = getFieldsTypes(o)
.entries
.where((e) =>
reflectionFactory.getRegisterEnumReflection(e.value.type) != null)
.toMapFromEntries();

return UnmodifiableMapView(_fieldsEnumTypes = enumFields);
return _fieldsEnumTypes = Map.unmodifiable(mapEnumFields);
}

Map<String, TypeInfo>? _fieldsEntityTypes;

Map<String, TypeInfo> getFieldsEntityTypes([O? o]) {
var entityFields = _fieldsEntityTypes;
if (entityFields != null) return UnmodifiableMapView(entityFields);
if (entityFields != null) return entityFields;

var enumFields = getFieldsEnumTypes(o);

entityFields = getFieldsTypes()
var mapEntityFields = getFieldsTypes()
.entries
.map((e) {
var field = e.key;
Expand All @@ -1412,7 +1424,7 @@
.nonNulls
.toMapFromEntries();

return UnmodifiableMapView(_fieldsEntityTypes = entityFields);
return _fieldsEntityTypes = Map.unmodifiable(mapEntityFields);
}

List<EntityAnnotation>? getFieldEntityAnnotations(O? o, String key);
Expand Down Expand Up @@ -1495,7 +1507,7 @@
}

if (resolvedValue != null) {
var fieldType = getFieldType(o, key);
var fieldType = getFieldType(o, key, resolveFiledName: true);

if (fieldType != null) {
EntityHandler<Object>? fieldEntityHandler;
Expand Down Expand Up @@ -2146,6 +2158,7 @@

var setFutures = fieldsValues.entries.map((e) {
return setFieldValueDynamic(o, e.key, e.value,
resolveFiledName: false,
entityProvider: entityProvider,
entityCache: entityCache,
resolutionRules: resolutionRules)
Expand All @@ -2156,10 +2169,12 @@
}

FutureOr<dynamic> setFieldValueDynamic(O o, String key, dynamic value,
{EntityProvider? entityProvider,
{bool resolveFiledName = false,
EntityProvider? entityProvider,
EntityCache? entityCache,
EntityResolutionRules? resolutionRules}) {
var retValue2 = resolveEntityFieldValue(o, key, value,
resolveFiledName: resolveFiledName,
entityProvider: entityProvider,
entityCache: entityCache,
resolutionRules: resolutionRules);
Expand Down Expand Up @@ -2312,8 +2327,6 @@
return fieldsNames;
}

Map<String, TypeInfo>? _fieldsTypes;

@override
Map<String, TypeInfo> fieldsTypes([O? o]) {
var fieldsTypes = _fieldsTypes;
Expand Down Expand Up @@ -2344,19 +2357,28 @@
if (o != null && _idFieldsName == null) {
_idFieldsName = o.idFieldName;

_fieldsNames ??= List<String>.unmodifiable(o.fieldsNames);
final fieldsNames =
_fieldsNames ??= List<String>.unmodifiable(o.fieldsNames);

_fieldsTypes ??= Map<String, TypeInfo>.unmodifiable(
Map<String, TypeInfo>.fromEntries(
_fieldsNames!.map((f) => MapEntry(f, o!.getFieldType(f)!))));
Map<String, TypeInfo>.fromEntries(
fieldsNames.map((f) => MapEntry(f, o!.getFieldType(f)!)),
),
);

_fieldsEntityAnnotations ??=
Map<String, List<EntityAnnotation>>.unmodifiable(
Map<String, List<EntityAnnotation>>.fromEntries(
_fieldsNames!.map((f) {
var list = o!.getFieldEntityAnnotations(f);
return list == null ? null : MapEntry(f, UnmodifiableListView(list));
}).nonNulls));
Map<String, List<EntityAnnotation>>.fromEntries(
fieldsNames.map(
(f) {
var list = o!.getFieldEntityAnnotations(f);
return list == null
? null
: MapEntry(f, List<EntityAnnotation>.unmodifiable(list));
},
).nonNulls,
),
);
}
}

Expand Down Expand Up @@ -2409,13 +2431,33 @@
}

@override
TypeInfo? getFieldType(O? o, String key) {
TypeInfo? getFieldType(O? o, String key, {bool resolveFiledName = false}) {
inspectObject(o);
if (o != null) {
return o.getFieldType(key);
} else {
return _fieldsTypes?[key];

TypeInfo? fieldType;

final fieldsTypes = _fieldsTypes;
if (fieldsTypes != null) {
fieldType = fieldsTypes[key];

if (fieldType == null && resolveFiledName) {
var resolvedFieldName = this.resolveFiledName(fieldsNames(), key);

Check warning on line 2444 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2444

Added line #L2444 was not covered by tests
if (resolvedFieldName != null) {
fieldType = fieldsTypes[resolvedFieldName];

Check warning on line 2446 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2446

Added line #L2446 was not covered by tests
}
}
} else if (o != null) {
fieldType = o.getFieldType(key);

Check warning on line 2450 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2450

Added line #L2450 was not covered by tests

if (fieldType == null && resolveFiledName) {
var resolvedFieldName = this.resolveFiledName(fieldsNames(), key);

Check warning on line 2453 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2453

Added line #L2453 was not covered by tests
if (resolvedFieldName != null) {
fieldType = o.getFieldType(key);

Check warning on line 2455 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2455

Added line #L2455 was not covered by tests
}
}
}

return fieldType;
}

@override
Expand Down Expand Up @@ -2535,9 +2577,35 @@
reflection.getJsonFieldsVisibleValues(o);

@override
TypeInfo? getFieldType(O? o, String key) {
var field = reflection.field(key, o);
return field != null ? TypeInfo.from(field) : null;
TypeInfo? getFieldType(O? o, String key, {bool resolveFiledName = false}) {
TypeInfo? fieldType;

final fieldsTypes = _fieldsTypes;
if (fieldsTypes != null) {
fieldType = fieldsTypes[key];

if (fieldType == null && resolveFiledName) {
var resolvedFieldName = this.resolveFiledName(fieldsNames(), key);

Check warning on line 2588 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2588

Added line #L2588 was not covered by tests
if (resolvedFieldName != null) {
fieldType = fieldsTypes[resolvedFieldName];

Check warning on line 2590 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2590

Added line #L2590 was not covered by tests
}
}
} else {
var field = reflection.field(key, o);

Check warning on line 2594 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2594

Added line #L2594 was not covered by tests

if (field == null && resolveFiledName) {
var resolvedFieldName = this.resolveFiledName(fieldsNames(), key);

Check warning on line 2597 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2597

Added line #L2597 was not covered by tests
if (resolvedFieldName != null) {
field = reflection.field(resolvedFieldName, o);

Check warning on line 2599 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2599

Added line #L2599 was not covered by tests
}
}

if (field != null) {
fieldType = TypeInfo.from(field);

Check warning on line 2604 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2604

Added line #L2604 was not covered by tests
}
}

return fieldType;
}

List<Object>? getFieldAnnotations(O? o, String key) {
Expand Down Expand Up @@ -2686,45 +2754,37 @@
List<String> fieldsNames([O? o]) =>
_fieldsNames ??= List.unmodifiable(fieldsTypes(o).keys);

Map<String, TypeInfo>? _fieldsTypes;

@override
Map<String, TypeInfo> fieldsTypes([O? o]) {
if (_fieldsTypes == null) {
var reflection = reflectionWithObject(o);

var types = reflection.fieldsNames.map((f) {
var field = reflection.field(f, o);
if (field == null || !field.hasSetter) return null;
var type = TypeInfo.from(field);
return MapEntry(f, type);
});

_fieldsTypes = UnmodifiableMapView<String, TypeInfo>(
Map<String, TypeInfo>.fromEntries(types.nonNulls));
}
return _fieldsTypes!;
return _fieldsTypes ??= Map<String, TypeInfo>.unmodifiable(
Map<String, TypeInfo>.fromEntries(
reflectionWithObject(o).fieldsNames.map(
(f) {
var field = reflection.field(f, o);
if (field == null || !field.hasSetter) return null;
var type = TypeInfo.from(field);
return MapEntry(f, type);
},
).nonNulls,
),
);
}

Map<String, Map<String, TypeInfo>>? _constructors;

@override
Map<String, Map<String, TypeInfo>>? constructors([O? o]) {
if (_constructors == null) {
var reflection = reflectionWithObject(o);

var map = reflection.allConstructors().map((c) {
var name = c.name;
var args = c.allParameters
.map((p) => MapEntry(p.jsonName, p.type.typeInfo))
.toMapFromEntries();
return MapEntry(name, UnmodifiableMapView(args));
}).toMapFromEntries();

_constructors = UnmodifiableMapView(map);
}

return _constructors!;
return _constructors ??= Map.unmodifiable(
reflectionWithObject(o).allConstructors().map(
(c) {
var name = c.name;
var args = c.allParameters
.map((p) => MapEntry(p.jsonName, p.type.typeInfo))
.toMapFromEntries();
return MapEntry(name, Map.unmodifiable(args));

Check warning on line 2784 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2777-L2784

Added lines #L2777 - L2784 were not covered by tests
},
).toMapFromEntries(),

Check warning on line 2786 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L2786

Added line #L2786 was not covered by tests
);
}

@override
Expand Down Expand Up @@ -6479,7 +6539,7 @@
{TypeInfo? fieldType, Transaction? transaction}) {
checkNotClosed();

fieldType ??= entityHandler.getFieldType(o, field)!;
fieldType ??= entityHandler.getFieldType(o, field, resolveFiledName: true)!;

Check warning on line 6542 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6542

Added line #L6542 was not covered by tests

if (!fieldType.isListEntityOrReference) {
throw StateError("Field `$field` not a `List` entity type: $fieldType");
Expand Down Expand Up @@ -6512,7 +6572,7 @@
{Object? oId, TypeInfo? fieldType, Transaction? transaction}) {
checkNotClosed();

fieldType ??= entityHandler.getFieldType(o, field)!;
fieldType ??= entityHandler.getFieldType(o, field, resolveFiledName: true)!;

Check warning on line 6575 in lib/src/bones_api_entity.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity.dart#L6575

Added line #L6575 was not covered by tests

if (!fieldType.isListEntityOrReference) {
throw StateError("Field `$field` not a `List` entity type: $fieldType");
Expand Down Expand Up @@ -6607,7 +6667,8 @@
var value = entityHandler.getField(o, fieldName);
if (value == null) return null;

var fieldType = entityHandler.getFieldType(o, fieldName)!;
var fieldType =
entityHandler.getFieldType(o, fieldName, resolveFiledName: false)!;

if (!EntityHandler.isValidEntityType(fieldType.type)) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion lib/src/bones_api_entity_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ abstract class DBAdapter<C extends Object> extends SchemeProvider
entityName: entityName, tableName: tableName, entityType: entityType);

if (entityRepository != null) {
var type = entityRepository.entityHandler.getFieldType(null, field);
var type = entityRepository.entityHandler
.getFieldType(null, field, resolveFiledName: true);
return type;
}

Expand Down
Loading