TatsuUkraine / dart_environment_config

Environment specific config generator for Dart and Flutter applications during CI/CD builds
https://pub.dev/packages/environment_config
BSD 3-Clause "New" or "Revised" License
92 stars 8 forks source link

Environment variables produce invalid syntax #24

Closed binarypie closed 4 years ago

binarypie commented 4 years ago

Perhaps I am doing something incorrect here.

environment_config:
  path: environment.dart
  class: Environment
  fields:
    api:
      type: String
      short_name: a
      pattern: 'https://__VALUE__'
      env_var: API_DOMAIN

Set your environment variable(s)

export API_DOMAIN="api.local"

Then run

pub run environment_config:generate

Dartfmt will fail because of invalid syntax.

❯ pub run environment_config:generate
Precompiling executable... (7.1s)
Precompiled environment_config:generate.
Could not format because the source could not be parsed:

line 1, column 46: Expected to find ';'.
  ╷
1 │ class Environment {static const String api = https://api.local;
  │                                              ^^^^^

As a work around the following works as expected.

environment_config:
  path: environment.dart
  class: Environment
  fields:
    api:
      short_name: a
      env_var: API_DOMAIN
      customClassValue:
        type: String
        pattern: 'https://__VALUE__'
TatsuUkraine commented 4 years ago

so the case here is that YAML treats with key values as Strings aways. So this

'http://__VALUE__'

will be equals to this

http://__VALUE__

For string fields default pattern is "'__VALUE__'". So in order to get the expected result your pattern should be following "'https://__VALUE__'". In that way ' will become part of the value and not opening/closing bracket of the String.

environment_config:
  path: environment.dart
  class: Environment
  fields:
    api:
      type: String
      short_name: a
      pattern: "'https://__VALUE__'"
      env_var: API_DOMAIN

The reason why I don't always add ' around field values is that I wanted to give dev an option to provide patterns to specify different types of values.

======= Here are some examples. ========

You may want to have something, that provides Strings value based on env value:

class SomeValueProvider {

  static String getSomeValue(String env) {
    ....
  }
}

In that way, your YAML will look like this:

environment_config:
  fields:
    env:
      type: String
      pattern: SomeValueProvider.getSomeValue('__VALUE__')

Another example. You may want to have different classes:

class ProdClass {

  static String getSomeValue() {
    ....
  }
}

class DevClass {

  static String getSomeValue() {
    ....
  }
}

In that way, your YAML will look like this:

environment_config:
  fields:
    env:
      type: String
      pattern: __VALUE__Class.getSomeValue()

Hope it helps)

binarypie commented 4 years ago

This is a great explanation. Thank you!

TatsuUkraine commented 4 years ago

I understand that these examples are raw) I just wanted to show some probable use cases of my decision to don't always wrap result values)