private string convertToClientType(ITypeModel type, string source, string target, int level = 0) { if (type == null) { return(target + " = " + source + ";"); } IndentedStringBuilder builder = new IndentedStringBuilder(); SequenceTypeModel sequenceType = type as SequenceTypeModel; DictionaryTypeModel dictionaryType = type as DictionaryTypeModel; if (sequenceType != null) { var elementType = sequenceType.ElementTypeModel; 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.ValueTypeModel; 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 + ".getDateTime();"); } else if (type.IsPrimaryType(KnownPrimaryType.UnixTime)) { return(target + " = new DateTime(" + source + " * 1000L, DateTimeZone.UTC);"); } else if (type.IsPrimaryType(KnownPrimaryType.Base64Url)) { return(target + " = " + source + ".getDecodedBytes();"); } else { return(target + " = " + source + ";"); } }
private string convertClientTypeToWireType(ITypeModel 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.DefaultValue(_method)) .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.DefaultValue(_method)) .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.DefaultValue(_method)) .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.DefaultValue(_method)) .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 SequenceTypeModel) { if (!IsRequired) { builder.AppendLine("{0} {1} = {2};", WireType.Name, target, wireType.DefaultValue(_method)) .AppendLine("if ({0} != null) {{", source).Indent(); } var sequenceType = wireType as SequenceTypeModel; var elementType = sequenceType.ElementTypeModel; 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 DictionaryTypeModel) { if (!IsRequired) { builder.AppendLine("{0} {1} = {2};", WireType.Name, target, wireType.DefaultValue(_method)) .AppendLine("if ({0} != null) {{", source).Indent(); } var dictionaryType = wireType as DictionaryTypeModel; var valueType = dictionaryType.ValueTypeModel; 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()); }
private string convertToClientType(ITypeModel type, string source, string target, int level = 0) { if (type == null) { return target + " = " + source + ";"; } IndentedStringBuilder builder = new IndentedStringBuilder(); SequenceTypeModel sequenceType = type as SequenceTypeModel; DictionaryTypeModel dictionaryType = type as DictionaryTypeModel; if (sequenceType != null) { var elementType = sequenceType.ElementTypeModel; 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.ValueTypeModel; 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 + ".getDateTime();"; } else { return target + " = " + source + ";"; } }
private string convertClientTypeToWireType(ITypeModel 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.DefaultValue(_method)) .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.DefaultValue(_method)) .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.DefaultValue(_method)) .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.DefaultValue(_method)) .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 SequenceTypeModel) { if (!IsRequired) { builder.AppendLine("{0} {1} = {2};", WireType.Name, target, wireType.DefaultValue(_method)) .AppendLine("if ({0} != null) {{", source).Indent(); } var sequenceType = wireType as SequenceTypeModel; var elementType = sequenceType.ElementTypeModel; 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 DictionaryTypeModel) { if (!IsRequired) { builder.AppendLine("{0} {1} = {2};", WireType.Name, target, wireType.DefaultValue(_method)) .AppendLine("if ({0} != null) {{", source).Indent(); } var dictionaryType = wireType as DictionaryTypeModel; var valueType = dictionaryType.ValueTypeModel; 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(); }