st2

package module
v0.0.0-...-824c794 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 22 Imported by: 0

README

Table of Contents

st2

go codecov GitHub tag Go Reference

st2 provide a package to parse json/yaml/protobuf/thrift/go/csv/xml/toml code and generage go/protobuf/thrift code.

Cli

st2 provide a terminal command line tool st2, which can be used to generate go/protobuf/thrift code from json/yaml/protobuf/thrift/go/csv code.

Install
Use home brew
brew tap tenfyzhong/tap
brew install st2
Download from release

You can download the release of st2 from the GitHub releases.

build from source
go install github.com/tenfyzhong/st2/cmd/st2@latest
Usage
NAME:
   st2 - convert between json, yaml, protobuf, thrift, go struct

USAGE:
   st2 [global options] [arguments...]

VERSION:
   developing

AUTHOR:
   tenfyzhong

GLOBAL OPTIONS:
   --help, -h     show help (default: false)
   --version, -v  print the version (default: false)

   common

   --root name, -r name  The root struct name (default: Root)

   input

   --input file, -i file              Input file, if not set, it will read from stdio
   --rc                               Read input from clipboard (default: false)
   --src type, -s type                The source data type, it will use the suffix of the input file if not set, available value: `[json,yaml,proto,thrift,go,csv,xml,toml]`
   --xml-attribute-tag-prefix prefix  Add prefix to xml attribute tag in go field, only works for xml source and go destination (default: ,)
   --xml-content-tag-prefix prefix    Add prefix to xml content tag in go field, only works for xml source and go destination

   output

   --dst type, -d type     The destination data type, it will use the suffix of the output file if not set, available value: `[go,proto,thrift]`
   --output file, -o file  Output file, if not set, it will write to stdout
   --prefix prefix         Add prefix to struct name
   --suffix suffix         Add suffix to struct name
   --wc                    Write output to clipboard (default: false)


COPYRIGHT:
   Copyright (c) 2022 tenfy

Documentation

Overview

Package st2 provide a package to parse json/protobuf/thrift/go/csv code and generage go/protobuf/thrift code

Index

Constants

View Source
const (
	LangGo     = "go"
	LangJson   = "json"
	LangProto  = "proto"
	LangThrift = "thrift"
	LangCsv    = "csv"
	LangYaml   = "yaml"
	LangYml    = "yml"
	LangXML    = "xml"
	LangToml   = "toml"

	RootDefault = "Root"

	FlagXMLAttributeTagPrefixDefault = ","
)
View Source
const (
	StrInt        = "int"
	StrInt8       = "int8"
	StrInt16      = "int16"
	StrInt32      = "int32"
	StrInt64      = "int64"
	StrUint       = "uint"
	StrUint8      = "uint8"
	StrUint16     = "uint16"
	StrUint32     = "uint32"
	StrUint64     = "uint64"
	StrSint32     = "sint32"
	StrSint64     = "sint64"
	StrI16        = "i16"
	StrI32        = "i32"
	StrI64        = "i64"
	StrFixed32    = "fixed32"
	StrFixed64    = "fixed64"
	StrFloat32    = "float32"
	StrFloat64    = "float64"
	StrDouble     = "double"
	StrFloat      = "float"
	StrBool       = "bool"
	StrString     = "string"
	StrComplex64  = "complex64"
	StrComplex128 = "complex128"
	StrByte       = "byte"
	StrBytes      = "bytes"
	StrRune       = "rune"
	StrUintptr    = "uintptr"
	StrAny        = "any"
	StrPbAny      = "google.protobuf.Any"
	StrBinary     = "binary"
	StrMap        = "map"
	StrList       = "list"
	StrSet        = "set"
	StrNil        = "nil"
	StrNull       = "null"
	StrNumber     = "number"
	StrRepeated   = "repeated"
)

Variables

View Source
var (
	SourceLangs = []Lang{
		{
			Lang: LangJson,
		},
		{
			Lang:    LangYaml,
			Aliases: []string{LangYml},
		},
		{
			Lang: LangProto,
		},
		{
			Lang: LangThrift,
		},
		{
			Lang: LangGo,
		},
		{
			Lang: LangCsv,
		},
		{
			Lang: LangXML,
		},
		{
			Lang: LangToml,
		},
	}

	DestinationLangs = []Lang{
		{
			Lang: LangGo,
		},
		{
			Lang: LangProto,
		},
		{
			Lang: LangThrift,
		},
	}
	LangTmplMap = map[string]string{
		LangGo:     tmpl.Go,
		LangProto:  tmpl.Proto,
		LangThrift: tmpl.Thrift,
	}
)

Functions

func Convert

func Convert(ctx Context, reader io.Reader, writer io.Writer) error

Convert is a wrap function parse from reader and write the output to writer

func CreateTmpl

func CreateTmpl(ctx Context) string

CreateTmpl Get the template data belongs to the context

Types

type AnyType

type AnyType struct{}

AnyType cover json null value, go any value, proto any value

func (AnyType) Go

func (v AnyType) Go() string

func (AnyType) IsBasicType

func (v AnyType) IsBasicType() bool

func (AnyType) Json

func (v AnyType) Json() string

func (AnyType) Proto

func (v AnyType) Proto() string

func (AnyType) Thrift

func (v AnyType) Thrift() string

func (AnyType) Value

func (v AnyType) Value() string

type ArrayType

type ArrayType struct {
	ChildType Type
}

func (ArrayType) Go

func (v ArrayType) Go() string

func (ArrayType) IsBasicType

func (v ArrayType) IsBasicType() bool

func (ArrayType) Json

func (v ArrayType) Json() string

func (ArrayType) Proto

func (v ArrayType) Proto() string

func (ArrayType) Thrift

func (v ArrayType) Thrift() string

type BinaryType

type BinaryType struct{}

func (BinaryType) Go

func (v BinaryType) Go() string

func (BinaryType) IsBasicType

func (v BinaryType) IsBasicType() bool

func (BinaryType) Json

func (v BinaryType) Json() string

func (BinaryType) Proto

func (v BinaryType) Proto() string

func (BinaryType) Thrift

func (v BinaryType) Thrift() string

type BoolType

type BoolType struct {
	V bool
}

func (BoolType) Go

func (v BoolType) Go() string

func (BoolType) IsBasicType

func (v BoolType) IsBasicType() bool

func (BoolType) Json

func (v BoolType) Json() string

func (BoolType) Proto

func (v BoolType) Proto() string

func (BoolType) Thrift

func (v BoolType) Thrift() string

func (BoolType) Value

func (v BoolType) Value() string

type Comment

type Comment struct {
	InlineComment string

	BeginningComments []string
}

Comment record a Member or Struct comments

type Context

type Context struct {
	Src        string
	Dst        string
	Root       string
	Prefix     string
	Suffix     string
	XMLContext XMLContext
}

Context struct contains the context running

func NewContext

func NewContext(src, dst, root, prefix, suffix string, xmlContext XMLContext) Context

type CsvParser

type CsvParser struct {
	// contains filtered or unexported fields
}

CsvParser is a Parser to parse `.csv` source.

func NewCsvParser

func NewCsvParser(ctx Context) *CsvParser

NewCsvParser create CsvParser

func (CsvParser) Parse

func (p CsvParser) Parse(reader io.Reader) ([]*Struct, error)

Parse method parse csv source

type EnumType

type EnumType struct {
	Name string
}

func (EnumType) Go

func (v EnumType) Go() string

func (EnumType) GoStructType

func (v EnumType) GoStructType() string

func (EnumType) IsBasicType

func (v EnumType) IsBasicType() bool

func (EnumType) Json

func (v EnumType) Json() string

func (EnumType) Proto

func (v EnumType) Proto() string

func (EnumType) ProtoStructType

func (v EnumType) ProtoStructType() string

func (EnumType) StructName

func (v EnumType) StructName() string

func (EnumType) Thrift

func (v EnumType) Thrift() string

func (EnumType) ThriftStructType

func (v EnumType) ThriftStructType() string

type Float32Type

type Float32Type struct {
	V float32
}

func (Float32Type) Go

func (v Float32Type) Go() string

func (Float32Type) IsBasicType

func (v Float32Type) IsBasicType() bool

func (Float32Type) Json

func (v Float32Type) Json() string

func (Float32Type) Proto

func (v Float32Type) Proto() string

func (Float32Type) Thrift

func (v Float32Type) Thrift() string

func (Float32Type) Value

func (v Float32Type) Value() string

type Float64Type

type Float64Type struct {
	V float64
}

func (Float64Type) Go

func (v Float64Type) Go() string

func (Float64Type) IsBasicType

func (v Float64Type) IsBasicType() bool

func (Float64Type) Json

func (v Float64Type) Json() string

func (Float64Type) Proto

func (v Float64Type) Proto() string

func (Float64Type) Thrift

func (v Float64Type) Thrift() string

func (Float64Type) Value

func (v Float64Type) Value() string

type GoParser

type GoParser struct {
	// contains filtered or unexported fields
}

GoParser is a Parser to parse golang source.

func NewGoParser

func NewGoParser(ctx Context) *GoParser

NewGoParser create GoParser

func (GoParser) Parse

func (p GoParser) Parse(reader io.Reader) ([]*Struct, error)

Parse method parse golang source

type Int16Type

type Int16Type struct {
	V int16
}

func (Int16Type) Go

func (v Int16Type) Go() string

func (Int16Type) IsBasicType

func (v Int16Type) IsBasicType() bool

func (Int16Type) Json

func (v Int16Type) Json() string

func (Int16Type) Proto

func (v Int16Type) Proto() string

func (Int16Type) Thrift

func (v Int16Type) Thrift() string

func (Int16Type) Value

func (v Int16Type) Value() string

type Int32Type

type Int32Type struct {
	V int32
}

func (Int32Type) Go

func (v Int32Type) Go() string

func (Int32Type) IsBasicType

func (v Int32Type) IsBasicType() bool

func (Int32Type) Json

func (v Int32Type) Json() string

func (Int32Type) Proto

func (v Int32Type) Proto() string

func (Int32Type) Thrift

func (v Int32Type) Thrift() string

func (Int32Type) Value

func (v Int32Type) Value() string

type Int64Type

type Int64Type struct {
	V int64
}

func (Int64Type) Go

func (v Int64Type) Go() string

func (Int64Type) IsBasicType

func (v Int64Type) IsBasicType() bool

func (Int64Type) Json

func (v Int64Type) Json() string

func (Int64Type) Proto

func (v Int64Type) Proto() string

func (Int64Type) Thrift

func (v Int64Type) Thrift() string

func (Int64Type) Value

func (v Int64Type) Value() string

type Int8Type

type Int8Type struct {
	V int8
}

func (Int8Type) Go

func (v Int8Type) Go() string

func (Int8Type) IsBasicType

func (v Int8Type) IsBasicType() bool

func (Int8Type) Json

func (v Int8Type) Json() string

func (Int8Type) Proto

func (v Int8Type) Proto() string

func (Int8Type) Thrift

func (v Int8Type) Thrift() string

func (Int8Type) Value

func (v Int8Type) Value() string

type JsonUnmarshalTagFormat

type JsonUnmarshalTagFormat struct{}

func (JsonUnmarshalTagFormat) TagFormat

func (j JsonUnmarshalTagFormat) TagFormat() string

func (JsonUnmarshalTagFormat) Unmarshal

func (j JsonUnmarshalTagFormat) Unmarshal(data []byte, v any) error

type Lang

type Lang struct {
	Lang    string
	Aliases []string
}

type MapType

type MapType struct {
	Key   Type
	Value Type
}

func (MapType) Go

func (v MapType) Go() string

func (MapType) IsBasicType

func (v MapType) IsBasicType() bool

func (MapType) Json

func (v MapType) Json() string

func (MapType) Proto

func (v MapType) Proto() string

func (MapType) Thrift

func (v MapType) Thrift() string

type Member

type Member struct {
	Field string
	Type
	Index    int
	Optional bool
	Comment  Comment
	GoTag    []string
}

Member is fields of Struct

func (Member) FieldCamel

func (m Member) FieldCamel() string

FieldCamel get a camel type field name

func (Member) Go

func (m Member) Go() string

Go get the golang file type string

func (Member) GoTagString

func (m Member) GoTagString() string

GoTagString get the go field tag string

type NodeList

type NodeList []*rawNode

func (NodeList) Len

func (l NodeList) Len() int

func (NodeList) Less

func (l NodeList) Less(i, j int) bool

func (NodeList) Swap

func (l NodeList) Swap(i, j int)

type Parse

type Parse interface {
	// Parse parse source code to Struct
	Parse(r io.Reader) ([]*Struct, error)
}

Parse interface has a method `Parse` which parse source code to a list of Struct

func CreateParser

func CreateParser(ctx Context) Parse

CreateParser Create a Parse belongs to the context

type ProtoParser

type ProtoParser struct {
	// contains filtered or unexported fields
}

ProtoParser is a Parser to parse protobuf source

func NewProtoParser

func NewProtoParser(ctx Context) *ProtoParser

NewProtoParser create ProtoParser

func (ProtoParser) Parse

func (p ProtoParser) Parse(reader io.Reader) ([]*Struct, error)

Parse method parse protobuf source

type SetType

type SetType struct {
	Key Type
}

func (SetType) Go

func (v SetType) Go() string

func (SetType) IsBasicType

func (v SetType) IsBasicType() bool

func (SetType) Json

func (v SetType) Json() string

func (SetType) Proto

func (v SetType) Proto() string

func (SetType) Thrift

func (v SetType) Thrift() string

type StringType

type StringType struct {
	V string
}

func (StringType) Go

func (v StringType) Go() string

func (StringType) IsBasicType

func (v StringType) IsBasicType() bool

func (StringType) Json

func (v StringType) Json() string

func (StringType) Proto

func (v StringType) Proto() string

func (StringType) Thrift

func (v StringType) Thrift() string

func (StringType) Value

func (v StringType) Value() string

type Struct

type Struct struct {
	Type    Type
	Members []*Member
	Comment Comment
}

Struct is a parsed result which contains the source struct data

type StructLikeSource

type StructLikeSource int

StructLikeSource which type of StructLikeType

const (
	SLSUnknown StructLikeSource = 0 // Unknown type
	SLSStruct  StructLikeSource = 1 // Struct type
	SLSUnion   StructLikeSource = 2 // Union type
)

const values of StructLikeSource

type StructLikeType

type StructLikeType struct {
	Name   string
	Source StructLikeSource
}

func (StructLikeType) Go

func (v StructLikeType) Go() string

func (StructLikeType) GoStructType

func (v StructLikeType) GoStructType() string

func (StructLikeType) IsBasicType

func (v StructLikeType) IsBasicType() bool

func (StructLikeType) Json

func (v StructLikeType) Json() string

func (StructLikeType) Proto

func (v StructLikeType) Proto() string

func (StructLikeType) ProtoStructType

func (v StructLikeType) ProtoStructType() string

func (StructLikeType) StructName

func (v StructLikeType) StructName() string

func (StructLikeType) Thrift

func (v StructLikeType) Thrift() string

func (StructLikeType) ThriftStructType

func (v StructLikeType) ThriftStructType() string

type StructuredParser

type StructuredParser struct {
	// contains filtered or unexported fields
}

StructuredParser is a Parser to parse structured data source

func NewJsonParser

func NewJsonParser(ctx Context) *StructuredParser

NewJsonParser create StructuredParser to parse json source

func NewStructuredParser

func NewStructuredParser(ctx Context, unmarshalTagFormat UnmarshalTagFormat) *StructuredParser

NewStructuredParser create StructuredParser

func NewTomlParser

func NewTomlParser(ctx Context) *StructuredParser

func NewXMLParser

func NewXMLParser(ctx Context) *StructuredParser

func NewYamlParser

func NewYamlParser(ctx Context) *StructuredParser

NewYamlParser create StructuredParser to parse yaml source

func (*StructuredParser) Parse

func (p *StructuredParser) Parse(reader io.Reader) ([]*Struct, error)

Parse method parse structured data source

type ThriftParser

type ThriftParser struct {
	// contains filtered or unexported fields
}

ThriftParser is a Parser to parse thrift source

func NewThriftParser

func NewThriftParser(ctx Context) *ThriftParser

NewThriftParser create ThriftParser

func (ThriftParser) Parse

func (p ThriftParser) Parse(reader io.Reader) ([]*Struct, error)

Parse method parse thrift source

type TomlUnmarshalTagFormat

type TomlUnmarshalTagFormat struct{}

func (TomlUnmarshalTagFormat) TagFormat

func (t TomlUnmarshalTagFormat) TagFormat() string

func (TomlUnmarshalTagFormat) Unmarshal

func (t TomlUnmarshalTagFormat) Unmarshal(data []byte, v any) error

type Type

type Type interface {
	Json() string
	Go() string
	Proto() string
	Thrift() string
	IsBasicType() bool
}
var (
	AnyVal        Type = &AnyType{}
	BoolVal       Type = &BoolType{}
	Float32Val    Type = &Float32Type{}
	Float64Val    Type = &Float64Type{}
	StringVal     Type = &StringType{}
	ArrayVal      Type = &ArrayType{}
	Int8Val       Type = &Int8Type{}
	Int16Val      Type = &Int16Type{}
	Int32Val      Type = &Int32Type{}
	Int64Val      Type = &Int64Type{}
	Uint8Val      Type = &Uint8Type{}
	Uint16Val     Type = &Uint16Type{}
	Uint32Val     Type = &Uint32Type{}
	Uint64Val     Type = &Uint64Type{}
	BinaryVal     Type = &BinaryType{}
	MapVal        Type = &MapType{}
	SetVal        Type = &SetType{}
	EnumVal       Type = &EnumType{}
	StructLikeVal Type = &StructLikeType{}
)

type Uint16Type

type Uint16Type struct {
	V int16
}

func (Uint16Type) Go

func (v Uint16Type) Go() string

func (Uint16Type) IsBasicType

func (v Uint16Type) IsBasicType() bool

func (Uint16Type) Json

func (v Uint16Type) Json() string

func (Uint16Type) Proto

func (v Uint16Type) Proto() string

func (Uint16Type) Thrift

func (v Uint16Type) Thrift() string

func (Uint16Type) Value

func (v Uint16Type) Value() string

type Uint32Type

type Uint32Type struct {
	V int32
}

func (Uint32Type) Go

func (v Uint32Type) Go() string

func (Uint32Type) IsBasicType

func (v Uint32Type) IsBasicType() bool

func (Uint32Type) Json

func (v Uint32Type) Json() string

func (Uint32Type) Proto

func (v Uint32Type) Proto() string

func (Uint32Type) Thrift

func (v Uint32Type) Thrift() string

func (Uint32Type) Value

func (v Uint32Type) Value() string

type Uint64Type

type Uint64Type struct {
	V int64
}

func (Uint64Type) Go

func (v Uint64Type) Go() string

func (Uint64Type) IsBasicType

func (v Uint64Type) IsBasicType() bool

func (Uint64Type) Json

func (v Uint64Type) Json() string

func (Uint64Type) Proto

func (v Uint64Type) Proto() string

func (Uint64Type) Thrift

func (v Uint64Type) Thrift() string

func (Uint64Type) Value

func (v Uint64Type) Value() string

type Uint8Type

type Uint8Type struct {
	V int8
}

func (Uint8Type) Go

func (v Uint8Type) Go() string

func (Uint8Type) IsBasicType

func (v Uint8Type) IsBasicType() bool

func (Uint8Type) Json

func (v Uint8Type) Json() string

func (Uint8Type) Proto

func (v Uint8Type) Proto() string

func (Uint8Type) Thrift

func (v Uint8Type) Thrift() string

func (Uint8Type) Value

func (v Uint8Type) Value() string

type UnmarshalFunc

type UnmarshalFunc func(data []byte, v any) error

type UnmarshalTagFormat

type UnmarshalTagFormat interface {
	Unmarshal(data []byte, v any) error
	TagFormat() string
}

type XMLContext

type XMLContext struct {
	ContentTagPrefix   string
	AttributeTagPrefix string
}

type XMLUnmarshalTagFormat

type XMLUnmarshalTagFormat struct {
	ContentTagPrefix   string
	AttributeTagPrefix string
}

func (XMLUnmarshalTagFormat) TagFormat

func (x XMLUnmarshalTagFormat) TagFormat() string

func (XMLUnmarshalTagFormat) Unmarshal

func (x XMLUnmarshalTagFormat) Unmarshal(data []byte, v any) error

type YamlUnmarshalTagFormat

type YamlUnmarshalTagFormat struct{}

func (YamlUnmarshalTagFormat) TagFormat

func (j YamlUnmarshalTagFormat) TagFormat() string

func (YamlUnmarshalTagFormat) Unmarshal

func (j YamlUnmarshalTagFormat) Unmarshal(data []byte, v any) error

Directories

Path Synopsis
cmd
st2

Jump to

Keyboard shortcuts

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