Wrapper around string builder to help emit properly indented code
Exemplo n.º 1
0
        /// <summary>
        /// Given a Shape and sample data, build a literal/instantiation for the
        /// Shape's type with the sample data.
        /// </summary>
        /// <param name="shape">The Shape in the model</param>
        /// <param name="data">Sample data to populate the literal with</param>
        /// <param name="cb">A CodeBuilder instance to write the code to.</param>
        public void GetSampleLiteral(Shape shape, JsonData data, CodeBuilder cb)
        {
            if (shape.IsString && data.IsString)
            {
                cb.AppendQuote(data.ToString());
            }
            else if (shape.IsBoolean)
            {
                cb.Append(data.ToString().ToLower());
            }
            else if (shape.IsFloat || shape.IsInt || shape.IsDouble || shape.IsLong)
            {
                cb.Append(data.ToString());
            }

            else if (shape.IsList && data.IsArray)
            {
                var itemType = shape.ListShape;

                cb.AppendFormat("new List<{0}> ", ShapeType(itemType)).OpenBlock();

                for (int i = 0; i < data.Count; i++)
                {
                    GetSampleLiteral(itemType, data[i], cb);
                    if (i < (data.Count - 1))
                    {
                        cb.AppendLine(",");
                    }
                }

                cb.CloseBlock();
            }

            else if (shape.IsMap && data.IsObject)
            {
                var keyType = shape.KeyShape;
                var valType = shape.ValueShape;

                cb.AppendFormat("new Dictionary<{0}, {1}> ", ShapeType(keyType), ShapeType(valType));

                cb.OpenBlock();

                foreach (var k in data.PropertyNames)
                {
                    cb.Append("{ ");

                    GetSampleLiteral(keyType, k, cb);
                    cb.Append(", ");
                    GetSampleLiteral(valType, data[k], cb);

                    cb.Append(" }");
                    if (k != data.PropertyNames.Last())
                    {
                        cb.AppendLine(",");
                    }
                }

                cb.CloseBlock();
            }

            else if (shape.IsStructure && data.IsObject)
            {
                cb.AppendFormat("new {0} ", ShapeType(shape));

                if (data.PropertyNames.Count() > 1)
                {
                    cb.OpenBlock();
                }
                else
                {
                    cb.Append("{ ");
                }

                foreach (var field in data.PropertyNames)
                {
                    var property = shape.Members.GetMemberByName(field);

                    if (null == property)
                    {
                        continue;
                    }

                    cb.AppendFormat("{0} = ", property.PropertyName);
                    GetSampleLiteral(property, data[field], cb);

                    if (field != data.PropertyNames.Last())
                    {
                        cb.AppendLine(",");
                    }
                }

                if (data.PropertyNames.Count() > 1)
                {
                    cb.CloseBlock();
                }
                else
                {
                    cb.Append(" }");
                }
            }

            else if (shape.IsBlob && shape.IsMemoryStream && data.IsString)
            {
                cb.AppendFormat("new {0}({1})", ShapeType(shape), data.ToString());
            }

            else if (shape.IsDateTime)
            {
                string exampleValue = null;

                if (data.IsString)
                {
                    var      textValue = data.ToString();
                    DateTime parsedDateTime;
                    if (DateTime.TryParse(textValue, out parsedDateTime))
                    {
                        exampleValue = string.Format("new DateTime({0})",
                                                     parsedDateTime.ToString("yyyy, M, d, h, m, s"));
                    }
                }

                if (string.IsNullOrEmpty(exampleValue))
                {
                    exampleValue = "DateTime.Now";
                }

                cb.Append(exampleValue);
            }

            else
            {
                // default value
                cb.Append("<data>");
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Given a member and sample data, build a literal/instantation for the
 /// member's type with the sample data.
 /// </summary>
 /// <param name="member">The member in the model</param>
 /// <param name="data">Sample data to populate the literal with</param>
 /// <param name="cb">A CodeBuilder instance to write the code to.</param>
 public void GetSampleLiteral(Member member, JsonData data, CodeBuilder cb)
 {
     GetSampleLiteral(member.Shape, data, cb);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Given a Shape and sample data, build a literal/instantiation for the
        /// Shape's type with the sample data.
        /// </summary>
        /// <param name="shape">The Shape in the model</param>
        /// <param name="data">Sample data to populate the literal with</param>
        /// <param name="cb">A CodeBuilder instance to write the code to.</param>
        public void GetSampleLiteral(Shape shape, JsonData data, CodeBuilder cb)
        {
            if (shape.IsString && data.IsString)
                cb.AppendQuote(data.ToString());
            if (shape.IsBoolean)
                cb.Append(data.ToString().ToLower());
            if (shape.IsFloat || shape.IsInt || shape.IsDouble || shape.IsLong)
                cb.Append(data.ToString());

            if (shape.IsList && data.IsArray)
            {
                var itemType = shape.ListShape;

                cb.AppendFormat("new List<{0}> ", ShapeType(itemType)).OpenBlock();

                for (int i = 0; i < data.Count; i++)
                {
                    GetSampleLiteral(itemType, data[i], cb);
                    if (i < (data.Count - 1))
                        cb.AppendLine(",");
                }

                cb.CloseBlock();
            }

            if (shape.IsMap && data.IsObject)
            {
                var keyType = shape.KeyShape;
                var valType = shape.ValueShape;

                cb.AppendFormat("new Dictionary<{0}, {1}> ", ShapeType(keyType), ShapeType(valType));

                cb.OpenBlock();

                foreach (var k in data.PropertyNames)
                {
                    cb.Append("{ ");

                    GetSampleLiteral(keyType, k, cb);
                    cb.Append(", ");
                    GetSampleLiteral(valType, data[k], cb);

                    cb.Append(" }");
                    if (k != data.PropertyNames.Last())
                        cb.AppendLine(",");
                }

                cb.CloseBlock();

            }

            if (shape.IsStructure && data.IsObject)
            {
                cb.AppendFormat("new {0} ", ShapeType(shape));

                if (data.PropertyNames.Count() > 1)
                    cb.OpenBlock();
                else
                    cb.Append("{ ");

                foreach (var field in data.PropertyNames)
                {
                    var property = shape.Members.GetMemberByName(field);
 
                    if (null == property)
                        continue;

                    cb.AppendFormat("{0} = ", property.PropertyName);
                    GetSampleLiteral(property, data[field], cb);

                    if (field != data.PropertyNames.Last())
                        cb.AppendLine(",");

                }

                if (data.PropertyNames.Count() > 1)
                    cb.CloseBlock();
                else
                    cb.Append(" }");
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Given a member and sample data, build a literal/instantation for the
 /// member's type with the sample data.
 /// </summary>
 /// <param name="member">The member in the model</param>
 /// <param name="data">Sample data to populate the literal with</param>
 /// <param name="cb">A CodeBuilder instance to write the code to.</param>
 public void GetSampleLiteral(Member member, JsonData data, CodeBuilder cb)
 {
     GetSampleLiteral(member.Shape, data, cb);
 }
Exemplo n.º 5
0
        /// <summary>
        /// For the request, build the literals and/or instantiators to assign to each
        /// property in the request shape for which sample data was supplied in the example.
        /// </summary>
        /// <returns>A list of strings of the form 'PropertyName = value' with comments at the
        /// end, if present.</returns>
        public IList<string> GetRequestAssignments(int currentIndent)
        {
            var result = new List<string>();
            var last = InputParameters.Last().Key;
            foreach (var param in InputParameters)
            {
                var member = Operation.RequestStructure.Members.GetMemberByName(param.Key);

                if (null == member)
                    continue;

                var sb = new StringBuilder();
                var cb = new CodeBuilder(sb, currentIndent);

                cb.Append(member.PropertyName).Append(" = ");
                GetSampleLiteral(member, param.Value, cb);
                if (param.Key != last)
                    cb.Append(",");
                if (InputComments.ContainsKey(param.Key))
                    cb.Append(" // ").Append(InputComments[param.Key]);

                result.Add(sb.ToString());
            }

            return result;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Given a Shape and sample data, build a literal/instantiation for the
        /// Shape's type with the sample data.
        /// </summary>
        /// <param name="shape">The Shape in the model</param>
        /// <param name="data">Sample data to populate the literal with</param>
        /// <param name="cb">A CodeBuilder instance to write the code to.</param>
        public void GetSampleLiteral(Shape shape, JsonData data, CodeBuilder cb)
        {
            if (shape.IsString && data.IsString)
            {
                cb.AppendQuote(data.ToString());
            }
            if (shape.IsBoolean)
            {
                cb.Append(data.ToString().ToLower());
            }
            if (shape.IsFloat || shape.IsInt || shape.IsDouble || shape.IsLong)
            {
                cb.Append(data.ToString());
            }

            if (shape.IsList && data.IsArray)
            {
                var itemType = shape.ListShape;

                cb.AppendFormat("new List<{0}> ", ShapeType(itemType)).OpenBlock();

                for (int i = 0; i < data.Count; i++)
                {
                    GetSampleLiteral(itemType, data[i], cb);
                    if (i < (data.Count - 1))
                    {
                        cb.AppendLine(",");
                    }
                }

                cb.CloseBlock();
            }

            if (shape.IsMap && data.IsObject)
            {
                var keyType = shape.KeyShape;
                var valType = shape.ValueShape;

                cb.AppendFormat("new Dictionary<{0}, {1}> ", ShapeType(keyType), ShapeType(valType));

                cb.OpenBlock();

                foreach (var k in data.PropertyNames)
                {
                    cb.Append("{ ");

                    GetSampleLiteral(keyType, k, cb);
                    cb.Append(", ");
                    GetSampleLiteral(valType, data[k], cb);

                    cb.Append(" }");
                    if (k != data.PropertyNames.Last())
                    {
                        cb.AppendLine(",");
                    }
                }

                cb.CloseBlock();
            }

            if (shape.IsStructure && data.IsObject)
            {
                cb.AppendFormat("new {0} ", ShapeType(shape));

                if (data.PropertyNames.Count() > 1)
                {
                    cb.OpenBlock();
                }
                else
                {
                    cb.Append("{ ");
                }

                foreach (var field in data.PropertyNames)
                {
                    var property = shape.Members.GetMemberByName(field);

                    if (null == property)
                    {
                        continue;
                    }

                    cb.AppendFormat("{0} = ", property.PropertyName);
                    GetSampleLiteral(property, data[field], cb);

                    if (field != data.PropertyNames.Last())
                    {
                        cb.AppendLine(",");
                    }
                }

                if (data.PropertyNames.Count() > 1)
                {
                    cb.CloseBlock();
                }
                else
                {
                    cb.Append(" }");
                }
            }
        }