From edcc78ab8ba6cf9f7d33296297659b4a31dd7581 Mon Sep 17 00:00:00 2001 From: Fajrian Aidil Pratama Date: Thu, 13 Jul 2023 12:09:49 +0800 Subject: [PATCH] feat(casing): add variable name casing --- examples/envied_example/.env | 3 +- examples/envied_example/lib/env.dart | 2 ++ examples/envied_example/lib/env.g.dart | 1 + examples/envied_example/lib/example.dart | 1 + .../envied_generator/lib/src/generator.dart | 10 ++++-- .../lib/src/helpers/string.dart | 36 +++++++++++++++++++ packages/envied_generator/test/.env.example | 5 +++ .../test/src/generator_tests.dart | 22 ++++++------ 8 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 packages/envied_generator/lib/src/helpers/string.dart diff --git a/examples/envied_example/.env b/examples/envied_example/.env index e72d17e..155ab7b 100644 --- a/examples/envied_example/.env +++ b/examples/envied_example/.env @@ -1,2 +1,3 @@ KEY1=VALUE1 -KEY2=VALUE2 \ No newline at end of file +KEY2=VALUE2 +TEST_KEY=TEST_VALUE \ No newline at end of file diff --git a/examples/envied_example/lib/env.dart b/examples/envied_example/lib/env.dart index fb06ecf..c11e79d 100644 --- a/examples/envied_example/lib/env.dart +++ b/examples/envied_example/lib/env.dart @@ -15,4 +15,6 @@ abstract class Env { static const int key4 = _Env.key4; @EnviedField() static const bool key5 = _Env.key5; + @EnviedField() + static const String testKey = _Env.testKey; } diff --git a/examples/envied_example/lib/env.g.dart b/examples/envied_example/lib/env.g.dart index a3738d5..9f4c6f3 100644 --- a/examples/envied_example/lib/env.g.dart +++ b/examples/envied_example/lib/env.g.dart @@ -12,4 +12,5 @@ class _Env { static const String key3 = 'test_'; static const int key4 = 0; static const bool key5 = true; + static const String testKey = 'TEST_VALUE'; } diff --git a/examples/envied_example/lib/example.dart b/examples/envied_example/lib/example.dart index 5d518af..4d3cf93 100644 --- a/examples/envied_example/lib/example.dart +++ b/examples/envied_example/lib/example.dart @@ -6,4 +6,5 @@ void run() { print(Env.key3); print(Env.key4); print(Env.key5); + print(Env.testKey); } diff --git a/packages/envied_generator/lib/src/generator.dart b/packages/envied_generator/lib/src/generator.dart index 6498050..130f2ec 100644 --- a/packages/envied_generator/lib/src/generator.dart +++ b/packages/envied_generator/lib/src/generator.dart @@ -6,6 +6,7 @@ import 'package:build/build.dart'; import 'package:envied/envied.dart'; import 'package:envied_generator/src/generate_line.dart'; import 'package:envied_generator/src/generate_line_encrypted.dart'; +import 'package:envied_generator/src/helpers/string.dart'; import 'package:envied_generator/src/load_envs.dart'; import 'package:source_gen/source_gen.dart'; @@ -52,8 +53,13 @@ class EnviedGenerator extends GeneratorForAnnotation { DartObject? dartObject = enviedFieldChecker.firstAnnotationOf(fieldEl); ConstantReader reader = ConstantReader(dartObject); - String varName = - reader.read('varName').literalValue as String? ?? fieldEl.name; + late String varName; + + if (reader.read('varName').literalValue == null) { + varName = normalize(fieldEl.name); + } else { + varName = reader.read('varName').literalValue as String; + } Object? defaultValue = reader.read('defaultValue').literalValue; diff --git a/packages/envied_generator/lib/src/helpers/string.dart b/packages/envied_generator/lib/src/helpers/string.dart new file mode 100644 index 0000000..4666943 --- /dev/null +++ b/packages/envied_generator/lib/src/helpers/string.dart @@ -0,0 +1,36 @@ +// Helper functions related to strings + +/// Method to convert camelCases to snake_cases +String camelCaseToSnakeCase(String input) { + return input.replaceAllMapped( + RegExp('([A-Z])|([0-9]+)'), + (match) => '_${match.group(0)!.toLowerCase()}', + ); +} + +/// Method that will returns +/// [a] if [a] is not null or an empty string. Otherwise, +/// [b] will be returned. +String ifFalsy(String? a, String b) { + return validate(a, (x) => (x ?? '').isNotEmpty) ? a! : b; +} + +/// Method to normalize the [input] string to .ENV +/// compatible keys. +/// +/// For example: +/// `isStudent1` will have an output of `IS_STUDENT_1` +String normalize(String input) { + return camelCaseToSnakeCase(input).toUpperCase(); +} + +/// Method to decide whether a [String] input +/// returns `true` or `false` through the predicate function +/// or not. +bool validate(String? input, BoolFn predicate) { + return predicate(input); +} + +/// Type Definition: function with a [String] as a parameter +/// returning a [bool] +typedef BoolFn = bool Function(String?); diff --git a/packages/envied_generator/test/.env.example b/packages/envied_generator/test/.env.example index 75ea123..9c4fffe 100644 --- a/packages/envied_generator/test/.env.example +++ b/packages/envied_generator/test/.env.example @@ -1,7 +1,12 @@ test_string=test_string testString=testString +TEST_STRING=TEST_STRING testInt=123 +TEST_INT=123 testDouble=1.23 +TEST_DOUBLE=1.23 +TEST_BOOL=true testBool=true testDynamic=123abc +TEST_DYNAMIC=123abc SYSTEM_VAR=system_var \ No newline at end of file diff --git a/packages/envied_generator/test/src/generator_tests.dart b/packages/envied_generator/test/src/generator_tests.dart index 6b70ad7..39832e1 100644 --- a/packages/envied_generator/test/src/generator_tests.dart +++ b/packages/envied_generator/test/src/generator_tests.dart @@ -25,28 +25,28 @@ abstract class Env3 { static const Symbol? testString = null; } -@ShouldThrow('Type `int` do not align up to value `testString`.') +@ShouldThrow('Type `int` do not align up to value `TEST_STRING`.') @Envied(path: 'test/.env.example') abstract class Env4 { @EnviedField() static const int? testString = null; } -@ShouldThrow('Type `double` do not align up to value `testString`.') +@ShouldThrow('Type `double` do not align up to value `TEST_STRING`.') @Envied(path: 'test/.env.example') abstract class Env5 { @EnviedField() static const double? testString = null; } -@ShouldThrow('Type `num` do not align up to value `testString`.') +@ShouldThrow('Type `num` do not align up to value `TEST_STRING`.') @Envied(path: 'test/.env.example') abstract class Env6 { @EnviedField() static const num? testString = null; } -@ShouldThrow('Type `bool` do not align up to value `testString`.') +@ShouldThrow('Type `bool` do not align up to value `TEST_STRING`.') @Envied(path: 'test/.env.example') abstract class Env7 { @EnviedField() @@ -55,7 +55,7 @@ abstract class Env7 { @ShouldGenerate(''' class _Env8 { - static const String testString = 'testString'; + static const String testString = 'TEST_STRING'; static const int testInt = 123; static const double testDouble = 1.23; static const bool testBool = true; @@ -94,7 +94,7 @@ class _Env10 { ''') @Envied(path: 'test/.env.example') abstract class Env10 { - @EnviedField(varName: 'SYSTEM_VAR') + @EnviedField() static const String? systemVar = null; } @@ -160,15 +160,15 @@ class _Env15 { abstract class Env15 { @EnviedField(defaultValue: 'test_') static const String? testDefaultParam = null; - @EnviedField() + @EnviedField(varName: 'testString') static const String testString = 'testString'; - @EnviedField() + @EnviedField(varName: 'testInt') static const int testInt = 123; - @EnviedField() + @EnviedField(varName: 'testDouble') static const double testDouble = 1.23; - @EnviedField() + @EnviedField(varName: 'testBool') static const bool testBool = true; - @EnviedField() + @EnviedField(varName: 'testDynamic') static const dynamic testDynamic = '123abc'; }