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

Find and Invoke Slurm APIs using reflect to avoid version specific code #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yuva29
Copy link
Collaborator

@yuva29 yuva29 commented Oct 10, 2024

No description provided.


// Step 4: Execute the PostNode method with the updated request
postNodeResp := c.helper["PostNodeExecute"].Func.Call([]reflect.Value{
reflect.ValueOf(c.apiClient.SlurmAPI),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's just one corner case, if the input parameters of the PostNodeExecute function (or any other function that we call) change between Slurm versions that this code will not work. Such an API change would be a breaking change though and hopefully will happen in a corner case. If it happens, user would have to manually change this code to pass the new parameters.

Copy link
Collaborator Author

@yuva29 yuva29 Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, If the API, params or the names change, then this code will have to updated.

}

if _, found := c.helper[methodPing]; !found {
c.helper[methodPing] = method
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to store the function pointer to the method instead of storing the reflect method? That way, when calling the method you can directly call the function pointer instead of calling the function using reflect. Just a minor optimization, if possible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sajmera-pensando

    // Get the function as reflect.Value
    funcValue := method.Func

    // Convert reflect.Value to an interface and type assert it as a function
    // Here, specify the function signature you expect
    funcInterface := funcValue.Interface().(func(*MyStruct, string))

    // Call the function pointer with the specific arguments
    funcInterface(myStruct, "Hello, world!")

If we need to convert the reflect.Method to a function pointer then we need to know the exact API signature and In this case, the parameters are also version specific.
func (a *SlurmAPIService) SlurmV0040PostNodeExecute(r ApiSlurmV0040PostNodeRequest) (*V0040OpenapiResp, *http.Response, error)
func (r ApiSlurmV0040PostNodeRequest) V0040UpdateNodeMsg(v0040UpdateNodeMsg V0040UpdateNodeMsg) ApiSlurmV0040PostNodeRequest

Copy link
Collaborator

@sajmera-pensando sajmera-pensando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

posted few comments, please check

@powderluv
Copy link
Contributor

is this ready to land ?

@yuva29
Copy link
Collaborator Author

yuva29 commented Oct 14, 2024

is this ready to land ?

Hi @powderluv We’ll be holding off on this PR for a bit until the testing team complete their evaluations. Thank you.

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