本文整理汇总了Golang中github.com/theduke/go-dukedb.Backend.ModelInfo方法的典型用法代码示例。如果您正苦于以下问题:Golang Backend.ModelInfo方法的具体用法?Golang Backend.ModelInfo怎么用?Golang Backend.ModelInfo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/theduke/go-dukedb.Backend
的用法示例。
在下文中一共展示了Backend.ModelInfo方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: SetBackend
func (res *Resource) SetBackend(b db.Backend) {
res.backend = b
if !b.HasCollection(res.Collection()) {
b.RegisterModel(res.Model())
}
res.modelInfo = b.ModelInfo(res.Collection())
}
示例2: TestBackend
//.........这里部分代码省略.........
backend.Build()
})
Describe("Has one", func() {
It("Should ignore unpersisted has-one", func() {
t := &Task{
Name: "P1",
Project: Project{Name: "x"},
}
Expect(backend.Create(t)).ToNot(HaveOccurred())
Expect(backend.Q("projects").Count()).To(Equal(0))
})
It("Should set key for has-one relationship", func() {
p := &Project{Name: "P1"}
Expect(backend.Create(p)).ToNot(HaveOccurred())
t := &Task{
Name: "T1",
Project: *p,
}
Expect(backend.Create(t)).ToNot(HaveOccurred())
Expect(t.ProjectId).To(Equal(p.Id))
dbTask, err := backend.FindOne("tasks", t.Id)
Expect(err).ToNot(HaveOccurred())
Expect(dbTask.(*Task).ProjectId).To(Equal(p.Id))
})
It("Should auto-persist has-one", func() {
// Enable auto-create.
backend.ModelInfo("tasks").Relation("Project").SetAutoCreate(true)
t := &Task{
Name: "T1",
Project: Project{
Name: "test",
},
}
Expect(backend.Create(t)).ToNot(HaveOccurred())
Expect(t.Project.Id).ToNot(BeZero())
m, err := backend.FindOne("projects", t.Project.Id)
Expect(err).ToNot(HaveOccurred())
Expect(m).ToNot(BeNil())
tm, err := backend.FindOne("tasks", t.Id)
Expect(err).ToNot(HaveOccurred())
Expect(tm).ToNot(BeNil())
Expect(tm.(*Task).ProjectId).To(Equal(t.Project.Id))
})
It("Should auto-update has-one", func() {
// Enable auto-create.
rel := backend.ModelInfo("tasks").Relation("Project")
rel.SetAutoCreate(true)
rel.SetAutoUpdate(true)
t := &Task{
Name: "T1",
Project: Project{
Name: "test",
},
示例3: SerializeModel
func SerializeModel(backend db.Backend, m kit.Model) (*ApiModel, []*ApiModel, apperror.Error) {
modelData, err := backend.ModelToMap(m, true, false)
if err != nil {
return nil, nil, apperror.Wrap(err, "model_convert_error", "")
}
info := backend.ModelInfo(m.Collection())
data := &ApiModel{
Type: m.Collection(),
Id: m.GetStrId(),
Attributes: modelData,
}
// Build relationship data.
includedModels := make([]*ApiModel, 0)
// Check every model field.
r := reflector.R(m).MustStruct()
for fieldName, rel := range info.Relations() {
// Retrieve the related model.
field := r.Field(fieldName)
if field != nil {
return nil, nil, apperror.Wrap(err, "model_get_field_error")
}
// If field is zero value, skip.
if field.IsZero() {
continue
}
related := make([]kit.Model, 0)
if !rel.IsMany() {
// Make sure that we have a pointer.
if !field.IsPtr() {
field = field.Addr()
}
related = append(related, field.Interface().(kit.Model))
} else {
slice := field.MustSlice()
for _, item := range slice.Items() {
if !item.IsPtr() {
item = item.Addr()
}
related = append(related, item.Interface().(kit.Model))
}
}
for _, relatedModel := range related {
// Convert the related model.
relationData, included, err := SerializeModel(backend, relatedModel)
if err != nil {
return nil, nil, apperror.Wrap(err, "included_model_serialize_error", "")
}
// Build relation info and set in in relationships map.
relation := &ApiModel{
Type: relatedModel.Collection(),
Id: relatedModel.GetStrId(),
}
isSingle := !rel.IsMany()
data.AddRelation(rel.MarshalName(), relation, isSingle)
// Add related model to included data.
includedModels = append(includedModels, relationData)
// Add nested included models to included data.
includedModels = append(includedModels, included...)
}
}
return data, includedModels, nil
}