Example #1
0
        /// <summary>
        /// Creates a new set of metadata.
        /// </summary>
        /// <param name="executionState">The current execution state.</param>
        /// <param name="items">The initial set of items. If null, no underlying dictionary will be created.</param>
        public Metadata(IExecutionState executionState, IEnumerable <KeyValuePair <string, object> > items = null)
        {
            _ = executionState ?? throw new ArgumentNullException(nameof(executionState));

            if (items != null)
            {
                Dictionary = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

                // If items is an IMetadata, use the raw enumerable so that we don't expand IMetadataValue and Config values
                if (items is IMetadata metadata)
                {
                    items = metadata.GetRawEnumerable();
                }

                // Iterate the items, checking for script values
                foreach (KeyValuePair <string, object> item in items)
                {
                    if (ScriptMetadataValue.TryGetScriptMetadataValue(item.Key, item.Value, executionState, out ScriptMetadataValue metadataValue))
                    {
                        Dictionary[item.Key] = metadataValue;
                    }
                    else
                    {
                        Dictionary[item.Key] = item.Value;
                    }
                }
            }
        }
Example #2
0
 public static bool TryGetScriptMetadataValue(string key, object value, IExecutionState executionState, out ScriptMetadataValue scriptMetadataValue)
 {
     scriptMetadataValue = default;
     if (value is string stringValue && IScriptHelper.TryGetScriptString(stringValue, out string script))
     {
         scriptMetadataValue = new ScriptMetadataValue(
             key,
             stringValue.Substring(0, stringValue.Length - script.Length),
             script,
             executionState);
         return(true);
     }
     return(false);
 }
 public static bool TryGetScriptMetadataValue(string key, object value, IExecutionState executionState, out ScriptMetadataValue scriptMetadataValue)
 {
     scriptMetadataValue = default;
     if (value is string script)
     {
         script = script.TrimStart();
         if (script.StartsWith("=>"))
         {
             script = script.Substring(2);
             if (!string.IsNullOrWhiteSpace(script))
             {
                 scriptMetadataValue = new ScriptMetadataValue(
                     key,
                     ((string)value).Substring(0, ((string)value).Length - script.Length),
                     script,
                     executionState);
                 return(true);
             }
         }
     }
     return(false);
 }