logo
0
0
WeChat Login
李智维<2814869489@qq.com>
docs: 更新文档

hclvet

本项目是基于 Hashicorp 官方的 github.com/hashicorp/hcl/v2 实现一个解析 HCL 文件并检查错误的工具,当前只需实现对数据库 schema 定义的 hcl 文件的解析与校验。

hashicorp hcl 官方仓库

主要功能

  1. 解析 HCL 文件,发现基本语法错误并提示
  2. 可以自定义部分检查规则,如字段必须有默认值、字段不许为 NULL、时间戳默认值不许为零值等等

安装

go install e.coding.net/letmelife/open/hclvet@latest

使用方法

# 基本用法 hclvet <hcl文件路径> # 列出所有可用的规则 hclvet -list-rules # 禁用特定规则 hclvet -disable=require-comment,non-zero-datetime <hcl文件路径> # 仅启用特定规则 hclvet -enable=require-primary-key,varchar-length <hcl文件路径>

支持的规则

hclvet 支持以下检查规则,其中一部分规则默认启用:

规则ID说明默认启用
require-comment要求列必须有注释说明
require-default-null要求可为空的列必须设置默认值
non-zero-datetime要求datetime类型的列默认值必须非零
require-primary-key要求表必须定义主键
valid-schema-ref要求表引用的schema必须存在
varchar-length要求varchar类型必须指定长度
type-lowercase要求类型名称必须使用小写字母
table-comment要求表必须有注释说明
column-naming要求列名使用snake_case命名规范

当前支持的检查规则

  1. 基本语法检查(通过 Hashicorp HCL 解析器)
  2. 字段默认值检查:可为空的字段(null = true)应该设置默认值
  3. 时间戳默认值检查:类型为 datetime 的字段默认值要非零值
  4. 主键检查:每个表必须有主键定义
  5. Schema引用检查:表引用的schema必须存在于同一个文件中
  6. Varchar长度检查:varchar类型必须指定长度
  7. 类型名称小写检查:数据类型名称必须使用小写字母(防止使用 Atlas 等工具时出错)
  8. 列命名规范检查:列名应该使用snake_case命名规范(小写字母+下划线)
  9. 字段注释检查:所有字段都应该有注释说明(默认禁用)
  10. 表注释检查:表必须有注释说明(默认禁用)

hcl 示例文件

table "accounting_record" { schema = schema.tcstore comment = "入账记录表" column "id" { null = false type = bigint } column "trade_id" { null = false type = varchar(50) } column "pay_way" { null = false default = -1 type = tinyint comment = "支付方式(0.微信,1.支付宝)" } column "type" { null = false type = tinyint default = 0 } column "goods_flag" { null = false type = tinyint default = 0 comment = "商品类型" } column "amount" { null = false type = decimal(15,2) unsigned = false comment = "实际金额" } column "goods_id" { null = true type = varchar(50) default = "" comment = "商品id" } column "cost_price" { null = true type = decimal(15,2) unsigned = false comment = "成本价" } column "venue_id" { null = true type = varchar(50) default = "" comment = "点位 id" } column "administrator_id" { null = true type = varchar(50) default = "" } column "venue_admin_id" { null = true type = varchar(50) default = "" } column "bd_id" { null = true type = varchar(50) default = "" } column "create_time" { null = true type = datetime } column "update_time" { null = true type = datetime } primary_key { columns = [column.id] } index "accounting_record_trade_id" { columns = [column.trade_id] } } schema "tcstore" { charset = "utf8mb4" collate = "utf8mb4_general_ci" }

如何贡献

  1. Fork 这个仓库
  2. 创建您的功能分支 (git checkout -b feature/amazing-feature)
  3. 提交您的更改 (git commit -m 'Add some amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 打开一个 Pull Request