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

基本介绍

校验结果为一个error错误对象,内部使用gvalid.Error对象实现。当数据规则校验成功时,校验方法返回的结果为nil。当数据规则校验失败时,返回的该对象是包含结构化的层级map,包含多个字段及其规则及对应错误信息,以便于接收端能够准确定位错误规则。相关数据结构及方法如下: https://pkg.go.dev/github.com/gogf/gf/v2/util/gvalid

type Error interface {
    Code() gcode.Code
    Current() error
    Error() string
    FirstItem() (key string, messages map[string]error)
    FirstRule() (rule string, err error)
    FirstError() (err error)
    Items() (items []map[string]map[string]error)
    Map() map[string]error
    Maps() map[string]map[string]error
    String() string
    Strings() (errs []string)
}

可以结合后续的示例理解这个数据结构。我们可以通过Maps()方法获得该原始错误信息数据结构map。但在大多数时候我们可以通过Error接口的其他方法来方便地获取特定的错误信息。 :::tip 大多数情况下,我们不关心具体出错的校验规则,可以使用Error/String方法直接返回所有的错误信息即可。大部分的方法获取错误信息时根据校验规则的顺序性与否,返回的结果顺序会不太一样。 ::: 简要说明:

获取校验结果的值可以通过多个校验结果方法获取,为让各位开发者有充分的理解,详细说明以下:

方法 说明
Code 常用方法。实现 gerror 的 Code 接口,在校验组件中,该方法固定返回错误码 gcode.CodeValidationFailed。
Error 常用方法。实现标准库的 error.Error 接口,获取返回所有校验错误组成的错误字符串。内部逻辑同 String 方法。
Current 常用方法。实现了 gerror 的 Current 接口,用于获取校验错误中的第一条错误对象。
Items 在顺序性校验中将会按照校验规则顺序返回校验错误数组。其顺序性只有在顺序校验时有效,否则返回的结果是随机的。
Map 返回 FirstItem 中得出错自规则及对应错误信息 map。
Maps 返回所有的出错键名及对应的出错规则及对应的错误信息( map[string]map[string]error)。
String 返回所有的错误信息,构成一条字符串返回,多个规则错误信息之间以 ; 符号连接。其顺序性只有使用顺序校验规则时有效,否则返回的结果是随机的。
Strings 返回所有的错误信息,构成 []string 类型返回。其顺序性只有使用顺序校验规则时有效,否则返回的结果是随机的。
FirstItem 在有多个键名/属性校验错误的时候,用以获取出错的第一个键名,以及其对应的出错规则和错误信息。其顺序性只有使用顺序校验规则时有效,否则返回的结果是随机的。
FirstRule 返回 FirstItem 中得第一条出错的规则及错误信息。其顺序性只有使用顺序校验规则时有效,否则返回的结果是随机的。
FirstString 返回 FirstRule 中得第一条规则错误信息。其顺序性只有使用顺序校验规则时有效,否则返回的结果是随机的。

gerror.Current 支持

我们可以看到, gvalid.Error 实现了 Current() error 接口,因此可以通过 gerror.Current 方法获取它的第一条错误信息,这在接口校验失败时返回错误信息非常方便。我们来看一个示例:

package main

import (
    "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"
)

func main() {
    type User struct {
        Name string `v:"required#请输入用户姓名"`
        Type int    `v:"required|min:1#|请选择用户类型"`
    }
    var (
        err  error
        ctx  = gctx.New()
        user = User{}
    )
    if err = g.Validator().Data(user).Run(ctx); err != nil {
        g.Dump(err.(gvalid.Error).Maps())
        g.Dump(gerror.Current(err))
    }
}

这里使用了 gerror.Current(err) 来获取校第一条验错误信息。执行后,终端输出:

{
    "Name": {
        "required": "请输入用户姓名",
    },
    "Type": {
        "min": "请选择用户类型",
    },
}
"请输入用户姓名"

:::warning 需要注意的是,数据校验时存在 顺序性校验 和 非顺序性校验,这会对获取第一条错误信息的结果产生影响。关于顺序与非顺序性校验,具体可以参考后续章节介绍。 :::

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.