fix: omit JsonValue type if not used#235
Conversation
Signed-off-by: joshhunt <git@joshhunt.dev>
2e78c3a to
acc5087
Compare
There was a problem hiding this comment.
Code Review
This pull request implements conditional imports for the JsonValue type in Angular and React generators, ensuring it is only included when 'object' flags are defined. It introduces a HasFlagType method to the Flagset struct and adds corresponding test cases and golden files. Feedback was provided to improve the HasFlagType method by using enum comparisons and adding a nil check for better robustness.
| func (fs *Flagset) HasFlagType(typeName string) bool { | ||
| for _, f := range fs.Flags { | ||
| if f.Type.String() == typeName { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } |
There was a problem hiding this comment.
The HasFlagType method currently relies on a direct string comparison with the output of f.Type.String(). This is somewhat fragile as it requires the caller to know the exact string representation used internally (e.g., "object" vs "Object").
It would be more robust and efficient to use the existing ParseFlagType function to convert the input string to a FlagType enum once, and then compare the enums in the loop. This also allows the method to support aliases (like "JSON" for objects) and case-insensitivity, consistent with how flags are parsed from the manifest. Additionally, adding a check for a nil receiver prevents potential panics.
func (fs *Flagset) HasFlagType(typeName string) bool {
if fs == nil {
return false
}
t, err := ParseFlagType(typeName)
if err != nil {
return false
}
for _, f := range fs.Flags {
if f.Type == t {
return true
}
}
return false
}
This PR
If you don't use flags with the object type, the React generator will make code that contains errors/warnings that the
JsonValueimport is unused. This PR fixes that by only including theJsonValueimport if it is needed by object flags.JsonValuetype if not usedtypeimport type forJsonValue