如何在结构中传递结构数组?

How to pass an array of structs inside a struct?

我在结构内部传递结构数组,在单元测试中,我使用了 ServiceAccounts 的 dto 和我的测试代码 TestStoreServiceAccounts.How 在结构内部传递结构数组?结构嵌套是我不明白什么。

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)

}

    // ServiceAccountRequestDTO - DTO of service account model
type ServiceAccountRequestDTO struct {
    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"`
}```

TL;DR:在 DTO 结构之外定义您的 ServiceAccount 结构,以便您可以重用它。

由于您在 ServiceAccountRequestDTO 结构中定义了 ServiceAccounts 的结构,因此您还需要在创建对象时再次定义它,如下所示:

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 ...
    },
}