jy2struct

package
v0.0.0-...-336be2e Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

README

jy2struct

json和yaml转go struct代码。


安装

go get -u github.com/zhufuyi/pkg/jy2struct


使用示例

主要设置参数:

type Args struct {
	Format    string // 文档格式,json或yaml
	Data      string // json或yaml内容
	InputFile string // 文件
	Name      string // 结构体名称
	SubStruct bool   // 子结构体是否分开
	Tags      string // 添加额外tag,多个tag用逗号分隔
}

转换示例:

    // json转struct
    code, err := jy2struct.Covert(&jy2struct.Args{
        Format: "json",
        // InputFile: "user.json", // 来源于json文件
        SubStruct: true,
    })

    // json转struct
    code, err := jy2struct.Covert(&jy2struct.Args{
        Format: "yaml",
        // InputFile: "user.yaml", // 来源于yaml文件
        SubStruct: true,
    })

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ForceFloats bool

ForceFloats whether to force a change to float

Functions

func Covert

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

Covert json或yaml转go struct

func FmtFieldName

func FmtFieldName(s string) string

FmtFieldName formats a string as a struct key

Example:

FmtFieldName("foo_id")

Output: FooID

func Generate

func Generate(input io.Reader, parser Parser, structName, pkgName string, tags []string, subStruct bool, convertFloats bool) ([]byte, error)

Generate a struct definition given a JSON string representation of an object and a name structName.

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 // 文档格式,json或yaml
	Data      string // json或yaml内容
	InputFile string // 文件
	Name      string // 结构体名称
	SubStruct bool   // 子结构体是否分开
	Tags      string // 字段tag,多个tag用逗号分隔
	// contains filtered or unexported fields
}

Args 参数

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