Skip to content

Commit d72c1f5

Browse files
committed
support for DynamicValue-->DefaultValue for yaml/json/xml (#95)
1 parent 8323aa7 commit d72c1f5

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

Synapse.Core/Classes/DynamicValue.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public bool TryGetDefaultValue(out object value)
2323

2424
return ok;
2525
}
26+
internal bool DefaultAllowNull { get { return Default.Value != null || Default.AllowNull; } }
2627

2728
public override string ToString()
2829
{

Synapse.Core/Utilities/XmlHelpers.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,26 @@ static int FindElementIndex(XmlElement element)
198198
throw new ArgumentException( "Couldn't find element within parent" );
199199
}
200200

201-
public static void Merge(ref XmlDocument source, List<DynamicValue> patch, Dictionary<string, string> values)
201+
public static void Merge(ref XmlDocument source, List<DynamicValue> dynamicValues, Dictionary<string, string> values)
202202
{
203203
if( source == null ) { throw new ArgumentException( "Source cannot be null.", "source" ); }
204-
if( patch == null ) { throw new ArgumentException( "Patch cannot be null.", "patch" ); }
205-
if( values == null ) { throw new ArgumentException( "Values cannot be null.", "values" ); }
206204

207-
foreach( DynamicValue dv in patch )
205+
//if there's nothing to do, get out!
206+
if( dynamicValues == null || dynamicValues?.Count == 0 )
207+
return;
208+
209+
foreach( DynamicValue dv in dynamicValues )
208210
{
209-
if( values.ContainsKey( dv.Source ) )
211+
string value = null;
212+
if( (values?.ContainsKey( dv.Source )).Value && !string.IsNullOrWhiteSpace( values[dv.Source] ) )
213+
value = values[dv.Source];
214+
215+
if( string.IsNullOrWhiteSpace( value ) && dv.TryGetDefaultValue( out object defaultValue ) )
216+
if( defaultValue != null )
217+
value = defaultValue.ToString();
218+
219+
if( !string.IsNullOrWhiteSpace( value ) || dv.DefaultAllowNull )
210220
{
211-
string value = values[dv.Source];
212221
if( !dv.Validate( value, out string validationErrorMessage ) )
213222
throw new ArgumentException( validationErrorMessage );
214223

Synapse.Core/Utilities/YamlHelpers.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,11 @@ public static void Merge(ref Dictionary<object, object> source, List<DynamicValu
121121
if( (values?.ContainsKey( dv.Source )).Value && !string.IsNullOrWhiteSpace( values[dv.Source] ) )
122122
val = values[dv.Source];
123123

124-
if( string.IsNullOrWhiteSpace( val ) && dv.TryGetDefaultValue( out object defaultValue ))
125-
val = Serialize( defaultValue );
124+
if( string.IsNullOrWhiteSpace( val ) && dv.TryGetDefaultValue( out object defaultValue ) )
125+
if( defaultValue != null )
126+
val = Serialize( defaultValue );
126127

127-
if( !string.IsNullOrWhiteSpace( val ) )
128+
if( !string.IsNullOrWhiteSpace( val ) || dv.DefaultAllowNull )
128129
{
129130
if( !dv.Validate( val, out string validationErrorMessage ) )
130131
throw new ArgumentException( validationErrorMessage );
@@ -391,7 +392,7 @@ internal static void ApplyPatchValues(List<object> source, List<object> patch, I
391392
ApplyPatchValues( (List<object>)listItemValue[patchKey], (List<object>)patchValue, dv );
392393
}
393394
else //if( patchValue is 'the value' )
394-
listItemValue[patchKey] = patchValue;
395+
listItemValue[patchKey] = RegexReplaceOrValue( listItemValue[patchKey], patchValue, dv );
395396
}
396397
else if( source[i] is List<object> )
397398
//recurse back in
@@ -434,12 +435,13 @@ internal static object RegexReplaceOrValue(object input, object replacement, IRe
434435
{
435436
if( rv.HasReplace )
436437
{
438+
string repl = $"{replacement}";
437439
if( rv.IsBase64Encode )
438-
replacement = CryptoHelpers.Encode( replacement.ToString() );
439-
value = Regex.Replace( input.ToString(), rv.Replace, replacement.ToString(), RegexOptions.IgnoreCase );
440+
repl = CryptoHelpers.Encode( repl );
441+
value = Regex.Replace( input.ToString(), rv.Replace, repl, RegexOptions.IgnoreCase );
440442
}
441443
else if( rv.IsBase64Encode )
442-
value = CryptoHelpers.Encode( value.ToString() );
444+
value = CryptoHelpers.Encode( $"{value}" );
443445
}
444446

445447
return value;

0 commit comments

Comments
 (0)