-
Notifications
You must be signed in to change notification settings - Fork 0
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: Render entry.go
(second stage)
#34
Conversation
…d functions used in templates
entry.go
(second stage)
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.
So yeah, please no string concatenation. Either do fmt.Sprintf()
or a template. There will be lots of templates flying around in the generator, just like there are lots of templates flying around in the scm generator :)
Future you that has to do bug fixes and feature enhancement will appreciate it :)
… the issues from `golangci-lint run --enable-all`
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.
Very good approach! But I will opting for improving this, specially the func.go
} | ||
|
||
// SpecParamType return param type (it can be nested spec) (for struct based on spec from YAML files). | ||
func SpecParamType(parent string, param *properties.SpecParam) string { |
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.
Function XmlParamType and SpecParamType are very similar, you can refactor them with some helper function.
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 was thinking about it, but if I combine this 2 functions into one, more general, more if
will be required and (IMO) it will be harder to understand.
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 could split it into the smaller functions and deduplicate some code from them, it is up to you if you have cycle to take time for it, if no we can think about it a bit later.
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 splitted it into the smaller functions and deduplicated some code from them . Are you able to quickly check new approach, please ?
pkg/translate/funcs.go
Outdated
} | ||
builder.WriteString(fmt.Sprintf("entry.%s = &Spec%s%s{\n", param.Name.CamelCase, param.Name.CamelCase, specSuffix)) | ||
for _, subParam := range param.Spec.Params { | ||
builder.WriteString(nestedObjectAssignment([]string{param.Name.CamelCase}, specSuffix, subParam)) |
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.
Why not putting this to line 45?
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 cannot put this line into 45 line, because before we start entry
assignment, we need to declare variables. Here is example output from generator:
nestedProtocolTcpDestinationPort := o.Protocol.Tcp.DestinationPort
nestedProtocolTcpSourcePort := o.Protocol.Tcp.SourcePort
nestedProtocolTcpOverrideYesTimewaitTimeout := o.Protocol.Tcp.Override.Yes.TimewaitTimeout
nestedProtocolTcpOverrideYesTimeout := o.Protocol.Tcp.Override.Yes.Timeout
nestedProtocolTcpOverrideYesHalfcloseTimeout := o.Protocol.Tcp.Override.Yes.HalfcloseTimeout
nestedProtocolTcpOverrideNo := o.Protocol.Tcp.Override.No
nestedProtocolUdpDestinationPort := o.Protocol.Udp.DestinationPort
nestedProtocolUdpSourcePort := o.Protocol.Udp.SourcePort
nestedProtocolUdpOverrideYesTimeout := o.Protocol.Udp.Override.Yes.Timeout
nestedProtocolUdpOverrideNo := o.Protocol.Udp.Override.No
entry.Protocol = &SpecProtocolXml{
Tcp: &SpecProtocolTcpXml{
DestinationPort: nestedProtocolTcpDestinationPort,
SourcePort: nestedProtocolTcpSourcePort,
Override: &SpecProtocolTcpOverrideXml{
Yes: &SpecProtocolTcpOverrideYesXml{
TimewaitTimeout: nestedProtocolTcpOverrideYesTimewaitTimeout,
Timeout: nestedProtocolTcpOverrideYesTimeout,
HalfcloseTimeout: nestedProtocolTcpOverrideYesHalfcloseTimeout,
},
No: nestedProtocolTcpOverrideNo,
},
},
..............
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.
Oh ok, understand, we can try to move some part of this code to different function and just iterate over Param
and OneOf
, and second function will builder.WriteString operation.
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 completely refactored funcs.go
. Are you able to quickly check new approach, please ?
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 should rethink the implementation of prepareAssignment
, nestedObjectAssignment
to be more manageable. Nevertheless usage of the recursive call is fine :)
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.
should I rebuild in this PR ? If yes, do you have something in mind ?
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 mean the implementation is not bad, it actually do what is intend to do, but looks like a bit chaotic. Maybe let's stick to it now, and we can park a refactor for bit later when we have cycle to do 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 completely refactored funcs.go
. Are you able to quickly check new approach, please ?
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 blast radius of this PR is large.
if there are things you are specifically looking for feedback on, please be sure to bring attention to that somehow
} | ||
|
||
// SpecParamType return param type (it can be nested spec) (for struct based on spec from YAML files). | ||
func SpecParamType(parent string, param *properties.SpecParam) string { |
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 could split it into the smaller functions and deduplicate some code from them, it is up to you if you have cycle to take time for it, if no we can think about it a bit later.
pkg/translate/funcs.go
Outdated
} | ||
builder.WriteString(fmt.Sprintf("entry.%s = &Spec%s%s{\n", param.Name.CamelCase, param.Name.CamelCase, specSuffix)) | ||
for _, subParam := range param.Spec.Params { | ||
builder.WriteString(nestedObjectAssignment([]string{param.Name.CamelCase}, specSuffix, subParam)) |
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.
Oh ok, understand, we can try to move some part of this code to different function and just iterate over Param
and OneOf
, and second function will builder.WriteString operation.
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 mean the implementation is not bad, it actually do what is intend to do, but looks like a bit chaotic. Maybe let's stick to it now, and we can park a refactor for bit later when we have cycle to do 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.
It looks so much better, great work!!
Description
PR delivers next stage for generating
entry.go
:template.FuncMap
AddDefaultTypesForParams
)not_present
andfrom_version
for profiles (used in next stage for versioning)NestedSpecs
func SpecifyEntry(o Entry) (any, error)
unc (c *EntryXmlContainer) Normalize()
entry.tmpl
only if spec is defined for entry (e.g. for NTP or DNS we are NOT renderingentry.go
)entry.tmpl
Motivation and Context
#30
How Has This Been Tested?
Automated tests are part of PR
Types of changes
Checklist