Add an inspection that reports properties being set to their default value #169
Add an inspection that reports properties being set to their default value #169
Conversation
These providers had errors when I ran the schema extractor but I ignored them as out of scope:
And there were 4 new providers which I also ignored:
|
schemas-extractor/build-all.sh
Outdated
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 | ||
} |
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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
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.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neither do I :)
There was a problem hiding this comment.
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.
I've addressed all the PR comments. The useless schema like below is because of
|
6c7a2b3
to
0bd7ddd
Compare
No description provided.