Skip to content

Commit

Permalink
Conceptual documentation was updated
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 committed Dec 31, 2021
1 parent 61c7806 commit d9c5d47
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/articles/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,20 @@ catch(FileNotFoundException ex)
}
```

### Interpolating variables

Sometimes you will need to interpolate variables within a value, for example:
```
MYSQL_USER=root
MYSQL_ROOT_PASSWORD=1234
CONNECTION_STRING=username=${MYSQL_USER};password=${MYSQL_ROOT_PASSWORD};database=testdb;
```
If the variable embedded in the value does not exist in the current process, the parser will throw an exception, for example:
```
CONNECTION_STRING=username=${MYSQL_USER};password=${MYSQL_ROOT_PASSWORD};database=testdb;
MYSQL_USER=root
MYSQL_ROOT_PASSWORD=1234
```
In the above example, the parser should throw an exception because the `MYSQL_USER` variable does not exist yet.

**Note:** If you don't know what each class does, don't forget to check the [API documentation](xref:DotEnv.Core).
87 changes: 87 additions & 0 deletions docs/articles/more_options_for_loader.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ new EnvLoader()
```
This method will tell the parser not to remove trailing white-spaces from the keys.

## DisableTrimKeys

Disables the trim at the start and end of the keys:
```cs
new EnvLoader()
.DisableTrimKeys()
.Load();
```
This method will tell the parser not to remove leading and trailing white-spaces from the keys.

## DisableTrimStartValues

Disable the trim at the beginning of the values:
Expand All @@ -42,6 +52,16 @@ new EnvLoader()
```
This method will tell the parser not to remove trailing white-spaces from the values.

## DisableTrimValues

Disables the trim at the start and end of the values:
```cs
new EnvLoader()
.DisableTrimValues()
.Load();
```
This method will tell the parser not to remove leading and trailing white-spaces from the values.

## DisableTrimStartComments

Disable the trim at the beginning of the comments:
Expand All @@ -62,4 +82,71 @@ new EnvLoader()
```
Imagine that if there is an environment variable called `KEY1` whose value is `1`, then in an .env file there can be a key named `KEY1` whose value is `2`, if this option is enabled, then, when the parser reads `KEY1` from the .env file, it will overwrite the value of `KEY1` by `2`.

## AllowConcatDuplicateKeys

Allows to concatenate the values of the duplicate keys:
```cs
new EnvLoader()
.AllowConcatDuplicateKeys()
.Load();
```
This method will by default concatenate to the end of the value, for example, imagine you have the following .env file:
```
KEY1 = Hello
KEY1 = World
KEY1 = !
```
The parser will concatenate the duplicate keys in this way:
```
KEY1 = HelloWorld!
```
But we can also tell the parser to concatenate at the beginning of the value using `ConcatKeysOptions` enum:
```cs
new EnvLoader()
.AllowConcatDuplicateKeys(ConcatKeysOptions.Start)
.Load();
```
So if we follow the above example, the parser will concatenate the duplicate keys in this way:
```
KEY1 = !WorldHello
```

## IgnoreParserExceptions

Ignores parser exceptions. By calling this method the parser will not throw any exceptions when it encounters an error:
```cs
new EnvLoader()
.IgnoreParserExceptions()
.Load();
```

## SetCommentChar

Sets the character that will define the beginning of a comment:
```cs
new EnvLoader()
.SetCommentChar(';')
.Load();
```
So the .env file could look like this:
```
; comment (1)
KEY1=VAL1
; comment (2)
```

## SetDelimiterKeyValuePair

Sets the delimiter that separates an assigment of a value to a key:
```cs
new EnvLoader()
.SetDelimiterKeyValuePair(':')
.Load();
```
So the .env file could look like this:
```
KEY1: VAL1
KEY2: VAL2
```

**As a side note**, the `EnvLoader` class has a parameterized constructor in which you can specify a custom parser. You can inherit from `EnvParser` and inject the instance into the constructor.
12 changes: 11 additions & 1 deletion docs/articles/using_parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Don't forget to look up in the [API documentation](xref:DotEnv.Core.EnvParserOpt

## Customizing the parser algorithm

You can also create a class that inherits from `EnvParser` and then you can override its methods:
You can also create a class that inherits from `EnvParser` and then you can override its methods (each of these methods are used by the [parser algorithm](xref:DotEnv.Core.EnvParser.Parse(System.String))):
```cs
class CustomEnvParser : EnvParser
{
Expand Down Expand Up @@ -73,6 +73,16 @@ class CustomEnvParser : EnvParser
{
// Here you can add your own implementation.
}

protected override string ConcatValues(string currentValue, string value)
{
// Here you can add your own implementation.
}

protected override string ExpandEnvironmentVariables(string value, int lineNumber)
{
// Here you can add your own implementation.
}
}
```
This is useful when you need to customize the parser and change its internal behavior.
Expand Down

0 comments on commit d9c5d47

Please sign in to comment.