Skip to content

Commit

Permalink
feat(casing): add variable name casing
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanaidilp committed Jul 13, 2023
1 parent 5af86fd commit edcc78a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 14 deletions.
3 changes: 2 additions & 1 deletion examples/envied_example/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
KEY1=VALUE1
KEY2=VALUE2
KEY2=VALUE2
TEST_KEY=TEST_VALUE
2 changes: 2 additions & 0 deletions examples/envied_example/lib/env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions examples/envied_example/lib/env.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/envied_example/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ void run() {
print(Env.key3);
print(Env.key4);
print(Env.key5);
print(Env.testKey);
}
10 changes: 8 additions & 2 deletions packages/envied_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -52,8 +53,13 @@ class EnviedGenerator extends GeneratorForAnnotation<Envied> {
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;

Expand Down
36 changes: 36 additions & 0 deletions packages/envied_generator/lib/src/helpers/string.dart
Original file line number Diff line number Diff line change
@@ -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?);
5 changes: 5 additions & 0 deletions packages/envied_generator/test/.env.example
Original file line number Diff line number Diff line change
@@ -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
22 changes: 11 additions & 11 deletions packages/envied_generator/test/src/generator_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
Expand Down Expand Up @@ -94,7 +94,7 @@ class _Env10 {
''')
@Envied(path: 'test/.env.example')
abstract class Env10 {
@EnviedField(varName: 'SYSTEM_VAR')
@EnviedField()
static const String? systemVar = null;
}

Expand Down Expand Up @@ -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';
}

Expand Down

0 comments on commit edcc78a

Please sign in to comment.