Skip to content

Not working gen.WithMethod #755

@miya-masa

Description

@miya-masa

Description

DDL:

CREATE TABLE "my_table" (
  "id" serial PRIMARY KEY
);

My configuration

// configuration.go
package main

import (
	"flag"

	"gorm.io/driver/postgres"
	"gorm.io/gen"
	"gorm.io/gorm"
)

func main() {
	var outPath string
	var dsn string
	flag.StringVar(&outPath, "outPath", "", "outPath")
	flag.StringVar(&dsn, "dsn", "", "dsn")
	flag.Parse()

	// Initialize the generator with configuration
	g := gen.NewGenerator(gen.Config{
		OutPath:       outPath, // output directory, default value is ./query
		Mode:          gen.WithDefaultQuery | gen.WithQueryInterface,
		FieldNullable: true,
	})

	// Initialize a *gorm.DB instance
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}

	g.UseDB(db)
	g.ApplyBasic(g.GenerateModel("my_table", gen.WithMethod(CommonMethod{})))
	g.Execute()
}

type CommonMethod struct {
	ID int32
}

func (m *CommonMethod) IsEmpty() bool {
	if m == nil {
		return true
	}
	return m.ID == 0
}

func (m *CommonMethod) GetID() int {
	return int(m.ID)
}

Generated Model:

// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.

package model

const TableNameMyTable = "my_table"

// MyTable mapped from table <my_table>
type MyTable struct {
	ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
}

// TableName MyTable's table name
func (*MyTable) TableName() string {
	return TableNameMyTable
}

I expect to exist CommonMethods, but there is no its methods.

I think wrong a skip number of here .
When I call GenerateModel I think it's 5.

When i call GenerateModelAs, it will work correctly.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions