【问题标题】:Golang with GORM. How to correctly pass model type into method?Golang 与 GORM。如何正确地将模型类型传递给方法?
【发布时间】:2019-05-17 14:26:36
【问题描述】:

我有下一个 GORM 模型

package entity

import (
    "github.com/jinzhu/gorm"
)

type InterfaceEntity interface {
}

type User struct {
    InterfaceEntity
    gorm.Model
    Name string
}

我尝试将 GORM 实体类型传递到基础 crud 存储库。我的基础 crud 存储库:

package repository

import (
    "billingo/model/entity"
    "fmt"
    "github.com/jinzhu/gorm"
    "reflect"
)

type CrudRepository struct {
    *BaseRepository
}

func NewCrudRepository(db *gorm.DB) CrudRepositoryInterface {
    repo := NewBaseRepository(db).(*BaseRepository)
    return &CrudRepository{repo}
}

func (c CrudRepository) Find(id uint, item entity.InterfaceEntity) entity.InterfaceEntity {
    fmt.Println("--- Initial")
    var local entity.User
    fmt.Println("--- local: ", reflect.TypeOf(local), local)
    fmt.Println("--- Item:  ", reflect.TypeOf(item), item)


    fmt.Println("--- Values")
    c.db.First(&local, id)
    fmt.Println("--- local: ", reflect.TypeOf(local), local)
    c.db.First(&item, id)
    fmt.Println("--- Item: ", reflect.TypeOf(item), item)
    return item
}

您可以在此处看到Find() 方法中的itemlocal 变量。

我使用服务中的下一个方式通过item

func (c CrudService) GetItem(id uint) entity.InterfaceEntity {
    var item entity.User
    return c.repository.Find(id, item)
}

看来localitem 必须相等,行为必须相等。

但是输出是

--- Initial
--- local:  entity.User {<nil> {0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} }
--- Item:   entity.User {<nil> {0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} }
--- Values
--- local:  entity.User {<nil> {1 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} test 1}
--- Item:  entity.User {<nil> {0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} }
INFO[0000] User info                                     user="{<nil> {0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} }" user-id=1

(/home/mnv/go/src/billingo/model/repository/CrudRepository.go:29) 
[2019-05-17 17:07:37]  unsupported destination, should be slice or struct 

item 从服务传递到消息

不支持的目标,应该是切片或结构

如何正确传递item,我需要像local 这样的行为?

【问题讨论】:

    标签: go go-gorm go-interface


    【解决方案1】:

    Gorm 不想将数据解组到您的空接口类型中。

    即使您传入一个实现该特定接口的结构,它在传递后仍保持类型为接口。您需要将该 item 接口转换回您的 User 结构。

    点赞item.(entity.User)

    【讨论】:

    • 感谢您的回复。 item.(entity.User) 运作良好。但是我需要创建更多不知道模型类型的抽象方法。我是 Golang 的新手。我可以使用item(reflect.TypeOf(item)) 之类的东西吗?
    【解决方案2】:

    哦,我已经修好了。

    现在使用&amp;item从服务调用存储库方法Find

    func (c CrudService) GetItem(id uint) entity.InterfaceEntity {
        var item entity.User
        return c.repository.Find(id, &item)
    }
    

    并且存储库方法通过item 没有&amp;

    func (c CrudRepository) Find(id uint, item entity.InterfaceEntity) entity.InterfaceEntity {
        c.db.First(item, id)
        return item
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 2012-12-13
      • 2021-09-01
      • 2021-08-15
      • 1970-01-01
      相关资源
      最近更新 更多