【发布时间】:2018-03-12 17:36:30
【问题描述】:
我有以下结构:
type Record struct {
Id string `json:"id"`
ApiKey string `json:"apiKey"`
Body []string `json:"body"`
Type string `json:"type"`
}
这是 dynamoDB 表的蓝图。我需要以某种方式删除 ApiKey,用于检查用户是否有权访问捐赠记录。解释:
我的 API 中有端点,用户可以发送id 来获取项目,但他需要访问 ID 和 ApiKey(我使用 Id (uuid) + ApiKey)来创建独特的物品。
我的表现如何:
func getExtraction(id string, apiKey string) (Record, error) {
svc := dynamodb.New(cfg)
req := svc.GetItemRequest(&dynamodb.GetItemInput{
TableName: aws.String(awsEnv.Dynamo_Table),
Key: map[string]dynamodb.AttributeValue{
"id": {
S: aws.String(id),
},
},
})
result, err := req.Send()
if err != nil {
return Record{}, err
}
record := Record{}
err = dynamodbattribute.UnmarshalMap(result.Item, &record)
if err != nil {
return Record{}, err
}
if record.ApiKey != apiKey {
return Record{}, fmt.Errorf("item %d not found", id)
}
// Delete ApiKey from record
return record, nil
}
在检查 ApiKey 是否等于提供的apiKey 后,我想从record 中删除ApiKey,但不幸的是,使用delete 是不可能的。
谢谢。
【问题讨论】:
-
您不能在运行时修改结构的类型定义。结构类型的值将始终具有结构类型定义的所有字段。
-
您只是想用一个空字符串将其清空吗?