doubushi0031
doubushi0031
2019-09-13 02:15
浏览 31
已采纳

如何从数据存储中获取名称/密钥

Couldn't find any existing Go specific answer so creating a new one.

Our datastore consist of following columns/attributes

Name/ID Email UserID UserName

Now, I want to retrieve these values into my Go struct. Here is what i have written

type UserDetails struct {
    NameID    string
    Email     string `datastore:"Email"`
    UserID    string `datastore:"UserID"`
    UserName  string `datastore:UserName`
}

Now, when i am fetching this entity (based on kind), I am manually setting the NameID i.e.,

func (c DataStoreClient) GetUserDetailsByOrg(ctx context.Context, orgName string) ([]*UserDetails, error) {
    var userDetails []*UserDetails
    q := datastore.NewQuery(userDetailsKind).
        Namespace(orgName)
    keys, err := c.client.GetAll(ctx, q, &userDetails)
    for i, key := range keys {
        userDetails[i].NameID = key.Name
    }
    return userDetails, err
}

Now, the question that i want to ask that is this the correct approach to retrieve Name/ID or can i retrieve it by specifying datastore:Name/ID in struct?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpp66953
    dpp66953 2019-09-13 02:48
    已采纳

    Implement the KeyLoader interface to set the field during entity load.

    type UserDetails struct {
        NameID   string `datastore:"-"`
        Email    string `datastore:"Email"`
        UserID   string `datastore:"UserID"`
        UserName string `datastore:UserName`
    }
    
    func (ud *UserDetails) LoadKey(k *datastore.Key) error {
        ud.NameID = k.Name
        return nil
    }
    
    func (ud *UserDetails) Load(ps []datastore.Property) error {
        return datastore.LoadStruct(ud, ps)
    }
    
    func (ud *UserDetails) Save() ([]datastore.Property, error) {
        return datastore.SaveStruct(ud)
    }
    

    Use it like this:

    func (c DataStoreClient) GetUserDetailsByOrg(ctx context.Context, orgName string) ([]*UserDetails, error) {
        var userDetails []*UserDetails
        q := datastore.NewQuery(userDetailsKind).
            Namespace(orgName)
        _, err := c.client.GetAll(ctx, q, &userDetails)
        return userDetails, err
    }
    

    The approach in the question works. The advantage of the KeyLoader implementation is that it saves a couple of lines of code wherever the application queries for or gets an entity.

    Set the NameID datastore name to "-" so that the property loader and saver ignores the field.

    点赞 评论
  • dsiv4041
    dsiv4041 2019-09-13 02:23

    Your approach is fine. However, because you have NameID without any tag, there will be a datastore entity property with that name set to nothing. It's an unnecessary property to store. So, you could make it "NameID string datastore:"-" so datastore ignores it. Alternatively, if you are not exporting this field outside the package or via JSON, you can use a lower case name like "nameID".

    Another approach you have is to automatically get the entire key by having a "key" field. You can learn more about it in Key Field

    https://godoc.org/cloud.google.com/go/datastore#hdr-Key_Field

    点赞 评论

相关推荐