Dealing with optional values in responses.

This commit is contained in:
Junyi Yi 2018-04-12 14:32:27 -07:00
Родитель 68b8cb9f40
Коммит a24e8e9b78
5 изменённых файлов: 29 добавлений и 10 удалений

Просмотреть файл

@ -62,8 +62,9 @@
var itrVarName = "tmpParamItemOf" + baseVarName;
var valVarName = "tmpParamValueOf" + baseVarName;
@:@(Indentation)@(tempName) := make(@(TypeToString(typeOverride ?? Model.Type, "sdk"))@(isArray ? ", 0" : string.Empty))
@:@(IncludeLoadFields(Model.Data.BackingField, $"for {keyVarName}, {itrVarName} := range {{0}}")) {
@:@(IncludeLoadFields(Model.Data.BackingField,
$"{tempName} := make({TypeToString(typeOverride ?? Model.Type, "sdk")}{(isArray ? ", 0" : string.Empty)})\r\n".EscapeFormatString() +
$"for {keyVarName}, {itrVarName} := range {{0}}")) {
Indentation.Indent();
Model.FieldDefs.Push(new Dictionary<string, string>());

Просмотреть файл

@ -18,7 +18,7 @@
var existDefinition = Model.FieldDefs.FirstOrDefault(def => def.ContainsKey(Model.Field.PropertyPath));
if (existDefinition != null)
{
@:@(Indentation)@(string.Format(Model.TargetFormat, existDefinition[Model.Field.PropertyPath]))
@:@(Indentation.ApplyToMultiline(string.Format(Model.TargetFormat, existDefinition[Model.Field.PropertyPath])))
return;
}
@ -42,21 +42,21 @@
var getStatement = $"d.Get(\"{Model.Field.Name}\")";
if (Model.Field.MightBeEmpty)
{
@:@(Indentation)if v, paramExists := d.GetOk("@(Model.Field.Name)"); paramExists {
@:@(Indentation)if paramValue, paramExists := d.GetOk("@(Model.Field.Name)"); paramExists {
Indentation.Indent();
getStatement = "v";
getStatement = "paramValue";
}
@:@(Indentation)@(string.Format(actualTargetFormat, $"{getStatement}.({TypeToString(Model.Field.GoType, "tf")})"))
@:@(Indentation.ApplyToMultiline(string.Format(actualTargetFormat, $"{getStatement}.({TypeToString(Model.Field.GoType, "tf")})")))
}
else
{
var getStatement = $"{definition[Model.Field.Parent.PropertyPath]}[\"{Model.Field.Name}\"]";
if (Model.Field.MightBeEmpty)
{
@:@(Indentation)if v, paramExists := @(getStatement); paramExists {
@:@(Indentation)if paramValue, paramExists := @(getStatement); paramExists {
Indentation.Indent();
getStatement = "v";
getStatement = "paramValue";
}
@:@(Indentation)@(string.Format(actualTargetFormat, $"{getStatement}.({TypeToString(Model.Field.GoType, "tf")})"))
@:@(Indentation.ApplyToMultiline(string.Format(actualTargetFormat, $"{getStatement}.({TypeToString(Model.Field.GoType, "tf")})")))
}
}

Просмотреть файл

@ -27,6 +27,14 @@
var noFieldAccess = isIntermediate && Model.Type.Chain.Count() > 0;
var noIndirection = (Model.Type.IsSimple && Model.Type.Terminal == GoSDKTerminalTypes.Enum);
var value = (noIndirection ? string.Empty : "*") + Model.Scope + (noFieldAccess ? $"(*{TypeToString(Model.Type, "sdk")})" : Model.Data.Name);
var mightBeEmpty = !Model.Data.OriginalVariable.IsRequired && !noIndirection && !isIntermediate;
var isArray = Model.Type.Chain.Any() && Model.Type.Chain.First() == GoSDKNonTerminalTypes.Array;
if (mightBeEmpty)
{
@:@(Indentation)if @(value.Substring(1)) != nil @(isArray ? $"&& len({value}) > 0 " : string.Empty){
Indentation.Indent();
}
if (!Model.Type.Chain.Any())
{
if (Model.Data.GoType.Terminal != GoSDKTerminalTypes.Complex)
@ -45,7 +53,6 @@
{
var valueType = Model.Type.StripNonTerminal();
var baseVarName = GetNonTerminalBaseVariableName();
var isArray = Model.Type.Chain.First() == GoSDKNonTerminalTypes.Array;
var tempName = "tmpRespOf" + baseVarName;
var keyVarName = (isArray ? "_" : "tmpRespKeyOf" + baseVarName);
@ -96,4 +103,10 @@
}
}
if (mightBeEmpty)
{
Indentation.Outdent();
@:@(Indentation)}
}
}

Просмотреть файл

@ -1,4 +1,5 @@
using AutoRest.Core;
using AutoRest.Core.Utilities;
using System;
using System.Linq;
using static AutoRest.Core.Utilities.DependencyInjection;
@ -16,6 +17,8 @@ namespace AutoRest.Terraform
public void Outdent() => IndentationLevel = Math.Max(IndentationLevel - 1, 0);
public override string ToString() => string.Join(string.Empty, Enumerable.Repeat(IndentationWord, IndentationLevel));
public string ApplyToMultiline(string lines) => IndentedStringBuilder.IndentMultilineString(ToString() + lines, ToString());
}
public class TfProviderTemplateBase<T>

Просмотреть файл

@ -21,6 +21,8 @@ namespace AutoRest.Terraform
public static readonly string AnyPathExtension = AnyPathExtName.ToExtensionString();
public static string EscapeFormatString(this string format) => format.Replace("{", "{{").Replace("}", "}}");
private static string ToAttributeString(this object obj) => AttributeStart + obj + AttributeEnd;
private static string ToExtensionString(this string extension) => ExtensionStart + extension + ExtensionEnd;
private static string WrapByFormatBraces(this object content) => $"{{{content}}}";