douzhicui2209 2018-09-11 21:54
浏览 23
已采纳

有没有不使用自定义结构即可检索实体的方法?

The struct datastore.Entity looks very useful, and it's how I want to process entities, but I don't see any API the makes use of it. Most of the functions (such as Get) take an interface{} that only seems to work if it is struct that is precisely structured like the incoming data.

// https://godoc.org/cloud.google.com/go/datastore#Client.Get

ctx := context.Background()
client, err := datastore.NewClient(ctx, "project-id")
if err != nil {
    // TODO: Handle error.
}

type Article struct {
    Title       string
    Description string
    Body        string `datastore:",noindex"`
    Author      *datastore.Key
    PublishedAt time.Time
}
key := datastore.NameKey("Article", "articled1", nil)
article := &Article{}
if err := client.Get(ctx, key, article); err != nil {
    // TODO: Handle error.
}

How would I obtain this entity in a generalized way? What if I don't know the structure perfectly? (More specifically, how do I obtain an instance of datastore.Entity instead?)

  • 写回答

1条回答 默认 最新

  • doqrt26664 2018-09-12 09:10
    关注

    So you want a "general" type that can hold any type of entity? The datastore package already provides you such a type: datastore.PropertyList.

    This is how you can use it:

    var entity datastore.PropertyList
    if err := client.Get(ctx, key, &entity); err != nil {
        // TODO: Handle error.
    }
    

    Relevant docs from datastore:

    Properties

    An entity's contents can be represented by a variety of types. These are typically struct pointers, but can also be any type that implements the PropertyLoadSaver interface. If using a struct pointer, you do not have to explicitly implement the PropertyLoadSaver interface; the datastore will automatically convert via reflection. If a struct pointer does implement that interface then those methods will be used in preference to the default behavior for struct pointers. Struct pointers are more strongly typed and are easier to use; PropertyLoadSavers are more flexible.

    So you may use any type that implements the datastore.PropertyLoadSaver interface. This interface type is:

    type PropertyLoadSaver interface {
        Load([]Property) error
        Save() ([]Property, error)
    }
    

    Quoting again from package doc:

    The PropertyLoadSaver Interface

    An entity's contents can also be represented by any type that implements the PropertyLoadSaver interface. This type may be a struct pointer, but it does not have to be. The datastore package will call Load when getting the entity's contents, and Save when putting the entity's contents. Possible uses include deriving non-stored fields, verifying fields, or indexing a field only if its value is positive.

    [...] The *PropertyList type implements PropertyLoadSaver, and can therefore hold an arbitrary entity's contents.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c