-
Notifications
You must be signed in to change notification settings - Fork 17
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(go): template improvements #1444
Conversation
✅ Deploy Preview for api-clients-automation ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✗ The generated branch has been deleted.If the PR has been merged, you can check the generated code on the |
additionalProperties.put("packageVersion", Utils.getClientConfigField("go", "packageVersion")); | ||
// using hostForGo to avoid overriding host in supporting files | ||
additionalProperties.put("hostForGo", (String) additionalProperties.get("host")); |
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.
does the go generator requires one more variable than the others? If not, I'd rather change the util so that all templates uses the same naming/logic than having a special case
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.
for now it only requires this. My initial thought was also changing the util with other templates but I didn't want to do that directly. But if you are okay to do this change on other templates as well, I can do it. It should be fairly simple
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.
Yep it's basically changing the variable we use, we should force consistency between generators where applicable so that we don't struggle maintaining
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.
sure, I will update this with the next commit
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.
looks good ! GG on bringing the retry strategy !
|
||
const ( | ||
Read Kind = iota | ||
Write |
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.
isn't ReadWrite
a third option ? for me the 3 are different
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.
these constants are used with the operations, not for the hosts. They are used to call the API endpoints. We don't need another const as ReadWrite
because there will be no usage, now. We are setting it as Read
depending on the x-use-read-transporter
and the rest of the time it is just Write
. So there is no other option for now.
when defining hosts we are using the functions below and there are three of them as you mentioned. IsRead
, IsWrite
, and IsReadWrite
.
clients/algoliasearch-client-go/algolia/internal/errs/no_more_host_to_try_err.go
Show resolved
Hide resolved
clients/algoliasearch-client-go/algolia/internal/transport/requester.go
Outdated
Show resolved
Hide resolved
// Add Content-Encoding header, if needed | ||
if shouldCompress(t.compression, req.Method, req.Body) { | ||
switch t.compression { | ||
case compression.GZIP: |
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.
do we plan on supporting other compression algorithm ? if not this could be an if.
also I don't think we do anything about gzip in other languages, maybe it's something we miss
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.
Let me simplify this as you suggested.
--
tbh I am not sure about this compression stuff. It was supported by the original client and I thought adding it here will not break anything. But I couldn't see anything specific in the other generated clients.
} | ||
|
||
func WithPage(page int32) Option { | ||
func QueryParamOption(name string, val any) Option { |
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.
how is the DX with this change ? I know we shouldn't focus on that for the first iterations of the client, just wondering
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.
At the current stage, this shouldn't be necessary at all because we are proving all optional and required parameters to the users. But in case someone sends a very specific header or query param I add this. Also, I added opts ...Option
in case if it is needed.
Normally it will not affect the user but if it will be used in this stage it will look like this;
auths, err := ingestionClient.GetAuthentications(
ingestionClient.NewApiGetAuthenticationsRequest().WithItemsPerPage(2),
ingestion.QueryParamOption("myQueryParam1", "myQueryParamValue1"),
ingestion.HeaderParamOption("myHeaderParam1", "myHeaderParamValue2"),
)
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 issue is that the user shouldn't have to worry if it's a query param or a header, it should be the job of the API client.
or is this just a custom fallback if the param is not in the api client ?
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.
exactly this is this just a custom fallback if the param is not in the api client
all the query, header, or body params are provided to the users with the given specs. So normally they shouldn't worry about those but this is just a fallback functionality if there are some rare cases.
templates/go/api.mustache
Outdated
{{#returnType}} | ||
returnValue {{^isArray}}{{^returnTypeIsPrimitive}}*{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}} | ||
localVarReturnValue {{^isArray}}{{^returnTypeIsPrimitive}}*{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}} |
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.
is there a globalVarReturnValue
? I don't think the distinction is necessary
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.
These are made for providing extra safety when generating the code. Because using when with common var names there is a small colliding possibility.
For example; it was happening with body
, in one of the specs we had a request with a body
parameter, and the generated code was failing.
There is a small possibility for returnValue
or postBody
but still, it is better to reduce these chances as the default templates do.
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 still think they hurt readability, if we know there is no collision with global variable we should remove it IMO
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.
okay no huge deal for me.
I believe there is no collision as of now for returnValue
and postBody
. But let me double-check with generating the code to see.
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.
alright, updated with feef2aa
templates/go/api.mustache
Outdated
} else { | ||
localVarPostBody = body | ||
} | ||
localVarPostBody = r.{{paramName}} |
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 here, it could just be postBody
oh also I noticed that the go client is using the |
@millotp Sorry, I couldn't fully understand. So this means we shouldn't utilize the default value on the specs in any of the models and if the value is not set by the user, we should marshal it as empty? |
exactly, this is what we do in other languages, the default is purely informative for the user, but we don't rely on it |
@millotp Oh alright, I haven't noticed this at all. I am not sure if the changes will be small or not. If you don't mind, I would like to tackle this with another PR. So we will not make this PR bigger. |
You can ignore this comment, it turned out pretty simple to do so I added it in this PR. When it is removed from the initialization function, the rest is automatically handled. If the user is not using specifically |
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 looks super clean, I don't have anything blocking to add actually
return io.NopCloser(bytes.NewReader(data)), string(data) | ||
} | ||
|
||
func decodeGzipContent(in string) (string, 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.
do we expect to use 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.
well, if the debug
functionality is used, this may be necessary.
I add the debug package to comply with the current client so we keep this package this functionality should be here.
But for compression stuff, I am not sure if should we include it or not. Please see conversation; #1444 (comment)
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 might see more and more custom error, it might be worth to align on some names in out contributing documentation so that other clients can benefit 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.
doesn't have to be tackled in this PR, but worth adding a ticket to tackle 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.
Yes, I will open up a ticket in general about error handling to make it more standard.
"time" | ||
) | ||
|
||
const ( |
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.
just wondering but are they the same as the current client?
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.
yeap, they are the same as the current client, ref: https://github.com/algolia/algoliasearch-client-go/blob/master/algolia/transport/requester.go#L9-L14
just for context about the default values, they are set at the engine level so we don't need them in the API clients, that's why they are only in the specs |
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.
looks good ! nothing more to add except the localVar
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.
Gg!!
🧭 What and Why
🎟 JIRA Ticket: https://algolia.atlassian.net/browse/APIC-660
With #1423, the initial setup merged but as explained in that PR we need to make more improvements to finalize the generation.
This PR is meant the close the requirement gap on the go client. There is still some improvement room for the implementation but the needed requirements should be fulfilled with this PR.
Changes included:
templates/go/api.mustache
updated to handle parameters properly.templates/go/utils.mustache
added to prevent code generation errors.NullableBool
but I didn't remove the rest since it may be needed in the future.🧪 Test
The playground example was briefly updated to ensure the new request structure is working. Also, I couldn't find any static check errors on the code after the changes.