Skip to content
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

Merged
merged 23 commits into from
Mar 15, 2024
Merged

feat: Render entry.go (second stage) #34

merged 23 commits into from
Mar 15, 2024

Conversation

sebastianczech
Copy link
Contributor

@sebastianczech sebastianczech commented Mar 11, 2024

Description

PR delivers next stage for generating entry.go:

  • new functions for template.FuncMap
  • fix issues for settings default param type (function AddDefaultTypesForParams)
  • add not_present and from_version for profiles (used in next stage for versioning)
  • generating structs definitions using new function NestedSpecs
  • support for nested objects:
    • specifier - func SpecifyEntry(o Entry) (any, error)
    • normalizer - func (c *EntryXmlContainer) Normalize()
  • test for new functions
  • functions comments
  • feature to render entry.tmpl only if spec is defined for entry (e.g. for NTP or DNS we are NOT rendering entry.go)
  • extend entry.tmpl

Motivation and Context

#30

How Has This Been Tested?

Automated tests are part of PR

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist

  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes if appropriate.
  • All new and existing tests passed.

@sebastianczech sebastianczech requested review from pimielowski and shinmog and removed request for pimielowski March 11, 2024 22:07
@sebastianczech sebastianczech changed the title feat: Render entry.go (second stage) feat: Render entry.go (second stage) Mar 12, 2024
Copy link
Collaborator

@shinmog shinmog left a 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 :)

pkg/translate/funcs.go Show resolved Hide resolved
pkg/translate/structs.go Outdated Show resolved Hide resolved
Copy link
Contributor

@pimielowski pimielowski left a 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

pkg/properties/normalized_test.go Outdated Show resolved Hide resolved
pkg/translate/funcs.go Show resolved Hide resolved
pkg/properties/normalized.go Show resolved Hide resolved
pkg/properties/normalized.go Show resolved Hide resolved
pkg/properties/normalized.go Outdated Show resolved Hide resolved
}

// 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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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 Show resolved Hide resolved
pkg/translate/funcs.go Show resolved Hide resolved
}
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))
Copy link
Contributor

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?

Copy link
Contributor Author

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,
			},
		},
		..............

Copy link
Contributor

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.

Copy link
Contributor Author

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 ?

Copy link
Contributor

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 :)

Copy link
Contributor Author

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 ?

Copy link
Contributor

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.

Copy link
Contributor Author

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 ?

Copy link
Collaborator

@shinmog shinmog left a 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

pkg/properties/normalized.go Outdated Show resolved Hide resolved
pkg/translate/funcs.go Show resolved Hide resolved
}

// 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 {
Copy link
Contributor

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.

}
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))
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

@pimielowski pimielowski left a 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!!

@sebastianczech sebastianczech merged commit 25ec702 into main Mar 15, 2024
1 check passed
@sebastianczech sebastianczech deleted the render-entry branch March 15, 2024 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants