Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@BlackHole1
Copy link

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

Support Field-Level Permission in generating the model.

User Case Description

create table users
(
    id         int                                      not null comment 'id'
        primary key,
    username   varchar(40)                              not null comment 'username',
    password   varchar(60)                              not null comment 'password',
    created_at datetime(3) default current_timestamp(3) not null comment 'created time',
    updated_at datetime(3) default current_timestamp(3) not null on update current_timestamp(3) comment 'updated time',
);

The above MySQL code shows that created_at and updated_at have default values, and updated_at has an on update hook.
This means that updates to these two fields do not need to be reflected in the project code, and we do not expect developers to write to these two fields.

If Field-Level Permission is not supported, some developers may accidentally fill in these two fields during the development process and the result will not be as expected.

@qqxhb
Copy link
Member

qqxhb commented Feb 22, 2023

@BlackHole1 FieldGORMTag

@BlackHole1
Copy link
Author

BlackHole1 commented Feb 22, 2023

@qqxhb Yes, FieldGORMTag does do it to some extent, but there is a problem that it overwrites all the contents of gormTag.

g := gen.NewGenerator(gen.Config{
    Mode:              gen.WithoutContext,
    ModelPkgPath:      modelsPath,
    FieldWithIndexTag: true,
    FieldWithTypeTag:  true,
})
// ....
g.GenerateModelAs(table, db.Config.NamingStrategy.SchemaName(table), gen.FieldGORMTag("created_at", "->"))

Result:

type User struct {
    CreatedAt    time.Time `gorm:"->" json:"created_at"`  // created time
}

Expected results:

type User struct {
    CreatedAt    time.Time `gorm:"->;column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3)" json:"created_at"`  // created time
}

@BlackHole1
Copy link
Author

BlackHole1 commented Feb 22, 2023

I want to ensure that gen supports Field-Level Permission while automatically generating gorm tags, rather than writing all gorm tags manually.

@qqxhb
Copy link
Member

qqxhb commented Feb 22, 2023

@BlackHole1 FieldLevelPermission It is not universal. This may result in adding an option for each tag of gorm.

@BlackHole1
Copy link
Author

BlackHole1 commented Feb 22, 2023

I don't quite understand "It is not universal", as far as I know, Field-Level Permission doesn't state on the official documentation that this feature is non-generic.

我们也可以使用中文,这样可以一定程度上避免沟通问题,我从一开始选择英文,是因为这对于开源社区来说会更友好。

@qqxhb
Copy link
Member

qqxhb commented Feb 22, 2023

I don't quite understand "It is not universal", as far as I know, Field-Level Permission doesn't state on the official documentation that this feature is non-generic.

我们也可以使用中文,这样可以一定程度上避免沟通问题,我从一开始选择英文,是因为这对于开源社区来说会更友好。

哈哈 ,没问题。我想说的是,提供一个更换tag的option更通用,如果是加更换局部的,可能会导致option数量暴涨,每一个小点都可能要一个option

@BlackHole1
Copy link
Author

BlackHole1 commented Feb 22, 2023

提供一个更换tag的option更通用

Good idea。从这个方面来看,我们可以提供实现以下其中一个 API(可以讨论哪种方式会更好):

方案一

// 定义
func FieldGORMTagHandler(func(columnName, gormTag string) string) model.FilterFieldOpt

// 使用
gen.FieldGORMTagHandler(func(columnName, gormTag string) string {
    if (columnName == "created_at") {
        return "->;" + gormTag
    }

    return gormTag
})

方案二

// 定义
func FieldGORMTagHandler(func(gormTag string) string, columnNames ...string) model.FilterFieldOpt

// 使用
gen.FieldGORMTagHandler(func(gormTag string) string {
    return "->;" + gormTag
}, "created_at", "updated_at")

FieldGORMTagHandler 可能不是一个好名字,我们也可以想出其他的名字,如: FieldGORMTagOverwrite / FieldGORMTagRewrite

@BlackHole1
Copy link
Author

ping @qqxhb

@qqxhb
Copy link
Member

qqxhb commented Apr 11, 2023

#784
e.g.

gen.FieldGORMTag("created_at", func(tag field.GormTag) field.GormTag {//处理gorm tag
				tag.Set("<-", "false") //加只读 tag
				tag.Set("serialize","json")
				//tag.Remove("type")     //移除gorm的 type tag
				return tag
			}),

@qqxhb qqxhb closed this Apr 11, 2023
@BlackHole1 BlackHole1 deleted the add-fidld-level-permission branch April 11, 2023 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants