public static string ToStringTemplateForType(IModelTypeJv modelType) { string template = "{0}"; string modelTypeName = modelType.DeclarationName; if (modelType is AutoRest.Java.Model.PrimaryTypeJv) { if (modelTypeName.Equals("UUID")) { // { // ....... // "type": "string", // "format": "uuid" // } template = "UUID.fromString({0})"; } else if (modelTypeName.Equals("int")) { // { // ....... // "type": "integer" // } template = "Integer.parseInt({0})"; } else if (modelTypeName.Equals("double")) { // { // ....... // "type": "number", // "format": "float" // } template = "Double.parseDouble({0})"; } else if (modelTypeName.Equals("DateTime")) { // { // ....... // "type": "string", // "format": "date-time" // } template = "DateTime.parse({0})"; } } else if (modelType is AutoRest.Java.Azure.Fluent.Model.EnumTypeJvaf mtEnum) { if (mtEnum.ModelAsString) { // com.microsoft.rest.ExpandableStringEnum // template = $"{modelTypeName}.fromString({{0}})"; } else { // Native Enum // template = $"{modelTypeName}.valueOf({{0}})"; } } return(template); }
private string convertToClientType(IModelTypeJv type, string source, string target, int level = 0) { if (type == null) { return(target + " = " + source + ";"); } IndentedStringBuilder builder = new IndentedStringBuilder(); var sequenceType = type as SequenceTypeJv; var dictionaryType = type as DictionaryTypeJv; if (sequenceType != null) { var elementType = sequenceType.ElementType as IModelTypeJv; var itemName = string.Format(CultureInfo.InvariantCulture, "item{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture)); var itemTarget = string.Format(CultureInfo.InvariantCulture, "value{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture)); builder.AppendLine("{0} = new ArrayList<{1}>();", target, elementType.ResponseVariant.Name) .AppendLine("for ({0} {1} : {2}) {{", elementType.Name, itemName, source) .Indent().AppendLine("{0} {1};", elementType.ResponseVariant.Name, itemTarget) .AppendLine(convertToClientType(elementType, itemName, itemTarget, level + 1)) .AppendLine("{0}.add({1});", target, itemTarget) .Outdent().Append("}"); _implImports.Add("java.util.ArrayList"); return(builder.ToString()); } else if (dictionaryType != null) { var valueType = dictionaryType.ValueType as IModelTypeJv; var itemName = string.Format(CultureInfo.InvariantCulture, "entry{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture)); var itemTarget = string.Format(CultureInfo.InvariantCulture, "value{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture)); builder.AppendLine("{0} = new HashMap<String, {1}>();", target, valueType.ResponseVariant.Name) .AppendLine("for (Map.Entry<String, {0}> {1} : {2}.entrySet()) {{", valueType.Name, itemName, source) .Indent().AppendLine("{0} {1};", valueType.ResponseVariant.Name, itemTarget) .AppendLine(convertToClientType(valueType, itemName + ".getValue()", itemTarget, level + 1)) .AppendLine("{0}.put({1}.getKey(), {2});", target, itemName, itemTarget) .Outdent().Append("}"); _implImports.Add("java.util.HashMap"); return(builder.ToString()); } else if (type.IsPrimaryType(KnownPrimaryType.DateTimeRfc1123)) { return(target + " = " + source + ".dateTime();"); } else if (type.IsPrimaryType(KnownPrimaryType.UnixTime)) { return(target + " = new DateTime(" + source + " * 1000L, DateTimeZone.UTC);"); } else if (type.IsPrimaryType(KnownPrimaryType.Base64Url)) { return(target + " = " + source + ".decodedBytes();"); } else { return(target + " = " + source + ";"); } }
public static bool HasProperty(IModelTypeJv type, string propertyName) { if (type is CompositeTypeJv compositeType) { return(compositeType.ComposedProperties.Any(p => p.SerializedName.EqualsIgnoreCase("name"))); } else { return(false); } }
/// <summary> /// Creates FluentDefinitionOrUpdateStageMethod describing a method in a Fluent "Defintion" or "Update" stage. /// </summary> /// <param name="methodName">the method name</param> /// <param name="parameterDeclaration">The string containing method parameter declaration</param> /// <param name="parameterType">The parameter type</param> public FluentDefinitionOrUpdateStageMethod(string methodName, string parameterDeclaration, IModelTypeJv parameterType) : this(methodName, parameterDeclaration, new List <IModelTypeJv>() { parameterType }) { }
public ResponseJva(IModelTypeJv body, IModelTypeJv headers) : base(body, headers) { }
/// <summary> /// Given an ARM operation endpoint Uri, derive a "Segment Fluent Method Group" that the operation can possibly belongs to. /// </summary> /// <param name="fluentMethodGroups">the map holding all "Segment Fluent Method Group"s</param> /// <param name="innerMethod">inner Swagger method</param> /// <param name="segments">the ARM operation endpoint uri segments those appear after Provider name</param> /// <returns>The segment fluent method group</returns> public static SegmentFluentMethodGroup ResolveFluentMethodGroup(SegmentFluentMethodGroups fluentMethodGroups, MethodJvaf innerMethod, IEnumerable <Segment> segments, string defaultMethodGroupName) { List <string> fluentMethodGroupNamesInSegments = new List <string>(); Pluralizer pluralizer = new Pluralizer(); HttpMethod httpMethod = innerMethod.HttpMethod; segments .Where(segment => !(segment is PositionalSegment) && Utils.IsPlural(segment.Name, fluentMethodGroups.FluentConfig)) .ForEach(segment => { fluentMethodGroupNamesInSegments.Add(segment.Name); }); // if (fluentMethodGroupNamesInSegments.Count() == 0) { // Level 0 "Fluent Method Group" return(new SegmentFluentMethodGroup(fluentMethodGroups: fluentMethodGroups, localName: defaultMethodGroupName, parentMethodGroupNames: new List <string>())); } if (fluentMethodGroupNamesInSegments.Count() == 1) { // Level 0 "Fluent Method Group" return(new SegmentFluentMethodGroup(fluentMethodGroups: fluentMethodGroups, localName: fluentMethodGroupNamesInSegments[0], parentMethodGroupNames: new List <string>())); } else if (httpMethod == HttpMethod.Post) { if (segments.Last() is TerminalSegment && segments.Last().Name.EqualsIgnoreCase(fluentMethodGroupNamesInSegments.Last())) { //POST /providers/Microsoft.EventHub/namespaces/{nsname}/authorizationRules/{ruleName}/listKeys // return(new SegmentFluentMethodGroup(fluentMethodGroups: fluentMethodGroups, localName: fluentMethodGroupNamesInSegments.SkipLast(1).Last(), parentMethodGroupNames: fluentMethodGroupNamesInSegments.SkipLast(2).ToList())); } else { return(new SegmentFluentMethodGroup(fluentMethodGroups: fluentMethodGroups, localName: fluentMethodGroupNamesInSegments.Last(), parentMethodGroupNames: fluentMethodGroupNamesInSegments.SkipLast(1).ToList())); } } else { IModelTypeJv retType = innerMethod.ReturnTypeJva.BodyClientType; if ((httpMethod == HttpMethod.Get || httpMethod == HttpMethod.Put) && (retType is PrimaryType || (retType as SequenceType)?.ElementType is PrimaryType)) { return(new SegmentFluentMethodGroup(fluentMethodGroups: fluentMethodGroups, localName: fluentMethodGroupNamesInSegments.SkipLast(1).Last(), parentMethodGroupNames: fluentMethodGroupNamesInSegments.SkipLast(2).ToList())); } else { return(new SegmentFluentMethodGroup(fluentMethodGroups: fluentMethodGroups, localName: fluentMethodGroupNamesInSegments.Last(), parentMethodGroupNames: fluentMethodGroupNamesInSegments.SkipLast(1).ToList())); } } }
private string convertClientTypeToWireType(IModelTypeJv wireType, string source, string target, string clientReference, int level = 0) { IndentedStringBuilder builder = new IndentedStringBuilder(); if (wireType.IsPrimaryType(KnownPrimaryType.DateTimeRfc1123)) { if (!IsRequired) { builder.AppendLine("DateTimeRfc1123 {0} = {1};", target, wireType.GetDefaultValue(Method) ?? "null") .AppendLine("if ({0} != null) {{", source).Indent(); } builder.AppendLine("{0}{1} = new DateTimeRfc1123({2});", IsRequired ? "DateTimeRfc1123 " : "", target, source); if (!IsRequired) { builder.Outdent().AppendLine("}"); } } else if (wireType.IsPrimaryType(KnownPrimaryType.UnixTime)) { if (!IsRequired) { builder.AppendLine("Long {0} = {1};", target, wireType.GetDefaultValue(Method) ?? "null") .AppendLine("if ({0} != null) {{", source).Indent(); } builder.AppendLine("{0}{1} = {2}.toDateTime(DateTimeZone.UTC).getMillis() / 1000;", IsRequired ? "Long " : "", target, source); } else if (wireType.IsPrimaryType(KnownPrimaryType.Base64Url)) { if (!IsRequired) { builder.AppendLine("Base64Url {0} = {1};", target, wireType.GetDefaultValue(Method) ?? "null") .AppendLine("if ({0} != null) {{", source).Indent(); } builder.AppendLine("{0}{1} = Base64Url.encode({2});", IsRequired ? "Base64Url " : "", target, source); if (!IsRequired) { builder.Outdent().AppendLine("}"); } } else if (wireType.IsPrimaryType(KnownPrimaryType.Stream)) { if (!IsRequired) { builder.AppendLine("RequestBody {0} = {1};", target, wireType.GetDefaultValue(Method) ?? "null") .AppendLine("if ({0} != null) {{", source).Indent(); } builder.AppendLine("{0}{1} = RequestBody.create(MediaType.parse(\"{2}\"), {3});", IsRequired ? "RequestBody " : "", target, Method.RequestContentType, source); if (!IsRequired) { builder.Outdent().AppendLine("}"); } } else if (wireType is SequenceTypeJv) { if (!IsRequired) { builder.AppendLine("{0} {1} = {2};", WireType.Name, target, wireType.GetDefaultValue(Method) ?? "null") .AppendLine("if ({0} != null) {{", source).Indent(); } var sequenceType = wireType as SequenceTypeJv; var elementType = sequenceType.ElementType as IModelTypeJv; var itemName = string.Format(CultureInfo.InvariantCulture, "item{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture)); var itemTarget = string.Format(CultureInfo.InvariantCulture, "value{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture)); builder.AppendLine("{0}{1} = new ArrayList<{2}>();", IsRequired ? wireType.Name + " " : "", target, elementType.Name) .AppendLine("for ({0} {1} : {2}) {{", elementType.ParameterVariant.Name, itemName, source) .Indent().AppendLine(convertClientTypeToWireType(elementType, itemName, itemTarget, clientReference, level + 1)) .AppendLine("{0}.add({1});", target, itemTarget) .Outdent().Append("}"); _implImports.Add("java.util.ArrayList"); if (!IsRequired) { builder.Outdent().AppendLine("}"); } } else if (wireType is DictionaryTypeJv) { if (!IsRequired) { builder.AppendLine("{0} {1} = {2};", WireType.Name, target, wireType.GetDefaultValue(Method) ?? "null") .AppendLine("if ({0} != null) {{", source).Indent(); } var dictionaryType = wireType as DictionaryTypeJv; var valueType = dictionaryType.ValueType as IModelTypeJv; var itemName = string.Format(CultureInfo.InvariantCulture, "entry{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture)); var itemTarget = string.Format(CultureInfo.InvariantCulture, "value{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture)); builder.AppendLine("{0}{1} = new HashMap<String, {2}>();", IsRequired ? wireType.Name + " " : "", target, valueType.Name) .AppendLine("for (Map.Entry<String, {0}> {1} : {2}.entrySet()) {{", valueType.ParameterVariant.Name, itemName, source) .Indent().AppendLine(convertClientTypeToWireType(valueType, itemName + ".getValue()", itemTarget, clientReference, level + 1)) .AppendLine("{0}.put({1}.getKey(), {2});", target, itemName, itemTarget) .Outdent().Append("}"); _implImports.Add("java.util.HashMap"); if (!IsRequired) { builder.Outdent().AppendLine("}"); } } return(builder.ToString()); }
public FluentModelMemberVariable(string name, ParameterJv fromParameter) { this.VariableName = name; this.FromParameter = fromParameter; this.variableType = fromParameter.ClientType; }