Golang数据库管理器API概念,类型断言出错。

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

Golang database manager api concept, error with type assertion

问题

创建一个用于通过API获取数据的数据库管理器API的基本概念。我正在使用GORM来获取结构实例的数据。因此,有300-400个结构体代表着表格。

type Users struct {
  ID int64
  Name string
}

type Categories struct {
  ID int64
  Category string
}

接下来,我实现了一个函数,根据通过API端点参数获取的表名返回正确的结构体实例。

func GetModel(model string) interface{} {
  switch model {
  case "users":
    return Users{}
  case "categories":
    return Categories{}
  }
  return false
}

然后,有一个操作结构体,其中唯一的字段是DB。有一些方法,例如GetLast(),我想在其中使用GORM的db.Last(&users)函数。

func (o Operations) GetLast(model string) interface{} {
  modelStruct := GetModel(model)
  // ...
  return o.DB.Last(&modelStruct)
}

这里有一些我不知道的地方。当前的解决方案不起作用,因为在这种情况下,它是一个interface{},我需要进行类型断言在这个问题中有更多信息。类型断言看起来像这样:

func (o Operations) GetLast(model string) interface{} {
  modelStruct := GetModel(model)
  // ...
  test := modelStruct.(Users)
  // ...
  return o.DB.Last(&test)
}

这个解决方案有效,但在这种情况下,我失去了模块化。我尝试使用reflect.TypeOf(modelStruct),但它也不起作用,因为reflect.TypeOf的结果是一个reflect.Type,而不是一个golang类型。

英文:

The base concept creating a Database Manager API for getting data through an API. I am using the GORM for getting data of the instances of the strcuts. So there is 300-400 struct which represents the tables.

type Users struct {
  ID int64
  Name string
}

type Categories struct {
  ID int64
  Category string
}

The next step I implement a function which is return the correct instance of the struct by table name, what I get through the API endpoint param.

func GetModel(model string) interface{} {
  switch model {
  case "users":
    return Users{}
  case "categories"
    return Categories{}
  }
  return false
}

After there is an operations struct where the only one field is the DB. There is methods, for example the GetLast() where I want to use the GORM db.Last(&users) function.

func (o Operations) GetLast(model string) interface{} {
  modelStruct := GetModel(model)
  .
  .
  .
  return o.DB.Last(&modelStruct)
}

There is points so this is what I don't know. The current solution is not working because in this case it is an interface{} I need make a type assertion more info in this question. The type assertion is looks like:

func (o Operations) GetLast(model string) interface{} {
  modelStruct := GetModel(model)
  .
  test := modelStruct.(Users)
  .
  return o.DB.Last(&test)
}

This solution working, but in this case I lost the modularity. I try using the reflect.TypeOf(modelStruct), but it is also not working because the result of the reflect.TypeOf is a reflect.Type, with is not a golang type.

答案1

得分: 0

基本上,我解决了这个问题,通过将模型作为指针获取,并在返回之前将其作为JSON文件返回。

所以我的模型如下:

var Models = map[string]interface{}{
    "users": new(Users),
    "categories": new(Categories),
}

它返回一个新的模型,根据表类型可以用于gorm的First()函数。然后将其进行JSON编组,并返回。

func (o Operation) First(model string, query url.Values) string {
    modelStruct := Models[model]
    db := o.DB
    db.First(modelStruct)
    response, _ := json.Marshal(modelStruct)
    clear(modelStruct)
    return string(response)
}

在返回之前,我清除了模型指针,因为First()函数会存储最新查询的回调函数。

func clear(v interface{}) {
    p := reflect.ValueOf(v).Elem()
    p.Set(reflect.Zero(p.Type()))
}
英文:

Basically I solved the problem, for getting the model as a pointer, and after I return it back as a json file.

So my model is the following:

var Models = map[string]interface{}{
	"users": new(Users),
    "categories": new(Categories),
}

And it is return back a new model by table type. what I can use for gorm First() function. Then json Marshal it, and return.

func (o Operation) First(model string, query url.Values) string {
	modelStruct := Models[model]
	db := o.DB
	db.First(modelStruct)
	response, _ := json.Marshal(modelStruct)
	clear(modelStruct)
	return string(response)
}

Before the return I clear the model pointer because the First() function store callbacks for the latest queries.

func clear(v interface{}) {
	p := reflect.ValueOf(v).Elem()
	p.Set(reflect.Zero(p.Type()))
}

huangapple
  • 本文由 发表于 2017年2月4日 17:38:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/42038782.html
匿名

发表评论

匿名网友

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

确定