Skip to content

Comments

Fix some minor issues in Go codes templates#13

Merged
JunyiYi merged 8 commits intoVSChina:azure_backendfrom
houkms:houk-fixtemplates
Aug 13, 2019
Merged

Fix some minor issues in Go codes templates#13
JunyiYi merged 8 commits intoVSChina:azure_backendfrom
houkms:houk-fixtemplates

Conversation

@houkms
Copy link
Collaborator

@houkms houkms commented Aug 6, 2019

Fix some minor issues in Go codes templates.

Copy link
Collaborator

@JunyiYi JunyiYi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @houkms for the update. The code looks good except for some tiny naming and space issues. Let's use this PR to track most of the issues discovered in the Azure Frontdoor Firewall Policy resource.

<% elsif property.is_a?(Api::Type::Array) -%>
results := make([]<%= sdk_marshal.package -%>.<%= sdk_marshal.sdktype.go_type_name -%>, 0)
for _, v := range input {
v := v.(map[string]interface{})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
v := v.(map[string]interface{})
v := item.(map[string]interface{})

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be item := item.(map[string]interface{}) ?

The generated .go code is like this

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it should be v := item.(map[string]interface{})

@@ -74,6 +74,7 @@ func expand<%= sdk_marshal.resource -%><%= descriptor.func_name -%>(input <%= go
<% elsif property.is_a?(Api::Type::Array) -%>
results := make([]<%= sdk_marshal.package -%>.<%= sdk_marshal.sdktype.go_type_name -%>, 0)
for _, v := range input {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for _, v := range input {
for _, item := range input {

And rename the original item to result.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.

@houkms
Copy link
Collaborator Author

houkms commented Aug 6, 2019

@JunyiYi Codes are modified, find your time to check it.

@JunyiYi
Copy link
Collaborator

JunyiYi commented Aug 6, 2019

Thanks @houkms for the update. Please use v := item.(map[string]interface{}) instead of item := ... in the template because we will use v later in build_schema_property_get('v', output_var, prop, sdk_marshal, 8).

@houkms
Copy link
Collaborator Author

houkms commented Aug 7, 2019

OK, fixed.

@houkms
Copy link
Collaborator Author

houkms commented Aug 7, 2019

This PR fix the issue in this magic-module-specs PR.

@houkms
Copy link
Collaborator Author

houkms commented Aug 12, 2019

@JunyiYi Other possible issues are fixed in the above commit, including

  • Go SDK rename: FlattenStringArray --> FlattenStringSlice and ExpandStringArray --> ExpandStringSlice
  • String array decision condition adjustment
  • Terraform configuration grammer: use block for array
  • Introduce help function to flatten example properties

Please find your time to review.

@houkms
Copy link
Collaborator Author

houkms commented Aug 12, 2019

This PR also fixes the pipeline error in the magic-module-specs PR102

Copy link
Collaborator

@JunyiYi JunyiYi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @houkms , after taking a look at the PR, I think most of the string concatenation or to_s stuffs could be simplified by Ruby String Interpolation.

properties.each do |pn, pv|
if pv.is_a?(Hash)
pv.each do |key, val|
flat_properties[pn+"."+key] = val
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify the string with "#{pn}.#{key}".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced! Also done for other similar cases.

flatten_example_properties_to_check(example_props, true)
end

def flatten_example_properties_to_check(properties, is_nested)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is_nested, do you mean has_nested_item?

Copy link
Collaborator Author

@houkms houkms Aug 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and the name was changed to be has_nested_item

<% elsif prop[1].is_a?(Integer) || prop[1].is_a?(TrueClass) || prop[1].is_a?(FalseClass) -%>
<%= prop[0].ljust(prop_alignment) -%> = <%= lines(prop[1].to_s) -%>
<% elsif prop[1].is_a?(Array) && (prop[1].length == 0 || prop[1][0].is_a?(String))
res_arr = prop[1].collect{|elem| "\""+elem.to_s+"\""} -%>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try the following code whether it works or not?

Suggested change
res_arr = prop[1].collect{|elem| "\""+elem.to_s+"\""} -%>
res_arr = prop[1].map{|elem| "\"#{elem}\""} -%>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works, replaced now.

@houkms
Copy link
Collaborator Author

houkms commented Aug 13, 2019

@JunyiYi Codes were refined now according to your suggestions.

Copy link
Collaborator

@JunyiYi JunyiYi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is pretty good to go except for one minor issue. We still need to keep the = for tags while omit it for other block properties.

-%>

<%= prop[0] -%> = {
<%= prop[0] -%> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure we include the equal sign (=) for tags (You can define a new helper function in terraform/helpers.rb similar to this one for ansible). Here is the example in Terraform: https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/azurerm/resource_arm_eventgrid_topic_test.go#L201.

@houkms
Copy link
Collaborator Author

houkms commented Aug 13, 2019

Added codes to deal with the special case for tags property.

-%>

<%= prop[0] -%> = {
<%= prop[0] -%> <%= "= " if is_tags?(prop)-%>{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think for this case, it is not a good choice to introduce that common helper function. Let's do it inline: <%= '= ' if prop[0] == 'tags' -%>.

Copy link
Collaborator

@JunyiYi JunyiYi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM, let's merge that in.

@JunyiYi JunyiYi merged commit cbf48d4 into VSChina:azure_backend Aug 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants