Skip to content

Commit

Permalink
## 1.0.33
Browse files Browse the repository at this point in the history
- `Entity` & `EntityHandler`:
  - Added support for enums.
- Added `enumToName` and `enumFromName`
- reflection_factory: ^1.0.18
  • Loading branch information
gmpassos committed Nov 10, 2021
1 parent 0e85828 commit 78f045b
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 76 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.0.33

- `Entity` & `EntityHandler`:
- Added support for enums.
- Added `enumToName` and `enumFromName`
- reflection_factory: ^1.0.18

## 1.0.32

- `Json`:
Expand Down
6 changes: 3 additions & 3 deletions bones_api.iml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/coverage" />
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/.pub" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Dart Packages" level="project" />
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 @@ -16,7 +16,7 @@ import 'bones_api_utils.dart';
/// Root class of an API.
abstract class APIRoot {
// ignore: constant_identifier_names
static const String VERSION = '1.0.32';
static const String VERSION = '1.0.33';

static final Map<String, APIRoot> _instances = <String, APIRoot>{};

Expand Down
15 changes: 10 additions & 5 deletions lib/src/bones_api_condition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,23 @@ class ConditionParameter extends ConditionElement {

if (myValue is Iterable) {
if (otherValue is Iterable) {
var equals = isEqualsIterableDeep(myValue, otherValue);
var equals = isEqualsIterableDeep(myValue, otherValue,
valueEquality: EntityHandler.equalsValuesBasic);
return equals;
} else {
var contains = myValue.contains(otherValue);
var contains = myValue
.where((v) => EntityHandler.equalsValuesBasic(v, otherValue))
.isNotEmpty;
return contains;
}
} else if (otherValue is Iterable) {
var contains = otherValue.contains(myValue);
var contains = otherValue
.where((v) => EntityHandler.equalsValuesBasic(v, myValue))
.isNotEmpty;
return contains;
}

return myValue == otherValue;
return EntityHandler.equalsValuesBasic(myValue, otherValue);
}

bool matchesIn(List values,
Expand Down Expand Up @@ -556,7 +561,7 @@ class ConditionID<O> extends Condition<O> {
positionalParameters: positionalParameters,
namedParameters: namedParameters);
} else {
return id == idValue;
return EntityHandler.equalsValuesBasic(id, idValue);
}
}

Expand Down
111 changes: 92 additions & 19 deletions lib/src/bones_api_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,22 @@ abstract class EntityHandler<O> with FieldsFromMap {
var entityRepository =
valEntityHandler?.getEntityRepository(type: type.type);

if (entityRepository == null) {
throw StateError("Can't resolve value for type: $type -> $value");
if (entityRepository != null) {
var retEntity = entityRepository.selectByID(value);
return retEntity.resolveMapped((val) => val as T?);
}

var retEntity = entityRepository.selectByID(value);
return retEntity.resolveMapped((val) => val as T?);
try {
var value2 = Json.fromJson(value, type: type.type);

if (value2 != null) {
return value2 as T?;
} else {
return value as T?;
}
} catch (e) {
throw StateError("ERROR resolving value for type: $type -> $value. $e");
}
} else {
return type.parse<T>(value);
}
Expand Down Expand Up @@ -386,23 +396,18 @@ abstract class EntityHandler<O> with FieldsFromMap {
}

bool equalsValues(Object? value1, Object? value2) {
//if (identical(value1, value2)) return true ;

if (value1 == null) return value2 == null;
if (value2 == null) return false;
if (identical(value1, value2) || value1 == value2) return true;

if (value1 is DateTime || value2 is DateTime) {
var t1 = TypeParser.parseInt(value1);
var t2 = TypeParser.parseInt(value2);
return t1 == t2;
}
var equals = equalsValuesDateTime(value1, value2);
if (equals != null) return equals;

var primitive1 = TypeParser.isPrimitiveValue(value1);
var primitive2 = TypeParser.isPrimitiveValue(value2);
equals = equalsValuesPrimitive(value1, value2);
if (equals != null) return equals;

if (primitive1 && primitive2) {
return value1 == value2;
}
equals = equalsValuesEnum(value1, value2);
if (equals != null) return equals;

var collection1 = TypeParser.isCollectionValue(value1);
var collection2 = TypeParser.isCollectionValue(value2);
Expand All @@ -411,8 +416,8 @@ abstract class EntityHandler<O> with FieldsFromMap {
return isEqualsDeep(value1, value2, valueEquality: equalsValues);
}

var maybeEntity1 = !primitive1 && !collection1;
var maybeEntity2 = !primitive2 && !collection2;
var maybeEntity1 = !collection1 && !TypeParser.isPrimitiveValue(value1);
var maybeEntity2 = !collection2 && !TypeParser.isPrimitiveValue(value2);

var entityHandler1 = maybeEntity1 ? getEntityHandler(obj: value1) : null;
var entityHandler2 = maybeEntity2 ? getEntityHandler(obj: value2) : null;
Expand All @@ -438,7 +443,75 @@ abstract class EntityHandler<O> with FieldsFromMap {
}
}

return value1 == value2;
return false;
}

static bool equalsValuesBasic(Object? value1, Object? value2) {
if (value1 == null) return value2 == null;
if (value2 == null) return false;
if (identical(value1, value2) || value1 == value2) return true;

var equals = equalsValuesDateTime(value1, value2);
if (equals != null) return equals;

equals = equalsValuesPrimitive(value1, value2);
if (equals != null) return equals;

equals = equalsValuesEnum(value1, value2);
if (equals != null) return equals;

equals = equalsValuesCollection(value1, value2);
if (equals != null) return equals;

return false;
}

static bool? equalsValuesDateTime(Object value1, Object value2) {
if (value1 is DateTime || value2 is DateTime) {
var t1 = TypeParser.parseInt(value1);
var t2 = TypeParser.parseInt(value2);
return t1 == t2;
}

return null;
}

static bool? equalsValuesPrimitive(Object value1, Object value2) {
var primitive1 = TypeParser.isPrimitiveValue(value1);
var primitive2 = TypeParser.isPrimitiveValue(value2);

if (primitive1 && primitive2) {
return value1 == value2;
}

return null;
}

static bool? equalsValuesEnum(Object value1, Object value2) {
var enum1 = value1 is Enum;
var enum2 = value2 is Enum;

if (enum1 && enum2) {
return value1 == value2;
} else if (enum1 || enum2) {
var enumName1 = value1 is Enum ? enumToName(value1) : value1.toString();
var enumName2 = value2 is Enum ? enumToName(value2) : value2.toString();

return equalsIgnoreAsciiCase(enumName1, enumName2);
}

return null;
}

static bool? equalsValuesCollection(Object value1, Object value2) {
var collection1 = TypeParser.isCollectionValue(value1);
var collection2 = TypeParser.isCollectionValue(value2);

if (collection1 && collection2) {
return isEqualsDeep(value1, value2, valueEquality: equalsValuesBasic);
}

return null;
}

FutureOr<O> createFromMap(Map<String, dynamic> fields);
Expand Down
19 changes: 18 additions & 1 deletion lib/src/bones_api_entity_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,23 @@ abstract class SQLAdapter<C> extends SchemeProvider
/// If `true` indicates that this adapter SQL uses the `ON CONFLICT` syntax for inserts.
bool get sqlAcceptsInsertOnConflict;

Object? valueToSQL(Object? value) {
if (value == null) {
return null;
} else if (value is Enum) {
var enumType = value.runtimeType;
var enumReflection =
ReflectionFactory().getRegisterEnumReflection(enumType);

var name = enumReflection?.getName(value);
name ??= enumToName(value);

return name;
} else {
return value;
}
}

FutureOr<String> fieldValueToSQL(
EncodingContext context,
TableScheme tableScheme,
Expand All @@ -330,7 +347,7 @@ abstract class SQLAdapter<C> extends SchemeProvider
var fieldRef = tableScheme.getFieldsReferencedTables(fieldName);

if (fieldRef == null) {
fieldsValues.putIfAbsent(fieldName, () => value);
fieldsValues.putIfAbsent(fieldName, () => valueToSQL(value));
var valueSQL = _conditionSQLGenerator.parameterPlaceholder(fieldName);
return valueSQL;
} else {
Expand Down
19 changes: 19 additions & 0 deletions lib/src/bones_api_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,25 @@ bool isEqualsMapDeep(Map? m1, Map? m2, {ValueEquality? valueEquality}) {
return true;
}

/// Returns an [Enum] name.
String enumToName(Enum enumValue) {
var s = enumValue.toString();
var idx = s.indexOf('.');
var name = s.substring(idx + 1);
return name;
}

/// Returns an [Enum] from [enumValues] that matches [name].
Enum? enumFromName(String name, Iterable<Enum> enumValues) {
for (var e in enumValues) {
var n = enumToName(e);
if (n == name) {
return e;
}
}
return null;
}

typedef InstanceInfoExtractor<O, I> = I Function(O o);

/// Tracks an instance with a info relationship.
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: bones_api
description: Bones_API - A Powerful API backend framework for Dart. Comes with a built-in HTTP Server, routes handler, entity handler, SQL translator, and DB adapters.
version: 1.0.32
version: 1.0.33
homepage: https://github.com/Colossus-Services/bones_api

environment:
Expand All @@ -14,7 +14,7 @@ dependencies:
async_extension: ^1.0.9
dart_spawner: ^1.0.5
args: ^2.2.0
reflection_factory: ^1.0.17
reflection_factory: ^1.0.18
petitparser: ^4.2.0
hotreloader: ^3.0.1
logging: ^1.0.1
Expand Down
Loading

0 comments on commit 78f045b

Please sign in to comment.