亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在結構內傳遞結構數組?

如何在結構內傳遞結構數組?

Go
開滿天機 2022-08-30 15:12:04
我正在結構內傳遞結構數組,在單元測試中,我使用了ServiceAccounts的dto和我的測試代碼TestStoreServiceAccounts。如何在結構內傳遞結構數組?結構的嵌套是我無法理解的。func TestStoreServiceAccounts(t *testing.T) {StoreServiceAccounts = func(serviceAccounts []models.ServiceAccount) ([]string, error) {    ServiceAccounts := []string{"service-account-details-inserted"}    return ServiceAccounts, nil}Data := dto.ServiceAccountRequestDTO{    ServiceAccounts : []{ //error on this line        WorkspaceID:        1,        ServiceAccountName: "sa-10",        ServiceAccountKey: dto.ServiceAccountKey{            Type:                    "service_account",            ProjectID:               "abc",            PrivateKeyID:            "123",            PrivateKey:              "234",            ClientEmail:             "read-clusters",            ClientID:                "cdf",            AuthURI:                 "https://accounts.google.com/o/oaut",            TokenURI:                "https://oauth2.googleapis.com/token",            AuthProviderX509CertURL: "https://www.googleapis.com",            ClientX509CertURL:       "xwy",        },        CreatedAt: "2021-03-08 17:05:21.0",        UpdatedAt: "2021-03-08 17:05:21.0",        CreatedBy: "user-01",        UpdatedBy: "user-01",    },}responseData, err := clusterService.StoreServiceAccountsService(context.Background(), Data)serviceAccountsdata := []string{"service-account-details-inserted"}assert.Nil(t, err)assert.NotNil(t, responseData)assert.EqualValues(t, serviceAccountsdata, responseData)}
查看完整描述

1 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

在 DTO 結構外部定義您的 ServiceAccount 結構,以便您可以重用它。


由于您正在定義結構內部的結構,因此在創建對象時還需要再次定義它,如下所示:ServiceAccountsServiceAccountRequestDTO


Data := dto.ServiceAccountRequestDTO{

    ServiceAccounts : []struct{

        WorkspaceID        int64             `json:"workspace_id"`

        ServiceAccountName string            `json:"serviceAccountName"`

        ServiceAccountKey  ServiceAccountKey `json:"serviceAccountKey"`

        CreatedAt          string            `json:"created_at"`

        UpdatedAt          string            `json:"updated_at"`

        CreatedBy          string            `json:"created_by"`

        UpdatedBy          string            `json:"updated_by"`

    } `json:"serviceAccounts"`{ //start of slice (it's technically not an array) of objects

        { // first object

          WorkspaceID:        1,

          ServiceAccountName: "sa-10",

          ServiceAccountKey: dto.ServiceAccountKey{

              Type:                    "service_account",

              ProjectID:               "abc",

              PrivateKeyID:            "123",

              PrivateKey:              "234",

              ClientEmail:             "read-clusters",

              ClientID:                "cdf",

              AuthURI:                 "https://accounts.google.com/o/oaut",

              TokenURI:                "https://oauth2.googleapis.com/token",

              AuthProviderX509CertURL: "https://www.googleapis.com",

              ClientX509CertURL:       "xwy",

          },

          CreatedAt: "2021-03-08 17:05:21.0",

          UpdatedAt: "2021-03-08 17:05:21.0",

          CreatedBy: "user-01",

          UpdatedBy: "user-01",

        },

        // .. more ServiceAccount objects ...

    },

}

現在,這當然是令人難以置信的痛苦的編寫和閱讀,并復制了很多代碼。因此,您應該在外部定義結構。



type ServiceAccount struct{

        WorkspaceID        int64             `json:"workspace_id"`

        ServiceAccountName string            `json:"serviceAccountName"`

        ServiceAccountKey  ServiceAccountKey `json:"serviceAccountKey"`

        CreatedAt          string            `json:"created_at"`

        UpdatedAt          string            `json:"updated_at"`

        CreatedBy          string            `json:"created_by"`

        UpdatedBy          string            `json:"updated_by"`

    } `json:"serviceAccounts"`

}


type ServiceAccountRequestDTO struct {

    ServiceAccounts []ServiceAccount

}


然后你可以像這樣創建你的DTO


Data := dto.ServiceAccountRequestDTO{

    ServiceAccounts : []ServiceAccount{ //start of slice of objects

        ServiceAccount { // first object

          WorkspaceID:        1,

          ServiceAccountName: "sa-10",

          ServiceAccountKey: dto.ServiceAccountKey{

              Type:                    "service_account",

              ProjectID:               "abc",

              PrivateKeyID:            "123",

              PrivateKey:              "234",

              ClientEmail:             "read-clusters",

              ClientID:                "cdf",

              AuthURI:                 "https://accounts.google.com/o/oaut",

              TokenURI:                "https://oauth2.googleapis.com/token",

              AuthProviderX509CertURL: "https://www.googleapis.com",

              ClientX509CertURL:       "xwy",

          },

          CreatedAt: "2021-03-08 17:05:21.0",

          UpdatedAt: "2021-03-08 17:05:21.0",

          CreatedBy: "user-01",

          UpdatedBy: "user-01",

        },

        // .. more ServiceAccount objects ...

    },

}


查看完整回答
反對 回復 2022-08-30
  • 1 回答
  • 0 關注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號