Esempio n. 1
0
        private static Parameter CreateParameterFromGrouping(IEnumerable <ParameterTransformation> grouping, Method method, ServiceClient serviceClient)
        {
            var    properties         = new List <Property>();
            string parameterGroupName = null;

            foreach (var parameter in grouping.Select(g => g.OutputParameter))
            {
                Newtonsoft.Json.Linq.JContainer extensionObject = parameter.Extensions[ParameterGroupExtension] as Newtonsoft.Json.Linq.JContainer;
                string specifiedGroupName = extensionObject.Value <string>("name");
                if (specifiedGroupName == null)
                {
                    string postfix = extensionObject.Value <string>("postfix") ?? "Parameters";
                    parameterGroupName = method.Group + "-" + method.Name + "-" + postfix;
                }
                else
                {
                    parameterGroupName = specifiedGroupName;
                }

                Property groupProperty = new Property()
                {
                    IsReadOnly   = false, //Since these properties are used as parameters they are never read only
                    Name         = parameter.Name,
                    IsRequired   = parameter.IsRequired,
                    DefaultValue = parameter.DefaultValue,
                    //Constraints = parameter.Constraints, Omit these since we don't want to perform parameter validation
                    Documentation  = parameter.Documentation,
                    Type           = parameter.Type,
                    SerializedName = null //Parameter is never serialized directly
                };
                properties.Add(groupProperty);
            }

            var parameterGroupType = new CompositeType()
            {
                Name          = parameterGroupName,
                Documentation = "Additional parameters for the " + method.Name + " operation."
            };

            //Add to the service client
            serviceClient.ModelTypes.Add(parameterGroupType);

            foreach (Property property in properties)
            {
                parameterGroupType.Properties.Add(property);
            }

            bool isGroupParameterRequired = parameterGroupType.Properties.Any(p => p.IsRequired);

            //Create the new parameter object based on the parameter group type
            return(new Parameter()
            {
                Name = parameterGroupName,
                IsRequired = isGroupParameterRequired,
                Location = ParameterLocation.None,
                SerializedName = string.Empty,
                Type = parameterGroupType,
                Documentation = "Additional parameters for the operation"
            });
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public MsgData GetBodyData()
        {
            MsgData msgdata = null;

            try
            {
                Newtonsoft.Json.Linq.JContainer data = JsonConvert.DeserializeObject <Newtonsoft.Json.Linq.JContainer>(this.Body);

                msgdata      = new MsgData();
                msgdata.from = data["from"].ToString();
                msgdata.to   = data["to"].ToString();
                msgdata.ext  = data["ext"];

                Newtonsoft.Json.Linq.JContainer bodies = data.Value <Newtonsoft.Json.Linq.JContainer>("bodies");

                msgdata.bodies = new BodyBase[bodies.Count];

                for (int i = 0; i < bodies.Count; i++)
                {
                    string type = bodies[i]["type"].ToString();

                    switch (type)
                    {
                    case "img":
                        msgdata.bodies[i] = JsonConvert.DeserializeObject <ImageBody>(bodies[i].ToString());
                        break;

                    case "txt":
                        msgdata.bodies[i] = JsonConvert.DeserializeObject <TextBody>(bodies[i].ToString());
                        break;

                    case "audio":
                        msgdata.bodies[i] = JsonConvert.DeserializeObject <AudioBody>(bodies[i].ToString());
                        break;

                    case "video":
                        msgdata.bodies[i] = JsonConvert.DeserializeObject <VideoBody>(bodies[i].ToString());
                        break;

                    case "loc":
                        msgdata.bodies[i] = JsonConvert.DeserializeObject <LocationBody>(bodies[i].ToString());
                        break;

                    case "file":
                        msgdata.bodies[i] = JsonConvert.DeserializeObject <FileBody>(bodies[i].ToString());
                        break;

                    default:
                        //msgdata.bodies[i] = JsonConvert.DeserializeObject<FileBody>(bodies[i].ToString());
                        break;
                    }
                }


                //msgdata = JsonConvert.DeserializeObject<MsgData>(this.Body);
            }
            catch (Exception ex)
            {
            }

            if (msgdata != null)
            {
            }

            return(msgdata);
        }
Esempio n. 3
0
        /// <summary>
        /// Adds the parameter groups to operation parameters.
        /// </summary>
        /// <param name="serviceClient"></param>
        public static void AddParameterGroups(ServiceClient serviceClient)
        {
            if (serviceClient == null)
            {
                throw new ArgumentNullException("serviceClient");
            }

            foreach (Method method in serviceClient.Methods)
            {
                //This group name is normalized by each languages code generator later, so it need not happen here.
                Dictionary <string, Dictionary <Property, Parameter> > parameterGroups = new Dictionary <string, Dictionary <Property, Parameter> >();

                foreach (Parameter parameter in method.Parameters)
                {
                    if (parameter.Extensions.ContainsKey(ParameterGroupExtension))
                    {
                        Newtonsoft.Json.Linq.JContainer extensionObject = parameter.Extensions[ParameterGroupExtension] as Newtonsoft.Json.Linq.JContainer;
                        if (extensionObject != null)
                        {
                            string parameterGroupName = method.Group + "-" + method.Name + "-" + "Parameters";
                            parameterGroupName = extensionObject.Value <string>("name") ?? parameterGroupName;

                            if (!parameterGroups.ContainsKey(parameterGroupName))
                            {
                                parameterGroups.Add(parameterGroupName, new Dictionary <Property, Parameter>());
                            }

                            Property groupProperty = new Property()
                            {
                                IsReadOnly   = false,   //Since these properties are used as parameters they are never read only
                                Name         = parameter.Name,
                                IsRequired   = parameter.IsRequired,
                                DefaultValue = parameter.DefaultValue,
                                //Constraints = parameter.Constraints, Omit these since we don't want to perform parameter validation
                                Documentation  = parameter.Documentation,
                                Type           = parameter.Type,
                                SerializedName = null     //Parameter is never serialized directly
                            };

                            parameterGroups[parameterGroupName].Add(groupProperty, parameter);
                        }
                    }
                }

                foreach (string parameterGroupName in parameterGroups.Keys)
                {
                    //Define the new parameter group type (it's always a composite type)
                    CompositeType parameterGroupType = new CompositeType()
                    {
                        Name          = parameterGroupName,
                        Documentation = "Additional parameters for the " + method.Name + " operation."
                    };

                    //Populate the parameter group type with properties.
                    foreach (Property property in parameterGroups[parameterGroupName].Keys)
                    {
                        parameterGroupType.Properties.Add(property);
                    }

                    //Add to the service client
                    serviceClient.ModelTypes.Add(parameterGroupType);

                    bool isGroupParameterRequired = parameterGroupType.Properties.Any(p => p.IsRequired);

                    //Create the new parameter object based on the parameter group type
                    Parameter parameterGroup = new Parameter()
                    {
                        Name           = parameterGroupName,
                        IsRequired     = isGroupParameterRequired,
                        Location       = ParameterLocation.None,
                        SerializedName = string.Empty,
                        Type           = parameterGroupType,
                        Documentation  = "Additional parameters for the operation"
                    };

                    method.Parameters.Add(parameterGroup);

                    //Link the grouped parameters to their parent, and remove them from the method parameters
                    foreach (Property property in parameterGroups[parameterGroupName].Keys)
                    {
                        Parameter p = parameterGroups[parameterGroupName][property];

                        method.InputParameterMappings.Add(new ParameterMapping
                        {
                            InputParameter         = parameterGroup,
                            OutputParameter        = p,
                            InputParameterProperty = property.Name
                        });
                        method.Parameters.Remove(p);
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Adds the parameter groups to operation parameters.
        /// </summary>
        /// <param name="serviceClient"></param>
        public static void AddParameterGroups(ServiceClient serviceClient)
        {
            if (serviceClient == null)
            {
                throw new ArgumentNullException("serviceClient");
            }

            HashSet <CompositeType> generatedParameterGroups = new HashSet <CompositeType>();

            foreach (Method method in serviceClient.Methods)
            {
                //This group name is normalized by each languages code generator later, so it need not happen here.
                Dictionary <string, Dictionary <Property, Parameter> > parameterGroups = new Dictionary <string, Dictionary <Property, Parameter> >();

                foreach (Parameter parameter in method.Parameters)
                {
                    if (parameter.Extensions.ContainsKey(ParameterGroupExtension))
                    {
                        Newtonsoft.Json.Linq.JContainer extensionObject = parameter.Extensions[ParameterGroupExtension] as Newtonsoft.Json.Linq.JContainer;
                        if (extensionObject != null)
                        {
                            string specifiedGroupName = extensionObject.Value <string>("name");
                            string parameterGroupName;
                            if (specifiedGroupName == null)
                            {
                                string postfix = extensionObject.Value <string>("postfix") ?? "Parameters";
                                parameterGroupName = method.Group + "-" + method.Name + "-" + postfix;
                            }
                            else
                            {
                                parameterGroupName = specifiedGroupName;
                            }

                            if (!parameterGroups.ContainsKey(parameterGroupName))
                            {
                                parameterGroups.Add(parameterGroupName, new Dictionary <Property, Parameter>());
                            }

                            Property groupProperty = new Property()
                            {
                                IsReadOnly   = false, //Since these properties are used as parameters they are never read only
                                Name         = parameter.Name,
                                IsRequired   = parameter.IsRequired,
                                DefaultValue = parameter.DefaultValue,
                                //Constraints = parameter.Constraints, Omit these since we don't want to perform parameter validation
                                Documentation  = parameter.Documentation,
                                Type           = parameter.Type,
                                SerializedName = null //Parameter is never serialized directly
                            };

                            parameterGroups[parameterGroupName].Add(groupProperty, parameter);
                        }
                    }
                }

                foreach (string parameterGroupName in parameterGroups.Keys)
                {
                    CompositeType parameterGroupType =
                        generatedParameterGroups.FirstOrDefault(item => item.Name == parameterGroupName);
                    bool createdNewCompositeType = false;
                    if (parameterGroupType == null)
                    {
                        parameterGroupType = new CompositeType()
                        {
                            Name          = parameterGroupName,
                            Documentation = "Additional parameters for the " + method.Name + " operation."
                        };
                        generatedParameterGroups.Add(parameterGroupType);

                        //Populate the parameter group type with properties.

                        //Add to the service client
                        serviceClient.ModelTypes.Add(parameterGroupType);
                        createdNewCompositeType = true;
                    }

                    foreach (Property property in parameterGroups[parameterGroupName].Keys)
                    {
                        //Either the paramter group is "empty" since it is new, or it is "full" and we don't allow different schemas
                        if (createdNewCompositeType)
                        {
                            parameterGroupType.Properties.Add(property);
                        }
                        else
                        {
                            Property matchingProperty = parameterGroupType.Properties.FirstOrDefault(
                                item => item.Name == property.Name &&
                                item.IsReadOnly == property.IsReadOnly &&
                                item.DefaultValue == property.DefaultValue &&
                                item.SerializedName == property.SerializedName);

                            if (matchingProperty == null)
                            {
                                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Property {0} was specified on group {1} but it is not on shared parameter group object {2}",
                                                                          property.Name, method.Name, parameterGroupType.Name));
                            }
                        }
                    }

                    bool isGroupParameterRequired = parameterGroupType.Properties.Any(p => p.IsRequired);

                    //Create the new parameter object based on the parameter group type
                    Parameter parameterGroup = new Parameter()
                    {
                        Name           = parameterGroupName,
                        IsRequired     = isGroupParameterRequired,
                        Location       = ParameterLocation.None,
                        SerializedName = string.Empty,
                        Type           = parameterGroupType,
                        Documentation  = "Additional parameters for the operation"
                    };

                    method.Parameters.Add(parameterGroup);

                    //Link the grouped parameters to their parent, and remove them from the method parameters
                    foreach (Property property in parameterGroups[parameterGroupName].Keys)
                    {
                        Parameter p = parameterGroups[parameterGroupName][property];

                        var parameterTransformation = new ParameterTransformation
                        {
                            OutputParameter = p
                        };
                        parameterTransformation.ParameterMappings.Add(new ParameterMapping
                        {
                            InputParameter         = parameterGroup,
                            InputParameterProperty = property.Name
                        });
                        method.InputParameterTransformation.Add(parameterTransformation);
                        method.Parameters.Remove(p);
                    }
                }
            }
        }