using Json.LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceClientGenerator
{
#region BaseModel
///
/// Used to outline the basics of any model for the service
/// They have a service model, json data, and documentation
///
public abstract class BaseModel
{
public ServiceModel model { get; protected set; }
public JsonData data { get; protected set; }
protected BaseModel(ServiceModel model, JsonData data)
{
this.model = model;
this.data = data;
}
public virtual string Documentation
{
get
{
var docNode = data[ServiceModel.DocumentationKey];
if (docNode == null)
return string.Empty;
return docNode.ToString();
}
}
}
#endregion
#region OperationInput
///
/// For some operations the xmlnamespace and locations are specified on the
/// operation:input. E.g. Route53 ChangeResourceRecordSets.
///
public class OperationInput : BaseModel
{
///
/// Creates an operation input through the BaseModel
///
/// The model of the service
/// The model as a jsonData object
public OperationInput(ServiceModel model, JsonData data)
: base(model, data)
{
}
///
/// The name of the location
///
public string LocationName
{
get
{
var node = this.data[ServiceModel.LocationNameKey];
if (node == null)
return string.Empty;
return node.ToString();
}
}
///
/// The namespace for the xml
///
public string XmlNamespace
{
get
{
return data[ServiceModel.XmlNamespaceKey] == null ? string.Empty :
(string)data[ServiceModel.XmlNamespaceKey][ServiceModel.XmlNamespaceUriKey];
//var node = this.data[ServiceModel.XmlNamespaceKey];
//if (node == null)
// return string.Empty;
//return node.ToString();
}
}
}
#endregion
#region Enumeration
///
/// Used to create the enumeration class for a service
///
public class Enumeration
{
public const string EnumKey = "enum";
ServiceModel model;
readonly string _modelName;
readonly string _outputName;
readonly JsonData _data;
///
/// Creates an enumeration that can model parts of the enumeration for the service
///
/// The service model that is using the enumartion
/// The name of the enumeration
/// The json data for the enumartion object
public Enumeration(ServiceModel model, string name, JsonData data)
{
this.model = model;
_modelName = name;
_data = data;
var overrideName = model.Customizations.GetOverrideShapeName(_modelName);
_outputName = !string.IsNullOrEmpty(overrideName)
? overrideName.ToUpperFirstCharacter()
: _modelName.ToUpperFirstCharacter();
}
///
/// The name of the enumeration as encoded in the original model.
///
public string ModelName
{
get { return this._modelName; }
}
///
/// The emitted name of the enumeration. If no customization is
/// applied to the shape, this is the same as ModelName.
///
public string Name
{
get { return _outputName; }
}
private string Customize(string typeName, string propertyName)
{
var custom = this.model.Customizations.GetPropertyModifier(typeName, propertyName);
if (custom != null)
return custom.EmitName;
return propertyName;
}
///
/// A list of all the values for the Enumeration found in the json model
///
public IEnumerable EnumerationValues
{
get
{
var enumValues = this._data[EnumKey];
var list = (from object value in enumValues
select new EnumEntry(value.ToString())).ToList();
foreach(var item in list)
{
var custom = this.model.Customizations.GetPropertyModifier(this.ModelName, item.MarshallName);
if (custom != null)
item.CustomPropertyName = custom.EmitName;
}
return list.OrderBy(x => x.PropertyName).ToList();
}
}
#region EnumEntry
///
/// An enum entry is used to represent a value in the enumeration
///
public class EnumEntry
{
///
/// Creates an enumentry that represents a value of the enumeration that contains this object
///
///
public EnumEntry(string name)
{
this.MarshallName = name;
}
///
/// The name of the marshaller for the EnumEntry
///
public string MarshallName
{
get;
private set;
}
private static readonly char[] CapitalizingSeparators =
{
'-', '/', '.', ' ', ':', ',', '+'
};
public string CustomPropertyName { get; set; }
///
/// Then name of the entry to be used by the generator to represnet the entry in the code
///
public string PropertyName
{
get
{
if (this.CustomPropertyName != null)
return this.CustomPropertyName;
var sb = new StringBuilder();
var tokens = this.MarshallName.Split(CapitalizingSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (var token in tokens)
{
var upperChar = token[0].ToString().ToUpper();
sb.Append(upperChar);
if (token.Length > 1)
{
var remainingName = token.Substring(1);
sb.Append(remainingName);
}
}
var result = sb
.Replace("(", "")
.Replace(")", "")
.ToString();
return result;
}
}
}
#endregion
}
#endregion
}