该功能特性从v2.4版本开始提供。

基本介绍

该命令用于读取配置的数据库,根据数据表生成对应的proto数据结构文件。

命令使用

$ gf gen pbentity -h
USAGE
    gf gen pbentity [OPTION]

OPTION
    -p, --path                 directory path for generated files storing
    -k, --package              package path for all entity proto files
    -l, --link                 database configuration, the same as the ORM configuration of GoFrame
    -t, --tables               generate models only for given tables, multiple table names separated with ','
    -f, --prefix               add specified prefix for all entity names and entity proto files
    -r, --removePrefix         remove specified prefix of the table, multiple prefix separated with ','
    -rf, --removeFieldPrefix   remove specified prefix of the field, multiple prefix separated with ','
    -n, --nameCase             case for message attribute names, default is "Camel":
                               | Case            | Example            |
                               |---------------- |--------------------|
                               | Camel           | AnyKindOfString    |
                               | CamelLower      | anyKindOfString    | default
                               | Snake           | any_kind_of_string |
                               | SnakeScreaming  | ANY_KIND_OF_STRING |
                               | SnakeFirstUpper | rgb_code_md5       |
                               | Kebab           | any-kind-of-string |
                               | KebabScreaming  | ANY-KIND-OF-STRING |
    -j, --jsonCase             case for message json tag, cases are the same as "nameCase", default "CamelLower".
                               set it to "none" to ignore json tag generating.
    -o, --option               extra protobuf options
    -h, --help                 more information about this command

EXAMPLE
    gf gen pbentity
    gf gen pbentity -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
    gf gen pbentity -p ./protocol/demos/entity -t user,user_detail,user_login
    gf gen pbentity -r user_ -k github.com/gogf/gf/example/protobuf
    gf gen pbentity -r user_

CONFIGURATION SUPPORT
    Options are also supported by configuration file.
    It's suggested using configuration file instead of command line arguments making producing. 
    The configuration node name is "gf.gen.pbentity", which also supports multiple databases, for example(config.yaml):
    gfcli:
      gen:
      - pbentity:
            link:    "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
            path:    "protocol/demos/entity"
            tables:  "order,products"
            package: "demos"
      - pbentity:
            link:    "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
            path:    "protocol/demos/entity"
            prefix:  "primary_"
            tables:  "user, userDetail"
            package: "demos"
            option:  |
              option go_package    = "protobuf/demos";
              option java_package  = "protobuf/demos";
              option php_namespace = "protobuf/demos";

如果使用框架推荐的项目工程脚手架,并且系统安装了make工具,也可以使用make pbentity快捷指令。

参数说明:

名称默认值含义示例
gfcli.gen.pbentity
代码生成配置项,可以有多个配置项构成数组,支持多个数据库生成。不同的数据库可以设置不同的生成规则,例如可以生成到不同的位置或者文件。-
pathmanifest/protobuf/pbentity

生成proto文件的存储目录地址。

protobuf/pbentity
package自动识别go.mod生成的proto文件中的go_package路径,并自动识别package名称-
link
分为两部分,第一部分表示你连接的数据库类型mysql, postgresql等, 第二部分就是连接数据库的dsn信息。具体请参考 ORM使用配置 章节。

-

prefix
生成数据库对象及文件的前缀,以便区分不同数据库或者不同数据库中的相同表名,防止数据表同名覆盖。

order_

user_

removePrefix
删除数据表的指定前缀名称。多个前缀以,号分隔。gf_
removeFieldPrefix
删除字段名称的指定前缀名称。多个前缀以,号分隔。f_
tables
指定当前数据库中需要执行代码生成的数据表。如果为空,表示数据库的所有表都会生成。user, user_detail
nameCaseCamelLower生成的message属性字段名称格式。参数可选为:CamelCamelLowerSnakeSnakeScreamingSnakeFirstUpperKebabKebabScreaming。具体介绍请参考命名行帮助示例。Snake
option
额外的proto option配置列表

gen dao中的entity差别

相同之处

  • 两者生成的内容都是entity内容,即从数据集合(数据库表)中生成对应的Golang实体对象供程序方便使用。并且都是单向生成,即只能从数据集合生成实体对象代码,以保证实体对象数据结构的同步。
  • gen dao生成的entity数据实体对象是对于Golang语言来说是通用的,但目前主要为HTTP协议服务。在HTTP服务中,gen dao中生成的entity虽然是在internal目录下,但最终也会作为HTTP API返回的一部分服务客户端。

不同之处

  • GRPC服务中,gen dao生成的entity数据结构无法提供给GRPC接口使用,因为GRPC的数据结构需要使用proto文件来定义。因此,在GRPC服务中就需要使用到gen pbentity中生成的pbentity proto文件。同时,在GRPC微服务开发中,gen dao生成的entity已经没有具体作用。
  • 取名pbentity而不是entity的名称,是为了防止和gen dao中的entity含义冲突。










Content Menu

  • No labels

4 Comments

  1. 正确的配置格式应该是这样的

    gfcli:
      gen:
        pbentity:
          - link:    "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
            path:    "manifest/protobuf/pbentity"
            tables:  "order,products"
            package: "pbentity"

    注意横杠的位置

    1. 单个数据源是这样配置的,如果是多个还是要按官方文档上的demo来

  2. lin

    gf gen pbentity -j Snake 不生效, 生成的pb文件的字段还是采用大写字母开头

    1. 字段用-n Snake 

      NameCase     string `name:"nameCase"     short:"n" brief:"{CGenPbEntityBriefNameCase}" d:"Camel"`