jy2struct

package
v1.4.43 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 14 Imported by: 0

README

jy2struct

A library for generating go struct code, supporting json and yaml.


Example of use

Main setting parameters.

type Args struct {
	Format    string // document format, json or yaml
	Data      string // json or yaml content
	InputFile string // file
	Name      string // name of structure
	SubStruct bool   // are sub-structures separated
	Tags      string // add additional tags, multiple tags separated by commas
}

Example of conversion.

    import "github.com/18721889353/sunshine/pkg/jy2struct"

    // json convert to struct
    code, err := jy2struct.Convert(&jy2struct.Args{
        Format: "json",
        // InputFile: "user.json", // source from json file
        SubStruct: true,
    })

    // yaml convert to struct
    code, err := jy2struct.Convert(&jy2struct.Args{
        Format: "yaml",
        // InputFile: "user.yaml", // Source from yaml file
        SubStruct: true,
    })

Documentation

Overview

Package jy2struct is a library for generating go struct code, supporting json and yaml.

Index

Constants

This section is empty.

Variables

View Source
var ForceFloats bool

ForceFloats whether to force a change to float

Functions

func Convert

func Convert(args *Args) (string, error)

Convert 将 JSON 或 YAML 内容转换为 Go 结构体代码

优先使用 Data 字段的内容,如果 Data 为空则从 InputFile 读取文件。

参数:

args - 转换参数,包含格式、数据源、结构体名称等配置

返回值:

string - 生成的 Go 结构体代码
error  - 如果参数校验失败、文件读取失败或转换失败则返回错误

func FmtFieldName

func FmtFieldName(s string) string

FmtFieldName formats a string as a struct key

Example:

FmtFieldName("foo_id")

Output: FooID

func ParseJSON

func ParseJSON(input io.Reader) (interface{}, error)

ParseJSON parse json to struct

func ParseYaml

func ParseYaml(input io.Reader) (interface{}, error)

ParseYaml parse yaml to struct

Types

type Args

type Args struct {
	Format    string
	Data      string
	InputFile string
	Name      string
	SubStruct bool
	Tags      string
	// contains filtered or unexported fields
}

Args 转换参数,定义输入格式、内容和输出结构体名称

字段说明:

Format    - 文档格式,支持 "json" 或 "yaml"
Data      - JSON 或 YAML 内容(与 InputFile 二选一)
InputFile - 输入文件路径(与 Data 二选一)
Name      - 生成的根结构体名称
SubStruct - 是否将子结构分离为独立结构体
Tags      - 额外的标签,多个标签用逗号分隔

type Parser

type Parser func(io.Reader) (interface{}, error)

Parser parser function

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL