I have the code below:
type AzStorageAccount struct {
Type string `json:"type"`
Location string `json:"location"`
Tags struct {
} `json:"tags"`
Properties struct {
PrivateLinkServiceConnections []struct {
Name string `json:"name"`
Properties struct {
PrivateLinkServiceID string `json:"privateLinkServiceId"`
GroupIds string `json:"groupIds"`
PrivateLinkServiceConnectionState struct {
Status string `json:"status"`
Description string `json:"description"`
ActionsRequired string `json:"actionsRequired"`
} `json:"privateLinkServiceConnectionState"`
} `json:"properties"`
} `json:"privateLinkServiceConnections"`
ManualPrivateLinkServiceConnections []interface{} `json:"manualPrivateLinkServiceConnections"`
Subnet struct {
ID string `json:"id"`
} `json:"subnet"`
CustomDNSConfigs []interface{} `json:"customDnsConfigs"`
} `json:"properties"`
}
But I'm having issues to assung the values to the variables inside PrivateLinkServiceConnections []struct {}
At first I was using, but since I need to use []struct it does not work anymore.
storageAccount.Location = "eastus2"
storageAccount.Type = "Microsoft.Network/privateEndpoints"
storageAccount.Properties.PrivateLinkServiceConnections.Properties.PrivateLinkServiceId = "/subscriptions"
storageAccount.Properties.PrivateLinkServiceConnections.Name = "priv-endpoint"
storageAccount.Properties.PrivateLinkServiceConnections.Properties.GroupIds = "postgresqlServer"
storageAccount.Properties.PrivateLinkServiceConnections.Properties.PrivateLinkServiceConnectionState.Status = "Approved"
storageAccount.Properties.PrivateLinkServiceConnections.Properties.PrivateLinkServiceConnectionState.Description = "Auto-approved"
storageAccount.Properties.PrivateLinkServiceConnections.Properties.PrivateLinkServiceConnectionState.ActionsRequired = "None"
storageAccount.Properties.Subnet.Id = "/subscriptions/..."
marshaledStorageAccount, _ := json.Marshal(storageAccount)
utils.SendPut(endpoint, marshaledStorageAccount)
How can assign values to the code below?
PrivateLinkServiceConnections []struct {
Name string `json:"name"`
Properties struct {
PrivateLinkServiceID string `json:"privateLinkServiceId"`
GroupIds string `json:"groupIds"`
PrivateLinkServiceConnectionState struct {
Status string `json:"status"`
Description string `json:"description"`
ActionsRequired string `json:"actionsRequired"`
} `json:"privateLinkServiceConnectionState"`
} `json:"properties"`
} `json:"privateLinkServiceConnections"`
Thanks!
CodePudding user response:
The easiest and sane way to do this is by defining a new type:
type PrivateLinkServiceConnections struct {
Name string `json:"name"`
...
}
Properties struct {
Connections []PrivateLinkServiceConnections `json:"privateLinkServiceConnections"`
...
Otherwise, you have to explicitly specify the structure of each struct every time you initialize an instance, like:
x:=struct {
Name string `json:"name"`
Properties struct {
PrivateLinkServiceID string `json:"privateLinkServiceId"`
GroupIds string `json:"groupIds"`
PrivateLinkServiceConnectionState struct {
Status string `json:"status"`
Description string `json:"description"`
ActionsRequired string `json:"actionsRequired"`
} `json:"privateLinkServiceConnectionState"`
} `json:"properties"`
}{
Name:"name",
Properties:struct {
PrivateLinkServiceID string `json:"privateLinkServiceId"`
GroupIds string `json:"groupIds"`
PrivateLinkServiceConnectionState struct {
Status string `json:"status"`
Description string `json:"description"`
ActionsRequired string `json:"actionsRequired"`
} `json:"privateLinkServiceConnectionState"`
} {
PrivateLinkServiceID: id,
},
}
storageAccount.PrivateLinkServiceconnections=append(storageAccount.PrivateLinkServiceConnections, x)
...
CodePudding user response:
PrivateLinkServiceConnections is defined as an array. You cannot access it as you do with objects. To add items to it, you need to use append function.
Also, you have defined it as an inline anonymous struct, hence your code has become messy. Define a specific type for PrivateLinkServiceConnections and then each time on append, simply assign it without the need to redeclare it. It is a bad practice.
