Skip to content

Commit

Permalink
Merge v2.3.2
Browse files Browse the repository at this point in the history
 - `ClassReflection`:
   - Added `getJsonFieldsVisibleValues`

 - meta: ^1.14.0
 - build_runner: ^2.4.9
  • Loading branch information
gmpassos committed Apr 5, 2024
2 parents a091e7c + a5387bd commit 0c8efcd
Show file tree
Hide file tree
Showing 13 changed files with 387 additions and 85 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.3.2

- `ClassReflection`:
- Added `getJsonFieldsVisibleValues`

- meta: ^1.14.0
- build_runner: ^2.4.9

## 2.3.1

- `JsonEncoder`:
Expand Down
5 changes: 3 additions & 2 deletions example/reflection_factory_bridge_example.reflection.g.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// GENERATED CODE - DO NOT MODIFY BY HAND!
// BUILDER: reflection_factory/2.3.1
// BUILDER: reflection_factory/2.3.2
// BUILD COMMAND: dart run build_runner build
//

Expand All @@ -20,7 +20,7 @@ typedef __TI<T> = TypeInfo<T>;
typedef __PR = ParameterReflection;

mixin __ReflectionMixin {
static final Version _version = Version.parse('2.3.1');
static final Version _version = Version.parse('2.3.2');

Version get reflectionFactoryVersion => _version;

Expand Down Expand Up @@ -249,6 +249,7 @@ class User$reflection extends ClassReflection<User> with __ReflectionMixin {

@override
Map<String, dynamic> getFieldsValues(User? obj, {bool withHashCode = false}) {
obj ??= object;
return <String, dynamic>{
'email': obj?.email,
'pass': obj?.pass,
Expand Down
5 changes: 3 additions & 2 deletions example/reflection_factory_example.reflection.g.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// GENERATED CODE - DO NOT MODIFY BY HAND!
// BUILDER: reflection_factory/2.3.1
// BUILDER: reflection_factory/2.3.2
// BUILD COMMAND: dart run build_runner build
//

Expand All @@ -20,7 +20,7 @@ typedef __TI<T> = TypeInfo<T>;
typedef __PR = ParameterReflection;

mixin __ReflectionMixin {
static final Version _version = Version.parse('2.3.1');
static final Version _version = Version.parse('2.3.2');

Version get reflectionFactoryVersion => _version;

Expand Down Expand Up @@ -249,6 +249,7 @@ class User$reflection extends ClassReflection<User> with __ReflectionMixin {

@override
Map<String, dynamic> getFieldsValues(User? obj, {bool withHashCode = false}) {
obj ??= object;
return <String, dynamic>{
'email': obj?.email,
'pass': obj?.pass,
Expand Down
8 changes: 7 additions & 1 deletion lib/src/reflection_factory_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import 'reflection_factory_utils.dart';
/// Class with all registered reflections ([ClassReflection]).
class ReflectionFactory {
// ignore: constant_identifier_names
static const String VERSION = '2.3.1';
static const String VERSION = '2.3.2';

static final ReflectionFactory _instance = ReflectionFactory._();

Expand Down Expand Up @@ -1435,6 +1435,12 @@ abstract class ClassReflection<O> extends Reflection<O>
/// Returns a [Map] with [allFields] values.
Map<String, dynamic> getFieldsValues(O? obj, {bool withHashCode = false});

/// Returns a [Map] with [allFields] values without [JsonField.hidden].
/// - Note that [JsonField.visible] is an alias to `JsonField(hidden: false)`.
Map<String, dynamic> getJsonFieldsVisibleValues(O? obj,
{bool withHashCode = false}) =>
getFieldsValues(obj, withHashCode: withHashCode);

/// Invokes the method for [methodName].
R? invokeMethod<R>(String methodName, Iterable<Object?>? positionalArguments,
[Map<Symbol, Object?>? namedArguments]) {
Expand Down
40 changes: 38 additions & 2 deletions lib/src/reflection_factory_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2043,16 +2043,52 @@ class _ClassTree<T> extends RecursiveElementVisitor<T> {
str.write(' @override\n');
str.write(
' Map<String,dynamic> getFieldsValues($className? obj, {bool withHashCode = false}) {');

str.write(' obj ??= object;\n');
str.write(' return <String,dynamic>{\n');
for (var fieldName in entries.keys) {
if (fieldName == 'hashCode') continue;
str.write(" '$fieldName': obj?.$fieldName,");
}
str.write(" if (withHashCode) 'hashCode': obj?.hashCode,");
str.write(' };\n');

str.write(' }\n\n');

var entriesWithJsonFieldHidden = entries.entries
.where((e) =>
e.key != 'hashCode' &&
e.value.annotations.any((a) {
var o = a.computeConstantValue();
if (o == null) return false;

var isJsonField =
o.type?.getDisplayString(withNullability: false) ==
'JsonField';
if (!isJsonField) return false;

var hidden = o.getField('_hidden')?.toBoolValue() ?? false;
return hidden;
}))
.toList();

if (entriesWithJsonFieldHidden.isNotEmpty) {
var hiddenKeys = entriesWithJsonFieldHidden.map((e) => e.key).toSet();
var visibleKeys =
entries.keys.where((k) => !hiddenKeys.contains(k)).toList();

str.write(' @override\n');
str.write(
' Map<String,dynamic> getJsonFieldsVisibleValues($className? obj, {bool withHashCode = false}) {');
str.write(' obj ??= object;\n');
str.write(' return <String,dynamic>{\n');
for (var fieldName in visibleKeys) {
if (fieldName == 'hashCode') continue;
str.write(" '$fieldName': obj?.$fieldName,");
}
str.write(" if (withHashCode) 'hashCode': obj?.hashCode,");
str.write(' };\n');

str.write(' }\n\n');
}
}

void _buildStaticFields(StringBuffer str) {
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: reflection_factory
description: Allows Dart reflection with an easy approach, even for third-party classes, using code generation portable for all Dart platforms.
version: 2.3.1
version: 2.3.2
homepage: https://github.com/gmpassos/reflection_factory

environment:
Expand All @@ -10,7 +10,7 @@ dependencies:
build: ^2.4.1
analyzer: ^6.4.1
dart_style: ^2.3.6
meta: ^1.12.0
meta: ^1.14.0
mime: ^1.0.5
base_codecs: ^1.0.1
pub_semver: ^2.1.4
Expand All @@ -20,7 +20,7 @@ dependencies:
source_span: ^1.10.0

dev_dependencies:
build_runner: ^2.4.8
build_runner: ^2.4.9
build_test: ^2.2.2
lints: ^3.0.0
pubspec: ^2.3.0
Expand Down
51 changes: 51 additions & 0 deletions test/reflection_factory_build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,51 @@ void main() {
group('ReflectionBuilder', () {
setUp(() {});

test('EnableReflection: TestEmpty', () async {
var builder = ReflectionBuilder(verbose: true);

var sourceAssets = {
'$_pkgName|lib/foo.dart': '''
import 'package:reflection_factory/reflection_factory.dart';
part 'foo.reflection.g.dart';
@EnableReflection()
class TestEmpty {}
'''
};

await testBuilder(
builder,
sourceAssets,
reader: await PackageAssetReader.currentIsolate(),
generateFor: {'$_pkgName|lib/foo.dart'},
outputs: {
'$_pkgName|lib/foo.reflection.g.dart': decodedMatches(allOf(
allOf(
contains('GENERATED CODE - DO NOT MODIFY BY HAND'),
contains(
'BUILDER: reflection_factory/${ReflectionFactory.VERSION}'),
contains("part of 'foo.dart'"),
contains(
"Version _version = Version.parse('${ReflectionFactory.VERSION}')"),
),
allOf(
contains('TestEmpty\$reflection'),
contains('TestEmpty\$reflectionExtension'),
isNot(contains(
"Map<String, dynamic> getJsonFieldsVisibleValues(TestEmpty? obj,")),
),
)),
},
onLog: (msg) {
print(msg);
},
);
});

test('EnableReflection: User', () async {
var builder = ReflectionBuilder(verbose: true);

Expand Down Expand Up @@ -43,7 +88,9 @@ void main() {
@TestAnnotation(['field', 'email'])
String? email ;
@JsonField.hidden()
String pass ;
@JsonField.visible()
bool enabled ;
User(this.email, {required this.pass, this.enabled = true});
Expand Down Expand Up @@ -129,6 +176,10 @@ void main() {
r'Map<String, dynamic>\?\s+toJsonMap\(\{bool duplicatedEntitiesAsID = false\}\)\s+=>\s+reflection.toJsonMap\(duplicatedEntitiesAsID: duplicatedEntitiesAsID\)')),
),
allOf(
contains("JsonField.hidden()"),
contains("JsonField.visible()"),
contains(
"Map<String, dynamic> getJsonFieldsVisibleValues(User? obj,"),
contains("case 'tojson':"),
contains("case 'getfield':"),
contains("case 'setfield':"),
Expand Down
57 changes: 34 additions & 23 deletions test/reflection_factory_json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void main() {
'enabled': true,
'isEnabled': true,
'name': 'Joe',
'password': '123'
//'password': '123'
}));

expect(
Expand All @@ -347,7 +347,7 @@ void main() {
'enabled': true,
'isEnabled': true,
'name': 'Smith',
'password': '456'
//'password': '456'
}));

expect(
Expand Down Expand Up @@ -1173,20 +1173,24 @@ void main() {
expect(
encodedJson,
equals(
'{"axis":"x","email":"joe@mail.com","enabled":true,"id":101,"isEnabled":true,"theLevel":null,"name":"joe","password":"123"}'));
'{"axis":"x","email":"joe@mail.com","enabled":true,"id":101,"isEnabled":true,"theLevel":null,"name":"joe"}'));

var decodedUser1 = jsonCodec.decode<TestUserWithReflection>(
'{"axis":"x","email":"joe@mail.com","enabled":true,"id":101,"isEnabled":true,"theLevel":null,"name":"joe","password":"123"}',
type: TestUserWithReflection);

var decodedUser1 =
jsonCodec.decode(encodedJson, type: TestUserWithReflection);
expect(jsonCodec.encode(decodedUser1), equals(encodedJson));

var encodedUser1b =
'{"axis":"x","email":"joe2@mail.com","enabled":true,"id":101,"isEnabled":true,"theLevel":null,"name":"joe","passphrase":"123456"}';
var decodedUser1b = jsonCodec.decode(encodedUser1b,
type: TestUserWithReflection) as TestUserWithReflection;

expect(decodedUser1b.email, equals('joe2@mail.com'));
expect(decodedUser1b.password, equals('123456'));

expect(jsonCodec.encode(decodedUser1b),
equals(encodedUser1b.replaceFirst('passphrase', 'password')));
equals(encodedUser1b.replaceFirst(',"passphrase":"123456"', '')));
}

{
Expand All @@ -1195,9 +1199,10 @@ void main() {
expect(
encodedJson,
equals(
'{"axis":"z","email":"joe@mail.com","enabled":false,"id":null,"isEnabled":false,"theLevel":999,"name":"joe","password":"123"}'));
'{"axis":"z","email":"joe@mail.com","enabled":false,"id":null,"isEnabled":false,"theLevel":999,"name":"joe"}'));

var decodedUser2 = jsonCodec.decode(encodedJson,
var decodedUser2 = jsonCodec.decode(
encodedJson.replaceFirst('"}', '","password":"123"}'),
type: TestUserWithReflection) as TestUserWithReflection;
expect(jsonCodec.encode(decodedUser2), equals(encodedJson));
}
Expand All @@ -1210,12 +1215,13 @@ void main() {
expect(
encodedJson,
equals('['
'{"axis":"x","email":"joe@mail.com","enabled":true,"id":101,"isEnabled":true,"theLevel":null,"name":"joe","password":"123"},'
'{"axis":"x","email":"joe@mail.com","enabled":true,"id":101,"isEnabled":true,"theLevel":null,"name":"joe","password":"123"}'
'{"axis":"x","email":"joe@mail.com","enabled":true,"id":101,"isEnabled":true,"theLevel":null,"name":"joe"},'
'{"axis":"x","email":"joe@mail.com","enabled":true,"id":101,"isEnabled":true,"theLevel":null,"name":"joe"}'
']'));

{
var decoded = jsonCodec.decode(encodedJson,
var decoded = jsonCodec.decode(
encodedJson.replaceAll('"}', '","password":"123"}'),
type: TestUserWithReflection) as List;

print(decoded);
Expand All @@ -1229,8 +1235,9 @@ void main() {
}

{
var decoded = JsonCodec(forceDuplicatedEntitiesAsID: true)
.decode(encodedJson, type: TestUserWithReflection) as List;
var decoded = JsonCodec(forceDuplicatedEntitiesAsID: true).decode(
encodedJson.replaceAll('"}', '","password":"123"}'),
type: TestUserWithReflection) as List;

print(decoded);

Expand All @@ -1252,12 +1259,14 @@ void main() {
expect(
encodedJson,
equals('['
'{"axis":"x","email":"joe@mail.com","enabled":true,"id":101,"isEnabled":true,"theLevel":null,"name":"joe","password":"123"},'
'{"axis":"x","email":"joe@mail.com","enabled":true,"id":101,"isEnabled":true,"theLevel":null,"name":"joe"},'
'101'
']'));

var decoded = jsonCodec.decode(encodedJson,
type: TestUserWithReflection, duplicatedEntitiesAsID: true) as List;
var decoded = jsonCodec.decode(
encodedJson.replaceAll('"}', '","password":"123"}'),
type: TestUserWithReflection,
duplicatedEntitiesAsID: true) as List;

print(decoded);

Expand Down Expand Up @@ -1459,13 +1468,14 @@ void main() {
expect(
encodedJson,
equals('{"amount":10,'
'"fromUser":{"axis":"x","email":"joe@mail.com","enabled":true,"id":1001,"isEnabled":true,"theLevel":null,"name":"joe","password":"123"},'
'"toUser":{"axis":"x","email":"smith@mail.com","enabled":true,"id":1002,"isEnabled":true,"theLevel":null,"name":"smith","password":"456"}}'));
'"fromUser":{"axis":"x","email":"joe@mail.com","enabled":true,"id":1001,"isEnabled":true,"theLevel":null,"name":"joe"},'
'"toUser":{"axis":"x","email":"smith@mail.com","enabled":true,"id":1002,"isEnabled":true,"theLevel":null,"name":"smith"}}'));

{
var decoded =
jsonCodec.decode(encodedJson, type: TestTransactionWithReflection)
as TestTransactionWithReflection;
var decoded = jsonCodec.decode(
encodedJson.replaceAll('"}', '","password":"123"}'),
type: TestTransactionWithReflection)
as TestTransactionWithReflection;

print(decoded);

Expand All @@ -1490,11 +1500,12 @@ void main() {
expect(
encodedJson,
equals('{"amount":20,'
'"fromUser":{"axis":"x","email":"joe@mail.com","enabled":true,"id":1001,"isEnabled":true,"theLevel":null,"name":"joe","password":"123"},'
'"fromUser":{"axis":"x","email":"joe@mail.com","enabled":true,"id":1001,"isEnabled":true,"theLevel":null,"name":"joe"},'
'"toUser":1001}'));

{
var decoded = jsonCodec.decode(encodedJson,
var decoded = jsonCodec.decode(
encodedJson.replaceAll('"}', '","password":"123"}'),
type: TestTransactionWithReflection,
duplicatedEntitiesAsID: true) as TestTransactionWithReflection;

Expand Down

0 comments on commit 0c8efcd

Please sign in to comment.