private static void WriteJson(JsonTextWriter json, RuntimeValue data)
        {
            switch (data.ValueType)
            {
            case RuntimeValueType.Scalar:
                json.WriteValue(data.AsString());
                break;

            case RuntimeValueType.Vector:
                json.WriteStartArray();
                foreach (var v in data.AsEnumerable())
                {
                    WriteJson(json, v);
                }
                json.WriteEndArray();
                break;

            case RuntimeValueType.Map:
                json.WriteStartObject();
                foreach (var v in data.AsDictionary())
                {
                    json.WritePropertyName(v.Key);
                    WriteJson(json, v.Value);
                }
                json.WriteEndObject();
                break;
            }
        }
Exemple #2
0
        private static void WriteRuntimeValue(BinaryWriter writer, RuntimeValue value)
        {
            var type = value.ValueType;

            writer.Write((byte)type);

            if (type == RuntimeValueType.Scalar)
            {
                writer.Write(value.AsString() ?? string.Empty);
            }
            else if (type == RuntimeValueType.Vector)
            {
                var list = value.AsEnumerable().ToList();
                SlimBinaryFormatter.WriteLength(writer, list.Count);
                foreach (var i in list)
                {
                    WriteRuntimeValue(writer, i);
                }
            }
            else if (type == RuntimeValueType.Map)
            {
                WriteDictionary(writer, value.AsDictionary());
            }
            else
            {
                throw new ArgumentException("Unknown value type: " + type);
            }
        }
Exemple #3
0
        private static async Task <RuntimeValue> ExpandValueAsync(RuntimeValue value, RompExecutionContext context)
        {
            switch (value.ValueType)
            {
            case RuntimeValueType.Scalar:
                return(await context.ExpandVariablesAsync(value.AsString()));

            case RuntimeValueType.Vector:
                var list = new List <RuntimeValue>();
                foreach (var item in value.AsEnumerable())
                {
                    list.Add(await ExpandValueAsync(item, context));
                }
                return(new RuntimeValue(list));

            case RuntimeValueType.Map:
                var map = new Dictionary <string, RuntimeValue>();
                foreach (var pair in value.AsDictionary())
                {
                    map.Add(pair.Key, await ExpandValueAsync(pair.Value, context));
                }
                return(new RuntimeValue(map));

            default:
                throw new ArgumentException();
            }
        }
Exemple #4
0
 public static IEnumerable <RuntimeValue> ParseDictionary(this RuntimeValue val)
 {
     if ((val.AsDictionary()?.Keys.Count ?? 0) <= 0)
     {
         return(null);
     }
     return(new List <RuntimeValue> {
         val
     });
 }
Exemple #5
0
        public static object ConvertToPSValue(RuntimeValue value)
        {
            if (value.ValueType == RuntimeValueType.Scalar)
            {
                var s = value.AsString() ?? string.Empty;
                if (s.StartsWith(Functions.PsCredentialVariableFunction.Prefix))
                {
                    return(Functions.PsCredentialVariableFunction.Deserialize(s.Substring(Functions.PsCredentialVariableFunction.Prefix.Length)));
                }

                if (string.Equals(s, "true", StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }

                if (string.Equals(s, "false", StringComparison.OrdinalIgnoreCase))
                {
                    return(false);
                }

                return(s);
            }
            else if (value.ValueType == RuntimeValueType.Vector)
            {
                return(value.AsEnumerable().Select(v => v.ToString() ?? string.Empty).ToArray());
            }
            else if (value.ValueType == RuntimeValueType.Map)
            {
                var hashTable = new Hashtable();
                foreach (var pair in value.AsDictionary())
                {
                    hashTable[pair.Key] = ConvertToPSValue(pair.Value);
                }

                return(hashTable);
            }
            else
            {
                return(null);
            }
        }
Exemple #6
0
            static PsModulePackageConfiguration parseModule(RuntimeValue value)
            {
                var module = value.AsDictionary();

                if (module == null)
                {
                    return(null);
                }

                if (!module.TryGetValue("Name", out var name) || string.IsNullOrWhiteSpace(name.AsString()))
                {
                    return(null);
                }

                module.TryGetValue("Version", out var version);
                module.TryGetValue("ModuleType", out var moduleType);

                return(new PsModulePackageConfiguration
                {
                    PackageName = name.AsString(),
                    PackageVersion = version.AsString() ?? string.Empty,
                    ModuleType = moduleType.AsString()
                });
            }
Exemple #7
0
        public static object ConvertToPSValue(RuntimeValue value)
        {
            if (value.ValueType == RuntimeValueType.Scalar)
            {
                var s = value.AsString() ?? string.Empty;
                if (s.StartsWith(Functions.PsCredentialVariableFunction.Prefix))
                {
                    return(Functions.PsCredentialVariableFunction.Deserialize(s.Substring(Functions.PsCredentialVariableFunction.Prefix.Length)));
                }

                var match = TypeCastRegex.Match(s);
                if (match.Success)
                {
                    var v = match.Groups[2].Value;
                    switch (match.Groups[1].Value.ToLowerInvariant())
                    {
                    case "int":
                    case "int32":
                    case "sint32":
                    case "system.int32":
                        if (int.TryParse(v, out var i))
                        {
                            return(i);
                        }
                        break;

                    case "uint":
                    case "uint32":
                    case "system.uint32":
                        if (uint.TryParse(v, out var u))
                        {
                            return(u);
                        }
                        break;

                    case "bool":
                    case "boolean":
                    case "system.boolean":
                        if (bool.TryParse(v, out var b))
                        {
                            return(b);
                        }
                        break;

                    case "long":
                    case "int64":
                    case "system.int64":
                        if (long.TryParse(v, out var l))
                        {
                            return(l);
                        }
                        break;

                    case "ulong":
                    case "uint64":
                    case "system.uint64":
                        if (ulong.TryParse(v, out var ul))
                        {
                            return(ul);
                        }
                        break;

                    case "string":
                    case "system.string":
                        return(v);

                    case "float":
                    case "single":
                    case "system.single":
                        if (float.TryParse(v, out var f))
                        {
                            return(f);
                        }
                        break;

                    case "double":
                    case "system.double":
                        if (double.TryParse(v, out var d))
                        {
                            return(d);
                        }
                        break;

                    case "decimal":
                    case "system.decimal":
                        if (decimal.TryParse(v, out var de))
                        {
                            return(de);
                        }
                        break;

                    case "byte":
                    case "uint8":
                    case "system.byte":
                        if (byte.TryParse(v, out var bt))
                        {
                            return(bt);
                        }
                        break;

                    case "int8":
                    case "sbyte":
                    case "system.sbyte":
                        if (sbyte.TryParse(v, out var sbt))
                        {
                            return(sbt);
                        }
                        break;

                    case "short":
                    case "int16":
                    case "system.int16":
                        if (short.TryParse(v, out var sh))
                        {
                            return(sh);
                        }
                        break;

                    case "ushort":
                    case "uint16":
                    case "system.uint16":
                        if (ushort.TryParse(v, out var ush))
                        {
                            return(ush);
                        }
                        break;
                    }
                }

                if (int.TryParse(s, out int i2))
                {
                    return(i2);
                }

                if (string.Equals(s, "true", StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }

                if (string.Equals(s, "false", StringComparison.OrdinalIgnoreCase))
                {
                    return(false);
                }

                return(s);
            }
            else if (value.ValueType == RuntimeValueType.Vector)
            {
                return(value.AsEnumerable().Select(ConvertToPSValue).ToArray());
            }
            else if (value.ValueType == RuntimeValueType.Map)
            {
                var hashTable = new Hashtable();
                foreach (var pair in value.AsDictionary())
                {
                    hashTable[pair.Key] = ConvertToPSValue(pair.Value);
                }

                return(hashTable);
            }
            else
            {
                return(null);
            }
        }