Skip to content

Commit

Permalink
✨ add option to disable variable interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Mar 9, 2024
1 parent e21f3dc commit fffded3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 33 deletions.
12 changes: 12 additions & 0 deletions packages/envied/lib/src/envied_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ final class Envied {
/// ```
final bool useConstantCase;

/// Whether to interpolate the values for all fields.
/// If [interpolate] is `true`, the value will be interpolated
/// with the environment variables.
final bool interpolate;

/// Whether to use the raw string format for all string values.
///
/// **NOTE**: The string is always formatted `'<value>'`.
Expand All @@ -88,6 +93,7 @@ final class Envied {
this.obfuscate = false,
this.allowOptionalFields = false,
this.useConstantCase = false,
this.interpolate = true,
this.rawStrings = false,
}) : path = path ?? '.env',
requireEnvFile = requireEnvFile ?? false;
Expand Down Expand Up @@ -138,6 +144,11 @@ final class EnviedField {
/// ```
final bool? useConstantCase;

/// Whether to use the interpolated value for the field.
/// If [interpolate] is `true`, the value will be interpolated
/// with the environment variables.
final bool? interpolate;

/// Whether to use the raw string format for the value.
///
/// Can only be used with a [String] type.
Expand All @@ -155,6 +166,7 @@ final class EnviedField {
this.defaultValue,
this.optional,
this.useConstantCase,
this.interpolate,
this.rawString,
});
}
26 changes: 11 additions & 15 deletions packages/envied_generator/lib/src/generate_field.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:code_builder/code_builder.dart';
import 'package:envied_generator/src/env_val.dart';
import 'package:envied_generator/src/extensions.dart';
import 'package:source_gen/source_gen.dart';

Expand All @@ -14,7 +13,7 @@ import 'package:source_gen/source_gen.dart';
/// the type can't be casted, or is not supported.
Iterable<Field> generateFields(
FieldElement field,
EnvVal? value, {
String? value, {
bool allowOptional = false,
bool rawString = false,
}) {
Expand Down Expand Up @@ -55,7 +54,7 @@ Iterable<Field> generateFields(
if (field.type.isDartCoreInt ||
field.type.isDartCoreDouble ||
field.type.isDartCoreNum) {
final num? parsed = num.tryParse(value.interpolated);
final num? parsed = num.tryParse(value);

if (parsed == null) {
throw InvalidGenerationSourceError(
Expand All @@ -67,7 +66,7 @@ Iterable<Field> generateFields(
modifier = FieldModifier.constant;
result = literalNum(parsed);
} else if (field.type.isDartCoreBool) {
final bool? parsed = bool.tryParse(value.interpolated);
final bool? parsed = bool.tryParse(value);

if (parsed == null) {
throw InvalidGenerationSourceError(
Expand All @@ -79,7 +78,7 @@ Iterable<Field> generateFields(
modifier = FieldModifier.constant;
result = literalBool(parsed);
} else if (field.type.isDartCoreUri) {
final Uri? parsed = Uri.tryParse(value.interpolated);
final Uri? parsed = Uri.tryParse(value);

if (parsed == null) {
throw InvalidGenerationSourceError(
Expand All @@ -92,11 +91,11 @@ Iterable<Field> generateFields(
result = refer('Uri').type.newInstanceNamed(
'parse',
[
literalString(value.interpolated),
literalString(value),
],
);
} else if (field.type.isDartCoreDateTime) {
final DateTime? parsed = DateTime.tryParse(value.interpolated);
final DateTime? parsed = DateTime.tryParse(value);

if (parsed == null) {
throw InvalidGenerationSourceError(
Expand All @@ -109,13 +108,13 @@ Iterable<Field> generateFields(
result = refer('DateTime').type.newInstanceNamed(
'parse',
[
literalString(value.interpolated),
literalString(value),
],
);
} else if (field.type.isDartEnum) {
final EnumElement enumElement = field.type.element as EnumElement;

if (!enumElement.valueNames.contains(value.interpolated)) {
if (!enumElement.valueNames.contains(value)) {
throw InvalidGenerationSourceError(
'Enumerated type `$type` does not contain value `$value`. '
'Possible values are: ${enumElement.valueNames.map((el) => '`$el`').join(', ')}.',
Expand All @@ -126,18 +125,15 @@ Iterable<Field> generateFields(
modifier = FieldModifier.final$;
result = refer(type).type.property('values').property('byName').call(
[
literalString(value.interpolated),
literalString(value),
],
);
} else if (field.type.isDartCoreString) {
modifier = FieldModifier.constant;
result = switch (rawString) {
true => literalString(value.raw, raw: true),
_ => literalString(value.interpolated),
};
result = literalString(value, raw: rawString);
} else if (field.type is DynamicType) {
modifier = FieldModifier.constant;
result = literalString(value.interpolated);
result = literalString(value);
} else {
throw InvalidGenerationSourceError(
'Envied can only handle types such as `int`, `double`, `num`, '
Expand Down
19 changes: 8 additions & 11 deletions packages/envied_generator/lib/src/generate_field_encrypted.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:code_builder/code_builder.dart';
import 'package:envied_generator/src/env_val.dart';
import 'package:envied_generator/src/extensions.dart';
import 'package:source_gen/source_gen.dart';

Expand All @@ -17,7 +16,7 @@ import 'package:source_gen/source_gen.dart';
/// the type can't be casted, or is not supported.
Iterable<Field> generateFieldsEncrypted(
FieldElement field,
EnvVal? value, {
String? value, {
bool allowOptional = false,
}) {
final Random rand = Random.secure();
Expand Down Expand Up @@ -67,7 +66,7 @@ Iterable<Field> generateFieldsEncrypted(
}

if (field.type.isDartCoreInt) {
final int? parsed = int.tryParse(value.interpolated);
final int? parsed = int.tryParse(value);

if (parsed == null) {
throw InvalidGenerationSourceError(
Expand Down Expand Up @@ -105,7 +104,7 @@ Iterable<Field> generateFieldsEncrypted(
}

if (field.type.isDartCoreBool) {
final bool? parsed = bool.tryParse(value.interpolated);
final bool? parsed = bool.tryParse(value);

if (parsed == null) {
throw InvalidGenerationSourceError(
Expand Down Expand Up @@ -149,12 +148,10 @@ Iterable<Field> generateFieldsEncrypted(
field.type.isDartEnum ||
field.type.isDartCoreString ||
field.type is DynamicType) {
if ((field.type.isDartCoreUri &&
Uri.tryParse(value.interpolated) == null) ||
(field.type.isDartCoreDateTime &&
DateTime.tryParse(value.interpolated) == null) ||
if ((field.type.isDartCoreUri && Uri.tryParse(value) == null) ||
(field.type.isDartCoreDateTime && DateTime.tryParse(value) == null) ||
((field.type.isDartCoreDouble || field.type.isDartCoreNum) &&
num.tryParse(value.interpolated) == null)) {
num.tryParse(value) == null)) {
throw InvalidGenerationSourceError(
'Type `$type` does not align with value `$value`.',
element: field,
Expand All @@ -164,7 +161,7 @@ Iterable<Field> generateFieldsEncrypted(
if (field.type.isDartEnum) {
final EnumElement enumElement = field.type.element as EnumElement;

if (!enumElement.valueNames.contains(value.interpolated)) {
if (!enumElement.valueNames.contains(value)) {
throw InvalidGenerationSourceError(
'Enumerated type `$type` does not contain value `$value`. '
'Possible values are: ${enumElement.valueNames.map((el) => '`$el`').join(', ')}.',
Expand All @@ -175,7 +172,7 @@ Iterable<Field> generateFieldsEncrypted(

late final String? symbol;
late final Expression result;
final List<int> parsed = value.interpolated.codeUnits;
final List<int> parsed = value.codeUnits;
final List<int> key = [
for (int i = 0; i < parsed.length; i++) rand.nextInt(1 << 32)
];
Expand Down
18 changes: 15 additions & 3 deletions packages/envied_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ final class EnviedGenerator extends GeneratorForAnnotation<Envied> {
annotation.read('allowOptionalFields').literalValue as bool? ?? false,
useConstantCase:
annotation.read('useConstantCase').literalValue as bool? ?? false,
interpolate: annotation.read('interpolate').literalValue as bool? ?? true,
rawStrings: annotation.read('rawStrings').literalValue as bool? ?? false,
);

Expand Down Expand Up @@ -135,6 +136,9 @@ final class EnviedGenerator extends GeneratorForAnnotation<Envied> {
final bool optional = reader.read('optional').literalValue as bool? ??
config.allowOptionalFields;

final bool interpolate =
reader.read('interpolate').literalValue as bool? ?? config.interpolate;

final bool rawString =
reader.read('rawString').literalValue as bool? ?? config.rawStrings;

Expand All @@ -149,8 +153,16 @@ final class EnviedGenerator extends GeneratorForAnnotation<Envied> {
}

return reader.read('obfuscate').literalValue as bool? ?? config.obfuscate
? generateFieldsEncrypted(field, varValue, allowOptional: optional)
: generateFields(field, varValue,
allowOptional: optional, rawString: rawString);
? generateFieldsEncrypted(
field,
interpolate ? varValue?.interpolated : varValue?.raw,
allowOptional: optional,
)
: generateFields(
field,
interpolate ? varValue?.interpolated : varValue?.raw,
allowOptional: optional,
rawString: rawString,
);
}
}
8 changes: 4 additions & 4 deletions packages/envied_generator/test/src/generator_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ final class _Env8 {
abstract class Env8 {
@EnviedField()
static const String? testString = null;
@EnviedField(rawString: true)
@EnviedField(rawString: true, interpolate: false)
static const String? testUnescapedString = null;
@EnviedField(rawString: true)
@EnviedField(rawString: true, interpolate: false)
static const String? testUnescapedString2 = null;
@EnviedField(rawString: true)
@EnviedField(rawString: true, interpolate: false)
static const String? testUnescapedString3 = null;
@EnviedField(rawString: true)
@EnviedField(rawString: true, interpolate: false)
static const String? testUnescapedString4 = null;
@EnviedField()
static const int? testInt = null;
Expand Down

0 comments on commit fffded3

Please sign in to comment.