-
Notifications
You must be signed in to change notification settings - Fork 129
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
feat: implement new convert command #330
Conversation
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 review is from a standpoint of concern that the quality of deck's codebase may be at risk of many small changes introducing small bits of tech debt and, as a reviewer, I'd like to deal with that by:
- preventing (or at least limiting) code's side effects (for example mutating arguments),
- enforcing that new code follows SOLID (most notably the "S") where practical
Several (non-binding non-blocking) style preferences attached as well.
Test coverage not reviewed - that will happen in another round.
f44bf6a
to
faeeb7e
Compare
Codecov Report
@@ Coverage Diff @@
## main #330 +/- ##
==========================================
+ Coverage 53.57% 53.99% +0.42%
==========================================
Files 59 61 +2
Lines 4885 4967 +82
==========================================
+ Hits 2617 2682 +65
- Misses 1970 1986 +16
- Partials 298 299 +1
Continue to review full report at Codecov.
|
faeeb7e
to
07c3c6f
Compare
@mflendrich I rebased this on top of #332 and ensured that input arguments are not being modified. PTAL. |
2c2a9b3
to
8f62389
Compare
@mflendrich Friendly ping for another review. |
convert/convert.go
Outdated
func validConversion(from, to Format) (bool, error) { | ||
if fromMap, ok := conversions[from]; ok { | ||
if _, ok := fromMap[to]; ok { | ||
return ok, nil | ||
} | ||
} | ||
|
||
return false, fmt.Errorf("cannot convert from '%s' to '%s' format", from, to) | ||
} |
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.
Comment part 1:
This duplicates the logic of the switch
in func Convert()
. Why do we need both? Why not just let Convert()
fail if no suitable conversion is possible?
Comment part 2 (provided that we need to retain this function for some reason):
Each of the two return values of this function gives exact same information - the error
additionally giving the error message. This makes the return value space unnecessarily complex: ((true, nil), (true, err), (false, nil), (false, err)).
If you rephrase this function as func validateConversion(from, to Format) error
, you'll have only one return value. Easier to test and understand, more difficult to use in a buggy way.
Why: imagine that this function somehow ends up returning false, nil
(because this is a natural way to say "the result evaluated to false with no runtime errors". In such case, the caller (func Convert
) will erroneously return nil
(saying "no error").
convert/convert.go
Outdated
switch { | ||
case from == FormatKongGateway && to == FormatKonnect: | ||
return convertKongGatewayToKonnect(inputFilename, outputFilename) | ||
default: | ||
return fmt.Errorf("cannot convert from '%s' to '%s' format", from, to) | ||
} |
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.
We're passing inputFilename
and outputFilename
down the function calls several times without a clear reason, causing convertKongGatewayToKonnect
to be concerned not only with conversion logic, but also with file handling.
Also, with more conversion format pairs added, file handling logic will need to be copied across all these format pair implementations.
How about doing file operations in Convert
and having conversions as simple functional transformations (without the need to bloat actual conversion logic with the file handling concern)
func Convert(inputFilename, outputFilename string, from, to Format) error {
// probably unnecessary - see my comment above
if valid, err := validConversion(from, to); !valid {
return err
}
inputContent, err := file.GetContentFromFiles([]string{inputFilename})
if err != nil {
return err
}
var outputContent *file.Content
switch {
case from == FormatKongGateway && to == FormatKonnect:
outputContent = convertKongGatewayToKonnect(inputContent)
default:
return fmt.Errorf("cannot convert from '%s' to '%s' format", from, to)
}
return file.WriteContentToFile(outputContent, outputFilename, file.YAML)
}
convert/convert.go
Outdated
Implementation: &file.Implementation{ | ||
Type: utils.ImplementationTypeKongGateway, | ||
Kong: &file.Kong{ | ||
Service: serviceCopy, |
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.
func wipeServiceName(s *file.Service) *file.Service {
result := s.DeepCopy()
result.Name = nil
result.ID = kong.String(utils.UUID())
return result
}
and
Service: serviceCopy, | |
Service: wipeServiceName(service), |
7c64565
to
c2ece41
Compare
c2ece41
to
02d28b7
Compare
@mflendrich Resolved the comments. PTAL. |
ping for another review @mflendrich |
No description provided.