取消对分片的结构的返回空值,而不是空分片

问题描述 投票:0回答:1

如果创建没有任何标签的“照片”,则将其存储在dynamodb中,为>]

"tags": {
   "NULL": true
}, 

但是当我查询并解组记录时,我希望它会将其转换为空片,而不是我得到这个:

[{"photo_id":"bmpuh3jg","tags":null}]

是否有可能将其转换为空切片?例如

[{"photo_id":"bmpuh3jg","tags":[]}]

代码示例

我的结构

type Photo struct {
    Id        string   `json:"photo_id"`
    Tags      []string `json:"tags"`
}

查询

photo := &Photo{}
input := &dynamodb.QueryInput{
    TableName:                 aws.String("local.photos"),
    KeyConditionExpression:    aws.String("photo_id = :photo_id"),
    ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
        ":photo_id": {
            S: aws.String(photo_id),
        },
    },
}
db_result, err := db.Query(input)
if err != nil {
    return nil, err
} else if *db_result.Count == int64(0) {
    // No item found
    return nil, err
}

err = dynamodbattribute.UnmarshalListOfMaps(db_result.Items, photo)
if err != nil {
    return nil, err
}

photoJSON, err := json.Marshal(photo)
if err != nil {
    return nil, err
}

return photoJSON, nil

如果创建不带任何标签的“照片”,它会以“标签”的形式存储在dynamodb中:{“ NULL”:true},但是当我查询并解组记录时,我希望它会将其转换为空切片。 ..

go amazon-dynamodb unmarshalling
1个回答
2
投票

如果我正确理解了您的问题,要获得一个空白的标签({"photo_id":"bmpuh3jg","tags":[]}),请按以下步骤进行操作:


0
投票

这对我有用(注释是代码输出):

© www.soinside.com 2019 - 2024. All rights reserved.