All skills
gogf avatar

/goframe-v2

@911705d
by GoFramegogf/skills81 stars
3

GoFrame v2 development skill. Use only when the target Go project uses or is explicitly adopting GoFrame v2: the nearest go.mod requires github.com/gogf/gf/v2, existing Go files import github.com/gogf/gf/v2 or any github.com/gogf/gf/v2/... component package, or the user asks to scaffold, migrate, or build with GoFrame. Trigger for GoFrame-backed Go work such as APIs/controllers/services, middleware, routing/config, ORM/DAO/DO/entity/database operations, gf CLI/codegen, HTTP/gRPC services, and microservice conventions. Do not trigger for generic Go projects without GoFrame evidence, frontend-only work, shell scripts, or unrelated infrastructure tasks.

Use this Skill: https://skilld.dev/gh/gogf/skills/goframe-v2

This session only. Nothing lands on disk.

references核心组件数据校验数据校验-自定义规则自定义规则-规则注册.md

相关数据结构

自定义规则方法定义,以及对应的输入参数数据结构。

// RuleFuncInput holds the input parameters that passed to custom rule function RuleFunc.
type RuleFuncInput struct {
    // Rule specifies the validation rule string, like "required", "between:1,100", etc.
    Rule string

    // Message specifies the custom error message or configured i18n message for this rule.
    Message string

    // Value specifies the value for this rule to validate.
    Value *gvar.Var

    // Data specifies the `data` which is passed to the Validator. It might be a type of map/struct or a nil value.
    // You can ignore the parameter `Data` if you do not really need it in your custom validation rule.
    Data *gvar.Var
}

// RuleFunc is the custom function for data validation.
type RuleFunc func(ctx context.Context, in RuleFuncInput) error

方法参数简要说明:

  1. 上下文参数ctx是必须的。
  2. RuleFuncInput数据结构说明:
    • Rule表示当前的校验规则,包含规则的参数,例如:required、between:1,100、length:6等等。
    • Message参数表示在校验失败后返回的校验错误提示信息。
    • Value参数表示被校验的数据值,注意类型是一个*gvar.Var泛型,因此您可以传递任意类型的参数。
    • Data参数表示校验时传递的参数,例如校验的是一个map或者struct时,往往在联合校验时有用。需要注意的是,这个值是运行时输入的,值可能是nil。 :::tip 自定义错误默认情况下已支持i18n特性,因此您只需要按照gf.gvalid.rule.自定义规则名称配置i18n转译信息即可,该信息在校验失败时会自动从i18n管理器获取后,通过Message参数传入给您注册的自定义校验方法中。 :::

全局校验规则注册

自定义规则分为两种:全局规则注册和局部规则注册。

全局规则是全局生效的规则,注册之后无论是使用方法还是对象来执行数据校验都可以使用自定义的规则。

注册校验方法:

// RegisterRule registers custom validation rule and function for package.
func RegisterRule(rule string, f RuleFunc) {
    customRuleFuncMap[rule] = f
}

// RegisterRuleByMap registers custom validation rules using map for package.
func RegisterRuleByMap(m map[string]RuleFunc) {
    for k, v := range m {
        customRuleFuncMap[k] = v
    }
}

您需要按照RuleFunc类型的方法定义,实现一个您需要的校验方法,随后使用RegisterRule注册到gvalid模块中全局管理。该注册逻辑往往是在程序初始化时执行。该方法在对数据进行校验时将会被自动调用,方法返回nil表示校验通过,否则应当返回一个非空的error类型值。 :::warning 注意事项:自定义规则的注册方法不支持并发调用,您需要在程序启动时进行注册(例如在boot包中处理),无法在运行时动态注册,否则会产生并发安全问题。 :::

示例1,订单ID存在校验

在电商业务中,当我们对订单进行操作时,可以通过自定义规则校验给定的订单ID是否存在,因此我们可以注册一个 order-exist 的全局规则来实现。

package main

import (
    "context"
    "fmt"
    "github.com/gogf/gf/v2/database/gdb"
    "github.com/gogf/gf/v2/errors/gerror"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
    "github.com/gogf/gf/v2/util/gvalid"
    "time"
)

type Request struct {
    OrderId     int64 `v:"required|order-exist"`
    ProductName string
    Amount      int64
    // ...
}

func init() {
    rule := "order-exist"
    gvalid.RegisterRule(rule, RuleOrderExist)
}

func RuleOrderExist(ctx context.Context, in gvalid.RuleFuncInput) error {
    // SELECT COUNT(*) FROM `order` WHERE `id` = xxx
    count, err := g.Model("order").
        Ctx(ctx).
        Cache(gdb.CacheOption{
            Duration: time.Hour,
            Name:     "",
            Force:    false,
        }).
        WhereNot("id", in.Value.Int64()).
        Count()
    if err != nil {
        return err
    }
    if count == 0 {
        return gerror.Newf(`invalid order id "%d"`, in.Value.Int64())
    }
    return nil
}

func main() {
    var (
        ctx = gctx.New()
        req = &Request{
            OrderId:     65535,
            ProductName: "HikingShoe",
            Amount:      10000,
        }
    )
    err := g.Validator().CheckStruct(ctx, req)
    fmt.Println(err)
}

示例2,用户唯一性规则

在用户注册时,我们往往需要校验当前用户提交的名称/账号是否唯一,因此我们可以注册一个 unique-name 的全局规则来实现。

package main

import (
    "context"
    "fmt"
    "github.com/gogf/gf/v2/database/gdb"
    "github.com/gogf/gf/v2/errors/gerror"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
    "github.com/gogf/gf/v2/util/gvalid"
    "time"
)

type User struct {
    Id   int
    Name string `v:"required|unique-name#请输入用户名称|用户名称已被占用"`
    Pass string `v:"required|length:6,18"`
}

func init() {
    rule := "unique-name"
    gvalid.RegisterRule(rule, RuleUniqueName)
}

func RuleUniqueName(ctx context.Context, in gvalid.RuleFuncInput) error {
    var user *User
    if err := in.Data.Scan(&user); err != nil {
        return gerror.Wrap(err, `Scan data to user failed`)
    }
    // SELECT COUNT(*) FROM `user` WHERE `id` != xxx AND `name` != xxx
    count, err := g.Model("user").
        Ctx(ctx).
        Cache(gdb.CacheOption{
            Duration: time.Hour,
            Name:     "",
            Force:    false,
        }).
        WhereNot("id", user.Id).
        WhereNot("name", user.Name).
        Count()
    if err != nil {
        return err
    }
    if count > 0 {
        if in.Message != "" {
            return gerror.New(in.Message)
        }
        return gerror.Newf(`user name "%s" is already token by others`, user.Name)
    }
    return nil
}

func main() {
    var (
        ctx  = gctx.New()
        user = &User{
            Id:   1,
            Name: "john",
            Pass: "123456",
        }
    )
    err := g.Validator().CheckStruct(ctx, user)
    fmt.Println(err)
}

局部校验规则注册

局部规则是仅在当前校验对象下生效规则,校验规则是注册到当前使用的链式操作流程中而不是全局中。

注册方法:

// RuleFunc registers one custom rule function to current Validator.
func (v *Validator) RuleFunc(rule string, f RuleFunc) *Validator

// RuleFuncMap registers multiple custom rule functions to current Validator.
func (v *Validator) RuleFuncMap(m map[string]RuleFunc) *Validator

简要介绍:

  • RuleFunc 方法用于注册单个自定义校验规则到当前对象。
  • RuleFuncMap 方法用于注册多个自定义校验规则到当前对象。

使用示例:

我们将上面其中一个例子改为局部校验规则注册。

package main

import (
    "context"
    "fmt"
    "github.com/gogf/gf/v2/database/gdb"
    "github.com/gogf/gf/v2/errors/gerror"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
    "github.com/gogf/gf/v2/util/gvalid"
    "time"
)

type Request struct {
    OrderId     int64
    ProductName string
    Amount      int64
    // ...
}

func RuleOrderExist(ctx context.Context, in gvalid.RuleFuncInput) error {
    // SELECT COUNT(*) FROM `order` WHERE `id` = xxx
    count, err := g.Model("order").
        Ctx(ctx).
        Cache(gdb.CacheOption{
            Duration: time.Hour,
            Name:     "",
            Force:    false,
        }).
        WhereNot("id", in.Value.Int64()).
        Count()
    if err != nil {
        return err
    }
    if count == 0 {
        return gerror.Newf(`invalid order id "%d"`, in.Value.Int64())
    }
    return nil
}

func main() {
    var (
        ctx = gctx.New()
        req = &Request{
            OrderId:     65535,
            ProductName: "HikingShoe",
            Amount:      10000,
        }
    )
    err := g.Validator().RuleFunc("order-exist", RuleOrderExist).Data(req).Run(ctx)
    fmt.Println(err)
}

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk MEDIUM
  • Gen Agent Trust Hub16d

    The skill provides a comprehensive development guide and code examples for the GoFrame v2 framework. While highly informative for Go developers, it contains several examples with hardcoded placeholder secrets, tokens, and specific local file paths to sensitive configuration files (like Kubernetes credentials). These elements are provided for demonstration purposes but represent insecure practices if used in production without modification.

  • Socket16d

    5 alerts: gptSecurity, gptAnomaly

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    227/949 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 911705d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 months ago.

Steadyupdated 4 months ago
  • Go
  • Database
  • goframe
  • orm
  • http
  • grpc
  • microservices
  • crud
  • scaffolding
  • middleware

README badge

README badge for gogf/skills

Provides instruction and conventions for developing with GoFrame v2, a modular Go framework for building HTTP and gRPC services, microservices, and database operations using its ORM, CLI tooling, and project scaffolding. Use this skill when the target project has GoFrame v2 as a dependency or when the user requests scaffolding, migrations, or service development with GoFrame conventions.

Generated from the current SKILL.md.

When should I use this skill?
Use this skill only when your Go project uses GoFrame v2, indicated by github.com/gogf/gf/v2 in go.mod or imports. Do not use for generic Go projects, frontend work, or infrastructure tasks unrelated to GoFrame.
Should I manually set created_at and updated_at fields?
No. GoFrame automatically writes created_at on insert and updated_at on insert/update/save. Manually setting these fields is redundant and violates project conventions.
What should I use for database operations instead of g.Map?
Always use DO objects from internal/model/do/. DO struct fields are interface{}, unset fields remain nil and are automatically ignored by the ORM.
How does soft delete work in GoFrame?
Call Delete() on the DAO; GoFrame automatically converts it to UPDATE SET deleted_at = NOW(). Queries automatically filter out soft-deleted rows without requiring manual WhereNull conditions.
Where should I implement business logic?
Implement business logic directly in the service/ directory. Do not use the logic/ directory unless explicitly requested.

Generated from the current SKILL.md. These answers refresh after source changes.