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 ...
},
}
- 1 回答
- 0 关注
- 81 浏览
添加回答
举报