caicloud / nirvana

Golang Restful API Framework for Productivity
https://caicloud.github.io/nirvana/
Apache License 2.0
520 stars 105 forks source link

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

Closed iawia002 closed 3 years ago

iawia002 commented 3 years ago

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
xpofei commented 3 years ago

/lgtm

iawia002 commented 3 years ago

/approve

caicloud-bot commented 3 years ago

[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: - ~~[OWNERS](https://github.com/caicloud/nirvana/blob/master/OWNERS)~~ [iawia002] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment