-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Added multi-line parsing via double quotes & Fix: Pipelines #52
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
c2246df
Enabled multi-line parsing of env files
VijoPlays c294fcf
Merged master
VijoPlays 14f7b26
.net 8 changes
VijoPlays 5d8d36a
Expanded README with multi-line usage
VijoPlays 7e0d967
Testing pipelines fix
VijoPlays 56e7868
Testing pipelines fix
VijoPlays 208a08e
Dotnet versions
VijoPlays 06a8687
Pipeline: Test fix
VijoPlays 4d69544
Pipeline: Test fix
VijoPlays 5b2bad9
Pipeline: Test fix
VijoPlays dfab9ad
Pipeline: Test fix
VijoPlays 8460529
Pipeline: Test fix
VijoPlays 56eb55e
Pipeline: Test fix
VijoPlays 0d8e2dc
Pipeline: Test fix
VijoPlays c6622ca
Pipeline: Test fix
VijoPlays e6f3a50
Pipeline: Test fix
VijoPlays ed6028e
Pipeline: Test fix
VijoPlays 7be0f92
Pipeline: Debugging
VijoPlays 96014a2
Pipeline: Debugging
VijoPlays 9dfb2fd
Pipeline: Debugging
VijoPlays e5624d4
Pipeline: Debugging
VijoPlays 1acb1a2
Pipeline: Debugging
VijoPlays 0e6e2e3
Pipeline: Debugging
VijoPlays 9b508bf
Pipeline: Debugging
VijoPlays 7fabb73
Pipeline: Debugging
VijoPlays 8de254a
Pipeline: Debugging
VijoPlays 8e70bca
Pipeline: Debugging
VijoPlays c8c5611
Pipeline: Debugging
VijoPlays bd45389
Pipeline: Debugging
VijoPlays 6d7b23f
Pipeline: Debugging
VijoPlays 183379a
Pipeline: Debugging
VijoPlays d7fa8ad
Pipeline: Debugging
VijoPlays 81a4ccb
Pipeline: Fixed
VijoPlays c0a756d
Merge pull request #2 from SamhammerAG/pipelines
VijoPlays d60016b
Review adjustments
VijoPlays d43a3a2
Merge branch 'master' of github.com:SamhammerAG/dotenv.net
VijoPlays 3dc340c
Pipeline test fix
VijoPlays File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace dotenv.net; | ||
|
||
internal static class Parser | ||
{ | ||
private static readonly char[] SingleQuote = { '\'' }; | ||
private static readonly char[] DoubleQuotes = { '"' }; | ||
private const string SingleQuote = "'"; | ||
private const string DoubleQuotes = "\""; | ||
|
||
internal static ReadOnlySpan<KeyValuePair<string, string>> Parse(ReadOnlySpan<string> dotEnvRows, | ||
bool shouldTrimValue) | ||
internal static ReadOnlySpan<KeyValuePair<string, string>> Parse(ReadOnlySpan<string> rawEnvRows, | ||
bool trimValues) | ||
{ | ||
var validEntries = new List<KeyValuePair<string, string>>(); | ||
var keyValuePairs = new List<KeyValuePair<string, string>>(); | ||
|
||
foreach (var dotEnvRow in dotEnvRows) | ||
for (var i = 0; i < rawEnvRows.Length; i++) | ||
{ | ||
var row = new ReadOnlySpan<char>(dotEnvRow.TrimStart().ToCharArray()); | ||
|
||
if (row.IsEmpty) | ||
continue; | ||
|
||
if (row.IsComment()) | ||
continue; | ||
|
||
if (row.HasNoKey(out var index)) | ||
continue; | ||
|
||
var key = row.Key(index); | ||
var value = row.Value(index, shouldTrimValue); | ||
validEntries.Add(new KeyValuePair<string, string>(key, value)); | ||
var rawEnvRow = rawEnvRows[i]; | ||
|
||
if(rawEnvRow.StartsWith("#")) continue; | ||
|
||
if (rawEnvRow.Contains("=\"")) | ||
{ | ||
var key = rawEnvRow.Substring(0, rawEnvRow.IndexOf("=\"", StringComparison.Ordinal)); | ||
var valueStringBuilder = new StringBuilder(); | ||
valueStringBuilder.Append(rawEnvRow.Substring(rawEnvRow.IndexOf("=\"", StringComparison.Ordinal) + 2)); | ||
|
||
while (!rawEnvRow.EndsWith("\"")) | ||
{ | ||
i++; | ||
if (i >= rawEnvRows.Length) | ||
{ | ||
break; | ||
} | ||
rawEnvRow = rawEnvRows[i]; | ||
valueStringBuilder.AppendLine(); | ||
valueStringBuilder.Append(rawEnvRow); | ||
} | ||
//Remove last " | ||
valueStringBuilder.Remove(valueStringBuilder.Length - 1, 1); | ||
|
||
var value = valueStringBuilder.ToString(); | ||
if (trimValues) | ||
{ | ||
value = value.Trim(); | ||
} | ||
|
||
keyValuePairs.Add(new KeyValuePair<string, string>(key, value)); | ||
} | ||
else | ||
{ | ||
//Check that line is not empty | ||
var rawEnvEmpty = rawEnvRow.Trim(); | ||
if(string.IsNullOrEmpty(rawEnvEmpty)) continue; | ||
|
||
// Regular key-value pair | ||
var keyValue = rawEnvRow.Split(['='], 2); | ||
|
||
var key = keyValue[0].Trim(); | ||
var value = keyValue[1]; | ||
|
||
if(string.IsNullOrEmpty(key)) continue; | ||
|
||
if (IsQuoted(value)) | ||
{ | ||
value = StripQuotes(value); | ||
} | ||
|
||
if (trimValues) | ||
{ | ||
value = value.Trim(); | ||
} | ||
|
||
keyValuePairs.Add(new KeyValuePair<string, string>(key, value)); | ||
} | ||
} | ||
|
||
return new ReadOnlySpan<KeyValuePair<string, string>>(validEntries.ToArray()); | ||
return keyValuePairs.ToArray(); | ||
} | ||
|
||
private static bool IsComment(this ReadOnlySpan<char> row) => row[0] == '#'; | ||
private static bool IsQuoted(string value) => (value.StartsWith(SingleQuote) && value.EndsWith(SingleQuote)) | ||
|| (value.StartsWith(DoubleQuotes) && value.EndsWith(DoubleQuotes)); | ||
|
||
private static bool HasNoKey(this ReadOnlySpan<char> row, out int index) | ||
private static string StripQuotes(string value) | ||
{ | ||
index = row.IndexOf('='); | ||
return index <= 0; | ||
} | ||
|
||
private static bool IsQuoted(this ReadOnlySpan<char> row) => | ||
(row.StartsWith(SingleQuote) && row.EndsWith(SingleQuote)) | ||
|| (row.StartsWith(DoubleQuotes) && row.EndsWith(DoubleQuotes)); | ||
|
||
private static ReadOnlySpan<char> StripQuotes(this ReadOnlySpan<char> row) => row.Trim('\'').Trim('\"'); | ||
|
||
private static string Key(this ReadOnlySpan<char> row, int index) | ||
{ | ||
var untrimmedKey = row.Slice(0, index); | ||
return untrimmedKey.Trim().ToString(); | ||
} | ||
|
||
private static string Value(this ReadOnlySpan<char> row, int index, bool trimValue) | ||
{ | ||
var value = row.Slice(index + 1); | ||
|
||
// handle quoted values | ||
if (value.IsQuoted()) | ||
value = value.StripQuotes(); | ||
|
||
// trim output if requested | ||
if (trimValue) | ||
value = value.Trim(); | ||
|
||
return value.ToString(); | ||
return value.Substring(1, value.Length - 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
DOUBLE_QUOTE="double" | ||
DOUBLE_QUOTE_MULTI_LINE="dou | ||
bler" | ||
DOUBLE_QUOTE_EVEN_MORE_LINES="dou | ||
|
||
b | ||
|
||
lest" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will fail if the env is
I have merged the PR in but will have to introduce a fix