Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Add an inspection that reports properties being set to their default value #169

Conversation

matthew16550
Copy link
Contributor

No description provided.

@matthew16550
Copy link
Contributor Author

These providers had errors when I ran the schema extractor but I ignored them as out of scope:

  • terraform-provider-azure-classic
  • terraform-provider-google-beta
  • terraform-provider-oci
  • terraform-provider-scaffolding

And there were 4 new providers which I also ignored:

  • bigip
  • netlify
  • rightscale
  • tencentcloud

sed -i "s/__NAME__/${p:19}/g" generate-schema/generate-schema.go
sed -i "s/__REVISION__/$revision/g" generate-schema/generate-schema.go
sed -i "s~__OUT__~$out~g" generate-schema/generate-schema.go
sed -i '' "s/__FULL_NAME__/$p/g" generate-schema/generate-schema.go
Copy link
Contributor Author

Choose a reason for hiding this comment

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

sed on my Mac needs this

@@ -127,8 +126,11 @@ func export(v *schema.Schema) SchemaDefinition {
}

// TODO: Find better solution
if defValue, err := v.DefaultValue(); err == nil && defValue != nil && !reflect.DeepEqual(defValue, v.Default) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't see what DeepEqual was achieving here, perhaps something important that I misunderstood?

Copy link
Owner

Choose a reason for hiding this comment

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

Can't remember why I've added it here. Does removing changes anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using !reflect.DeepEqual(... there are 265 defaults generated. Removing it makes 3134 defaults generated. The diff is too big to compare everything but I think it is correctly finding all default values when removed.

}
}

private fun compare(value: HCLValue, defaultValue: Any): Boolean {
Copy link
Owner

Choose a reason for hiding this comment

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

It's better to name it isEqual

val defaultObj = value.obj("Default")
val defaultValue = if (defaultObj == null || defaultObj.isEmpty()) {
null
}
Copy link
Owner

Choose a reason for hiding this comment

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

Please reformat code

@@ -347,7 +348,24 @@ class TypeModelLoader(val external: Map<String, TypeModelProvider.Additional>) {
val conflicts: List<String> = value.array<String>("ConflictsWith")?.map { it } ?: emptyList()

val deprecated = value.string("Deprecated")
val has_default: Boolean = value.obj("Default")?.isNotEmpty() ?: false
val defaultObj = value.obj("Default")
val defaultValue = if (defaultObj == null || defaultObj.isEmpty()) {
Copy link
Owner

Choose a reason for hiding this comment

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

Seems if could be simplified to just defaultObj?.let { ... } since there null -> null block in when which will sort out isEmpty case

"Float", "TypeFloat" -> defaultValueString ?: "0"
"String", "TypeString" -> defaultValueString ?: ""
else -> {
LOG.warn("Unhandled default type $typeString for $fqn")
Copy link
Owner

Choose a reason for hiding this comment

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

logErrorAndFailInInternalMode could be used where to detect unsupported types in tests

@VladRassokhin
Copy link
Owner

Thank you for contribution.

First, could you please sign JetBrains CLA

About PR itself I see there're some useless parts of shema being generated.

        "Default": {
          "Type": "string"
        }

Apart from that I'd like to have separate commit for code and shema update (or even no schema update since it's hard to navigate through PR diff and I'd anyway regenerate them just before releasing next version)

- Rename compare() -> isEquals()
- Use logErrorAndFailInInternalMode() to report error

Also changed "application" to private class property in TypeModelLoader to avoid passing it around lots of methods.
}

func (e *SchemaElement) MarshalJSON() ([]byte, error) {
if e.Value == nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe there is a better way? I don't know much about Go.

Copy link
Owner

Choose a reason for hiding this comment

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

Neither do I :)

Choose a reason for hiding this comment

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

Origin

Value string `json:",omitempty"`

Should do the trick. You don't need to specify separate Marshaller for this.

And string in go is actually []byte which is actually already pointer type. And in most cases it is better to use just string instead of *string. And compare string with empty string ("") instead of null. What is literally the same.

I don't understand why original omitempty doesn't work.

@matthew16550
Copy link
Contributor Author

I've addressed all the PR comments.

The useless schema like below is because of json:",omitempty", I've put in a kludge to work around it.

"Default": {
  "Type": "string"
}

@matthew16550 matthew16550 closed this by deleting the head repository May 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants