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

refactor(*): import API types directly in client generation, no longer copy #457

Merged
merged 1 commit into from Jan 27, 2021

Conversation

iawia002
Copy link

What this PR does / why we need it:

Previously, generating a client copies all the built-in types, this PR changes the processing method to directly import the corresponding types to solve the type duplication problem and speed up the client generation speed.

The effect is as follows:

diff --git a/pkg/server/client/v20201010/client.go b/pkg/server/client/v20201010/client.go
index 8779df2..adbb782 100644
--- a/pkg/server/client/v20201010/client.go
+++ b/pkg/server/client/v20201010/client.go
@@ -2,6 +2,9 @@ package v20201010

 import (
 	"context"
+	addon "github.com/caicloud/module-template/pkg/server/apis/addon"
+	app "github.com/caicloud/module-template/pkg/server/apis/app"
+	storage "github.com/caicloud/module-template/pkg/server/apis/storage"

 	rest "github.com/caicloud/nirvana/rest"
 )
@@ -10,19 +13,19 @@ import (
 type Interface interface {
 	// GetStorageObject description:
 	// Get a object by id
-	GetStorageObject(ctx context.Context, iD int) (storageObject *StorageObject, err error)
+	GetStorageObject(ctx context.Context, iD int) (storageObject *storage.Object, err error)
 	// GetWorkload description:
 	// Get a workload by id
-	GetWorkload(ctx context.Context, name string) (workload *Workload, err error)
+	GetWorkload(ctx context.Context, name string) (appWorkload *app.Workload, err error)
 	// ListAddonObjects description:
 	// Query a specified number of objects and returns an array
-	ListAddonObjects(ctx context.Context, count int) (objects []Object, err error)
+	ListAddonObjects(ctx context.Context, count int) (addonObjects []addon.Object, err error)
 	// ListStorageObjects description:
 	// Query a specified number of objects and returns an array
-	ListStorageObjects(ctx context.Context, count int) (storageObjects []StorageObject, err error)
+	ListStorageObjects(ctx context.Context, count int) (storageObjects []storage.Object, err error)
 	// ListWorkloads description:
 	// Query a specified number of workloads and returns an array
-	ListWorkloads(ctx context.Context, count int) (workloads []Workload, err error)
+	ListWorkloads(ctx context.Context, count int) (appWorkloads []app.Workload, err error)
 }

 // Client for version v20201010.
@@ -50,8 +53,8 @@ func MustNewClient(cfg *rest.Config) *Client {

 // GetStorageObject description:
 // Get a object by id
-func (c *Client) GetStorageObject(ctx context.Context, iD int) (storageObject *StorageObject, err error) {
-	storageObject = new(StorageObject)
+func (c *Client) GetStorageObject(ctx context.Context, iD int) (storageObject *storage.Object, err error) {
+	storageObject = new(storage.Object)
 	err = c.rest.Request("POST", 200, "/?Version=2020-10-10&Action=GetStorageObject").
 		Query("ID", iD).
 		TOPRPCData(storageObject).
@@ -61,28 +64,28 @@ func (c *Client) GetStorageObject(ctx context.Context, iD int) (storageObject *S

 // GetWorkload description:
 // Get a workload by id
-func (c *Client) GetWorkload(ctx context.Context, name string) (workload *Workload, err error) {
-	workload = new(Workload)
+func (c *Client) GetWorkload(ctx context.Context, name string) (appWorkload *app.Workload, err error) {
+	appWorkload = new(app.Workload)
 	err = c.rest.Request("POST", 200, "/?Version=2020-10-10&Action=GetWorkload").
 		Query("Name", name).
-		TOPRPCData(workload).
+		TOPRPCData(appWorkload).
 		Do(ctx)
 	return
 }

 // ListAddonObjects description:
 // Query a specified number of objects and returns an array
-func (c *Client) ListAddonObjects(ctx context.Context, count int) (objects []Object, err error) {
+func (c *Client) ListAddonObjects(ctx context.Context, count int) (addonObjects []addon.Object, err error) {
 	err = c.rest.Request("POST", 200, "/?Version=2020-10-10&Action=ListAddonObjects").
 		Query("Count", count).
-		TOPRPCData(&objects).
+		TOPRPCData(&addonObjects).
 		Do(ctx)
 	return
 }

 // ListStorageObjects description:
 // Query a specified number of objects and returns an array
-func (c *Client) ListStorageObjects(ctx context.Context, count int) (storageObjects []StorageObject, err error) {
+func (c *Client) ListStorageObjects(ctx context.Context, count int) (storageObjects []storage.Object, err error) {
 	err = c.rest.Request("POST", 200, "/?Version=2020-10-10&Action=ListStorageObjects").
 		Query("Count", count).
 		TOPRPCData(&storageObjects).
@@ -92,10 +95,10 @@ func (c *Client) ListStorageObjects(ctx context.Context, count int) (storageObje

 // ListWorkloads description:
 // Query a specified number of workloads and returns an array
-func (c *Client) ListWorkloads(ctx context.Context, count int) (workloads []Workload, err error) {
+func (c *Client) ListWorkloads(ctx context.Context, count int) (appWorkloads []app.Workload, err error) {
 	err = c.rest.Request("POST", 200, "/?Version=2020-10-10&Action=ListWorkloads").
 		Query("Count", count).
-		TOPRPCData(&workloads).
+		TOPRPCData(&appWorkloads).
 		Do(ctx)
 	return
 }
diff --git a/pkg/server/client/v20201010/types.go b/pkg/server/client/v20201010/types.go
deleted file mode 100644
index a75ac58..0000000
--- a/pkg/server/client/v20201010/types.go
+++ /dev/null
@@ -1,18 +0,0 @@
-package v20201010
-
-// Object describes a object entry.
-type Object struct {
-	ID int `json:"Id"`
-}
-
-// StorageObject describes a object entry.
-//
-// +nirvana:api=origin:"Object"
-type StorageObject struct {
-	ID int `json:"Id"`
-}
-
-// Workload describes a workload entry.
-type Workload struct {
-	Name string `json:"Name"`
-}

Which issue(s) this PR is related to (optional, link to 3rd issue(s)):

Fixes #

Reference to #

Special notes for your reviewer:

/cc @xpofei

Release note:

NONE

@caicloud-bot caicloud-bot added release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 25, 2021
@xpofei
Copy link
Contributor

xpofei commented Jan 25, 2021

/lgtm

@caicloud-bot caicloud-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 25, 2021
@iawia002
Copy link
Author

/approve

@caicloud-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: iawia002, xpofei

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@caicloud-bot caicloud-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 27, 2021
@caicloud-bot caicloud-bot merged commit 2f781c0 into caicloud:master Jan 27, 2021
@iawia002 iawia002 deleted the re-client branch January 27, 2021 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants