Exemplo n.º 1
0
 public Context(string name, Dictionary <string, JsonElement> args, ContextDeviceInfo deviceInfo, Dictionary <string, JsonElement> extra, string?id)
 {
     Name       = name;
     Args       = args;
     DeviceInfo = deviceInfo;
     Extra      = extra;
     Id         = $"{deviceInfo.Id}-{id ?? Helper.randomBytes(16)}";
 }
        public static Context DecodeContext(JsonElement json_, string path_)
        {
            if (json_.ValueKind != JsonValueKind.Object)
            {
                throw new SdkgenException("Fatal", $"'{path_}' must be an object");
            }
            JsonElement argsJson_;

            if (!json_.TryGetProperty("args", out argsJson_) || argsJson_.ValueKind != JsonValueKind.Object)
            {
                throw new SdkgenException("Fatal", $"'{path_}.args' must be an object");
            }
            var args = new Dictionary <string, JsonElement>();

            foreach (var property in argsJson_.EnumerateObject())
            {
                args.Add(property.Name, property.Value);
            }

            JsonElement       deviceInfoJson_;
            ContextDeviceInfo deviceInfo;

            if (json_.TryGetProperty("deviceInfo", out deviceInfoJson_))
            {
                deviceInfo = DecodeContextDeviceInfo(deviceInfoJson_, $"{path_}.deviceInfo");
            }
            else
            {
                deviceInfo = new ContextDeviceInfo(null, null, new Dictionary <string, JsonElement>(), null, null, null, null);
            }

            JsonElement extraJson_;
            var         extra = new Dictionary <string, JsonElement>();

            if (json_.TryGetProperty("extra", out extraJson_))
            {
                if (argsJson_.ValueKind != JsonValueKind.Object)
                {
                    throw new SdkgenException("Fatal", $"'{path_}.extra' must be set to an object");
                }
                foreach (var property in extraJson_.EnumerateObject())
                {
                    extra.Add(property.Name, property.Value);
                }
            }

            JsonElement nameJson_;

            if (!json_.TryGetProperty("name", out nameJson_))
            {
                throw new SdkgenException("Fatal", $"'{path_}.name' must be set to a value of type string");
            }
            string name;

            if (nameJson_.ValueKind != JsonValueKind.String)
            {
                throw new SdkgenException("Fatal", $"'{path_}.name' must be set to a value of type string");
            }
            name = nameJson_.GetString();

            JsonElement requestIdJson_;

            if (!json_.TryGetProperty("requestId", out requestIdJson_))
            {
                requestIdJson_ = new JsonElement();
            }
            string?requestId;

            if (requestIdJson_.ValueKind == JsonValueKind.Null || requestIdJson_.ValueKind == JsonValueKind.Undefined)
            {
                requestId = null;
            }
            else
            {
                if (requestIdJson_.ValueKind != JsonValueKind.String)
                {
                    throw new SdkgenException("Fatal", $"'{path_}.requestId' must be set to a value of type string");
                }
                requestId = requestIdJson_.GetString();
            }
            return(new Context(name, args, deviceInfo, extra, requestId));
        }