Using (sub)struct located in other package in Datastore

huangapple go评论97阅读模式
英文:

Using (sub)struct located in other package in Datastore

问题

我无法找到与此相关的任何信息...假设你调用一个API,并将结果的JSON保存到包Foo中的结构体中,像这样:

package foo

type FooData struct {
  A string `json:"c"`
  B int    `json:"c"`
  C int64  `json:"c"`
}

然后你有自己的模型包:

package bar

import (
  "github.com/bla/bla/foo"
)

type BarData struct {
  A float64
  Foo foo.FooData
}

这样做是否可行?这种方式有什么不好的地方吗?原因是结构体有一百多行,所以我觉得复制它会很浪费。

另外,如果我只想在B字段上建立索引,那么我可以将它改为:

type FooData struct {
  A string `datastore:",noindex" json:"c"`
  B int    `json:"c"`
  C int64  `datastore:",noindex" json:"c"`
}

编辑:只是为了明确,我的意图是将BarData结构体保存在Datastore中。

英文:

I haven't been able to find any information related to this.. let's say you call an API and save the resulted json into a struct in package Foo, like this:

package foo

type FooData struct {
  A string `json:"c"`
  B int    `json:"c"`
  C int64  `json:"c"`
}

Then you have your own model package:

package bar

import (
  "github.com/bla/bla/foo"
)

type BarData struct {
  A float
  Foo foo.FooData
}

Is this possible? Is there anything negative about doing it this way? The reason is the structs are over a hundred lines, so I feel it would be wasteful to duplicate it.

Also, what if I only want to index in B? Then I can just change it to:

type FooData struct {
  A string `datastore:",noindex" `json:"c"`
  B int    `json:"c"`
  C int64  `datastore:",noindex" `json:"c"`
}

?

Edit: Just to make it clear, my intention is to save the BarData struct in Datastore

答案1

得分: 3

Go Datastore包中有一些与此相关的文档 - https://cloud.google.com/appengine/docs/standard/go/datastore/reference#hdr-Structured_Properties

"结构化属性

如果指向的结构体包含其他结构体,则嵌套或嵌入的结构体会被展开。例如,给定以下定义:

type Inner1 struct {
    W int32
    X string
}
type Inner2 struct {
    Y float64
}
type Inner3 struct {
    Z bool
}
type Outer struct {
    A int16
    I []Inner1
    J Inner2
    Inner3
}

那么Outer的属性将等同于以下结构体的属性:

type OuterEquivalent struct {
    A     int16
    IDotW []int32  `datastore:"I.W"`
    IDotX []string `datastore:"I.X"`
    JDotY float64  `datastore:"J.Y"`
    Z     bool
}

如果Outer的嵌入字段Inner3被标记为datastore:"Foo",那么等效字段将变为:FooDotZ bool datastore:"Foo.Z"

如果外部结构体被标记为"noindex",那么它的所有隐式展开字段都将被视为"noindex"。

因此,存储嵌套结构体不应该有任何问题,只需注意它们会在数据存储中被展开。文档还提到了不索引的问题,表示任何字段都会从其父结构体继承"noindex"。我不明白为什么你对内部字段进行"noindex"标记不起作用。

英文:

The Go Datastore package has some documentation relating to this - https://cloud.google.com/appengine/docs/standard/go/datastore/reference#hdr-Structured_Properties

"Structured Properties

If the struct pointed to contains other structs, then the nested or embedded structs are flattened. For example, given these definitions:

type Inner1 struct {
    W int32
    X string
}
type Inner2 struct {
    Y float64
}
type Inner3 struct {
    Z bool
}
type Outer struct {
    A int16
    I []Inner1
    J Inner2
    Inner3
}

then an Outer's properties would be equivalent to those of:

type OuterEquivalent struct {
    A     int16
    IDotW []int32  `datastore:"I.W"`
    IDotX []string `datastore:"I.X"`
    JDotY float64  `datastore:"J.Y"`
    Z     bool
}

If Outer's embedded Inner3 field was tagged as datastore:"Foo" then the equivalent field would instead be: FooDotZ bool datastore:"Foo.Z".

If an outer struct is tagged "noindex" then all of its implicit flattened fields are effectively "noindex"."

So there shouldn't be any issues with you storing nested structs, just be aware that they will be flattened in datastore. It also mentions the no indexing, saying any field inherits a "noindex" from its parent struct. I don't see why your "noindex" tagging of the inner fields wouldn't work.

huangapple
  • 本文由 发表于 2017年5月18日 12:07:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/44038401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定