-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor input functions to make them testable #255
Conversation
62d4064
to
8cd5e95
Compare
c67a24c
to
70d0827
Compare
src/cmd/input.go
Outdated
return nil | ||
} | ||
|
||
func ReadResourceHandleJSONFields[T any](input []byte) (*T, error) { |
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 looks like this is ONLY used in test files - this shouldn't be part of the codebase then it should be a utility function in the testing code.
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.
Same with its helper functions handleJSONString
and handleJSONSchema
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.
@rocktavious this is used in the properties PR that follows this: #257
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.
In fact after reading the whole code - i don't see where this function is used - so why are we testing it?
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.
@rocktavious I wanted to add an abstraction in case we would start adding more resource types that use JSON fields outside of properties, but I have now reversed this.
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 feel like the abstraction is more complex and unexpected then having custom yaml marshallars
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.
@rocktavious if we remove support for JSON, we could replace this code with only custom YAML marshalers which would be much cleaner. Supporting both YAML and JSON feels hacky.
Also, while you can read in JSON objects, reading in JSON values (number, string, boolean, list) is next to impossible without us writing another 100+ lines of code.
How do you feel about only supporting YAML?
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 think i already voiced my approval of having the yaml way only
src/cmd/input.go
Outdated
return &finalInput, nil | ||
} | ||
|
||
func ReadResourceInput[T any](input []byte) (*T, error) { |
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.
The only reason input
is here is because of the tests - we should refactor this so it doesn't take input (because the only thing i should be doing is "reading input"
The function that takes an input argument should be a helper function in the test code.
this will also remove the need to add all the (nil)
to the codebased where this function is used.
In the future it's worth remembering to not conflate test code with production code. Keeping separated concerns and making a function do 1 logical thing will help.
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.
@rocktavious being able to pass in input
can serve a purpose outside of tests as well, for example by being to do readInput()
to get input in bytes that can be used to get the raw input from the user before calling ReadResourceInput()
.
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.
This add complexity for a "what if" that we are currently not doing in our code outside of testing nor would likely do IMO.
Is there any reason that the tests cannot just use ReadResource[T](input)
?
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.
@rocktavious I have changed the ReadResourceInput
signature to use a variadic argument for the mocked inputs. Is that OK? It means that we don't need to (nil)
every time.
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.
thats definitely better - i still don't like it because now you are abusing variadic - especially because you don't handle multiple arguments so if someone passes 2 things they get unexpected behaviour of reading from standard input or from a file - but we can move on since this gets what i'm after it just leaves tech debt in our codebase sadly.
Issues
https://github.com/OpsLevel/team-platform/issues/264
Changelog
readResourceInput
into 3 functions: one to read input, one to apply input, one to do both