private void WriteStructure(JsonWriter writer, Shape structure) { if (structure.Payload != null) { this.WriteStructure(writer, structure.Members[0].Shape); return; } writer.WriteObjectStart(); foreach (var member in structure.Members) { writer.WritePropertyName(member.MarshallName); if (member.OverrideDataType != null && string.Equals(member.OverrideDataType.Unmarshaller, "DateTimeEpochLongMillisecondsUnmarshaller")) { var ticks = Constants.DEFAULT_DATE.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks; writer.Write((long)TimeSpan.FromTicks(ticks).TotalMilliseconds); } else if (member.OverrideDataType != null && string.Equals(member.OverrideDataType.Unmarshaller, "Amazon.Runtime.Internal.Transform.DateTimeUnmarshaller")) { writer.Write(Constants.DEFAULT_DATE.ToString(AWSSDKUtils.ISO8601DateFormat, CultureInfo.InvariantCulture)); } else { this.Write(writer, member.Shape); } } writer.WriteObjectEnd(); }
private void Write(JsonWriter writer, Shape shape) { if (shape.IsStructure) WriteStructure(writer, shape); else if (shape.IsList) WriteArray(writer, shape); else if (shape.IsMap) WriteMap(writer, shape); else if (shape.IsEnum) { var enumerationWrapper = this._model.Enumerations.First(x => x.Name == shape.Name); writer.Write(enumerationWrapper.EnumerationValues.ElementAt(0).MarshallName); } else if (shape.IsString) writer.Write(shape.Name + "_Value"); else if (shape.IsInt) writer.Write(int.MaxValue); else if (shape.IsLong) writer.Write(long.MaxValue); else if (shape.IsDouble) writer.Write(double.MaxValue); else if (shape.IsFloat) writer.Write(float.MaxValue); else if (shape.IsDateTime) writer.Write(Constants.DEFAULT_DATE); else if (shape.IsBoolean) writer.Write(true); else if (shape.IsBlob) writer.Write(Constants.DEFAULT_BLOB_ENCODED); else throw new Exception("Unknown Type for shape " + shape.Name); }
public Member(ServiceModel model, Shape owningShape, string name, string defaultMarshallName, JsonData data, CustomizationsModel.PropertyModifier propertyModifier) : base(model, data) { this.OwningShape = owningShape; _name = name; _defaultMarshallName = defaultMarshallName; this.PropertyModifier = propertyModifier; this.PropertyInjector = null; }
public void Validate(string parameterBase, object owningObject, Shape shape) { var owningType = owningObject.GetType(); foreach (var info in owningType.GetProperties()) { if (info.SetMethod == null || info.Name == "ContentLength") continue; var member = shape.Members.FirstOrDefault(x => x.PropertyName == info.Name); // Not a modeled property so skip if (member == null) continue; // auto-generated code to marshall wasn't used. skip if (GeneratorHelpers.UseCustomMarshall(member, this.Operation)) continue; var type = info.PropertyType; var propertyValue = info.GetMethod.Invoke(owningObject, new object[] { }); ValidateProperty(parameterBase, type, propertyValue, info.Name, member); } }
private void WriteStructure(XmlWriter writer, Shape structure) { foreach (var member in structure.Members) { if (member.IsInHeader) continue; if (member.Shape.IsMap) { WriteMap(writer, member.MarshallName, member.Shape); // map not used by EC2 protocol } else if (member.Shape.IsList) { WriteArray(writer, member, member.Shape); } else { // to allow for EC2's substitution of AttributeValue et al structure types with simple values, // eg request.Description => Description.Value after marshall, we potentially need to create // sub elements for each '.' subexpression and wind back after writing the actual value var unmarshallName = GeneratorHelpers.DetermineAWSQueryTestExpression(member); var nameElements = unmarshallName.Split('/'); foreach (var el in nameElements) { writer.WriteStartElement(el); } this.Write(writer, member.Shape); for (var i = 0; i < nameElements.Length; i++) { writer.WriteEndElement(); } } } }
public JsonSampleGenerator(ServiceModel model, Shape rootStructure) { this._model = model; this._rootStructure = rootStructure; }
private void WriteMap(JsonWriter writer, Shape map) { writer.WriteObjectStart(); var mapShape = map.ValueShape; if (!mapShape.IsStructure || !this._tcr.Contains(mapShape.Name)) { for (int i = 0; i < map.Name.Length % 5 + 2; i++) { writer.WritePropertyName("key" + i); Write(writer, map.ValueShape); } } writer.WriteObjectEnd(); }
private void WriteArray(JsonWriter writer, Shape array) { writer.WriteArrayStart(); var listShape = array.ListShape; if (!listShape.IsStructure || !this._tcr.Contains(listShape.Name)) { for (int i = 0; i < array.Name.Length % 5 + 2; i++) { Write(writer, listShape); } } writer.WriteArrayEnd(); }
private void WriteMap(JsonWriter writer, Shape map) { writer.WriteObjectStart(); for (int i = 0; i < map.Name.Length % 5 + 2; i++) { writer.WritePropertyName("key" + i); Write(writer, map.ValueShape); } writer.WriteObjectEnd(); }
/// <summary> /// Return the type name for a shape /// </summary> /// <param name="shape">The shape to get the type name for</param> /// <returns></returns> private string ShapeType(Shape shape) { if (shape.IsPrimitiveType) return shape.Type; if (shape.IsMap) return string.Format("Dictionary<{0}, {1}>", ShapeType(shape.KeyShape), ShapeType(shape.ValueShape)); if (shape.IsList) return string.Format("List<{0}>", ShapeType(shape.ListShape)); if (shape.IsStructure) return shape.Name; throw new InvalidOperationException(string.Format("Unable to resolve type for shape {0}", shape.Name)); }
/// <summary> /// Function that recursively searches for structures of a given shape /// </summary> /// <param name="structure">The shape to look for recursive structures in</param> public void SearchForNestedStructures(Shape structure) { if (NestedStructures.Contains(structure)) return; if (structure.IsStructure) NestedStructures.Add(structure); if (structure.IsList) { if (structure.ListShape.IsStructure || structure.ListShape.IsList || structure.ListShape.IsMap) { SearchForNestedStructures(structure.ListShape); } } else if (structure.IsMap) { if (structure.ValueShape.IsStructure || structure.ValueShape.IsList || structure.ValueShape.IsMap) { SearchForNestedStructures(structure.ValueShape); } } else if (structure.IsStructure) { foreach (var member in structure.Members) { SearchForNestedStructures(member.Shape); } } }
private void DetermineStructuresToProcess(Shape containingShape, bool includeContainingShape) { if (containingShape.IsStructure) { if (this._structuresToProcess.Contains(containingShape)) return; else if (includeContainingShape) this._structuresToProcess.Add(containingShape); foreach (var member in containingShape.Members) { if (member.IsStructure) { DetermineStructuresToProcess(member.Shape, true); } else if (member.IsList) { DetermineStructuresToProcess(member.Shape.ListShape, true); } else if (member.IsMap) { DetermineStructuresToProcess(member.Shape.ValueShape, true); } } } else if (containingShape.IsList) { DetermineStructuresToProcess(containingShape.ListShape, true); } else if (containingShape.IsMap) { DetermineStructuresToProcess(containingShape.ValueShape, true); } }
/// <summary> /// Creates a model that represents an exception in the service processed in the response /// </summary> /// <param name="data">The json data that contains information about the exception found in the model</param> /// <param name="name">The name of the exception</param> /// <param name="documentation">The documentation for the exception found in the service model json</param> /// <param name="structure">The shape that represents the exception</param> public ExceptionModel(JsonData data, string name, string documentation, Shape structure) { this._data = data; this._name = name; this._documentation = documentation; this._structure = structure; }
private void WriteMap(XmlWriter writer, string memberMarshallName, Shape shape) { if (!shape.IsFlattened) writer.WriteStartElement(memberMarshallName); for (int i = 0; i < shape.Name.Length % 5 + 2; i++) { if (!shape.IsFlattened) writer.WriteStartElement("entry"); else writer.WriteStartElement(memberMarshallName); writer.WriteStartElement(shape.KeyMarshallName); writer.WriteValue("Key" + i); writer.WriteEndElement(); writer.WriteStartElement(shape.ValueMarshallName); Write(writer, shape.ValueShape); writer.WriteEndElement(); writer.WriteEndElement(); } if (!shape.IsFlattened) writer.WriteEndElement(); }
private void WriteArray(XmlWriter writer, Member member, Shape shape) { if (!shape.IsFlattened) { writer.WriteStartElement(GeneratorHelpers.DetermineAWSQueryBaseUnmarshallName(member)); } for (int i = 0; i < shape.Name.Length % 5 + 2; i++) { writer.WriteStartElement(shape.ListMarshallName ?? "member"); Write(writer, shape.ModelListShape); writer.WriteEndElement(); } if (!shape.IsFlattened) writer.WriteEndElement(); }
private void WriteArray(JsonWriter writer, Shape array) { writer.WriteArrayStart(); for (int i = 0; i < array.Name.Length % 5 + 2; i++) { Write(writer, array.ListShape); } writer.WriteArrayEnd(); }
private void GenerateProperyValueRules(ServiceConfiguration serviceConfiguration, XmlWriter writer, string shapeName, Shape shape) { foreach (var member in shape.Members) { var memberShape = member.Shape; if (!memberShape.IsPrimitiveType) continue; if (memberShape.Min == null && memberShape.Max == null && memberShape.Pattern == null) continue; writer.WriteStartElement("property-value-rule"); var propertyName = string.Format("{0}.Model.{1}.{2}", serviceConfiguration.Namespace, shapeName, member.PropertyName); writer.WriteElementString("property", propertyName); if (memberShape.Min != null) writer.WriteElementString("min", memberShape.Min.Value.ToString()); if (memberShape.Max != null) writer.WriteElementString("max", memberShape.Max.Value.ToString()); if (memberShape.Pattern != null) { try { // Make sure we can compile the expression new System.Text.RegularExpressions.Regex(memberShape.Pattern); writer.WriteElementString("pattern", memberShape.Pattern); } catch(Exception e) { Console.Error.WriteLine("Failed to compile regex {0} for property {1}: {2}", memberShape.Pattern, propertyName, e.Message); } } writer.WriteEndElement(); } }
/// <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(" }"); } }