optimus

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2020 License: MIT Imports: 19 Imported by: 1

README

Optimus

Optimus 用于将 Excel 表格转换成便于程序使用的数据格式, 例如 lua, json, go 等.

Feature

  • 基本数据类型 int32, float32, string, bool, time, duration, repeated.
  • 支持自定义数据类型和自定义解析方法.
  • 支持自定义别名.
  • 支持默认值.
  • 支持横向表以及纵向表.
  • 支持多层级子表 (SubSheetType).
  • 支持解析 reflect.Value 自定义输出类型, 内置输出类型 lua, json, go.
  • 支持 golang 结构体 tag.
  • 支持单文件, 单字段级别的输出范围.
  • 自动合并连续字段, 结构型连续字段.
  • 自动合并以 - 分割的具有相同前缀的 sheet 和 xlsx.
  • 支持未过期文件自动忽略.

基本数据类型

类型 样例 说明
int32 10 范围 -2147483648~2147483647
float32 3.14 范围 ±1.18e−38~±3.4e+38
string optimus
bool true true, True, T, 1, false, False, F, 0
time 2020-01-01 00:00:00 go 转换成 time.Time, lua 转换成时间戳
duration 1h2m3s 最小单位为秒, 最终会转换成 int32

repeated 表示字段为数组类型, 数据之间通过 | 分隔.
eg. repeated int32 表示整型数组, 对应的值可以填写为 10|20|30
在 time 解析时, 默认时区为当前系统时区, 通过-timezone可以自定义时区.

Quick Start

cd cmd
make
cd bin
./optimus --help
./optimus

样例

参考 cmd/example

Documentation

Index

Constants

View Source
const (
	SheetHead_FieldName    = 0
	SheetHead_FieldKind    = 1
	SheetHead_FieldMeta    = 2
	SheetHead_FieldComment = 3
	SheetHead_FieldData    = 4
)
View Source
const (
	SheetHead_SubSheetType = "SubSheetType"
)

Variables

View Source
var (
	MetaModeS   = "s"
	MetaModeC   = "c"
	MetaIgnore  = "ignore"
	MetaTag     = "tag"
	MetaCombine = "combine"
	MetaDefault = "default"

	SerialSplitKey   = "_"
	SerialSplitSheet = "-"
	SerialSplitFile  = "-"
)
View Source
var Timezone = time.Local

Functions

func AlignXlsxFile

func AlignXlsxFile(file *xlsx.File, fileName string, start int)

func ArrayFieldIndex

func ArrayFieldIndex(s string) (prefix string, index int)

func CellInfo

func CellInfo(sheet *xlsx.Sheet, row int, col int) string

func ColS

func ColS(col int) string

func FileExist

func FileExist(filePath string) bool

func InitAliasConfig

func InitAliasConfig(file string) error

func InitConfig

func InitConfig(file string, names ...string) error

func IsFirstArrayField

func IsFirstArrayField(s string) bool

func IsSerialArrayField

func IsSerialArrayField(pre string, cur string) bool

func OpenXlsxFile

func OpenXlsxFile(start int, shouldSort bool, fname string) (file *xlsx.File, err error)

func RegisterFormatter

func RegisterFormatter(typeName string, formatter AliasTypeFormatter, objectType, objectName string, imports []string, notref bool)

func RegisterPrinter

func RegisterPrinter(s string, p Printer)

func RowS

func RowS(row int) string

func SerialStringLess

func SerialStringLess(s1 string, s2 string) bool

func SetTimeZone added in v1.0.2

func SetTimeZone(v string)

Types

type AliasConfig

type AliasConfig struct {
	IndexByAlias      map[string]*AliasInfo
	IndexByObjectType map[string]*AliasInfo
}

func GetAliasConfig

func GetAliasConfig() *AliasConfig

func (*AliasConfig) GetKind

func (this *AliasConfig) GetKind(kind string) string

func (*AliasConfig) GetType

func (this *AliasConfig) GetType(val string) (reflect.Type, error)

func (*AliasConfig) GetValue

func (this *AliasConfig) GetValue(val string, obj string) (reflect.Value, error)

type AliasInfo

type AliasInfo struct {
	ObjectType string
	Alias      string
	Kind       string
	Value      string
}

func (*AliasInfo) GetType

func (this *AliasInfo) GetType() (reflect.Type, error)

func (*AliasInfo) GetValue

func (this *AliasInfo) GetValue(val string, raw bool) (reflect.Value, error)

type AliasTypeFloatFormatter

type AliasTypeFloatFormatter struct{}

float32

func (*AliasTypeFloatFormatter) GetType

func (this *AliasTypeFloatFormatter) GetType(info *AliasInfo) reflect.Type

func (*AliasTypeFloatFormatter) GetValue

func (this *AliasTypeFloatFormatter) GetValue(val string, info *AliasInfo, raw bool) (reflect.Value, error)

type AliasTypeFormatter

type AliasTypeFormatter interface {
	GetType(*AliasInfo) reflect.Type
	GetValue(string, *AliasInfo, bool) (reflect.Value, error)
}

type AliasTypeInt32Formatter

type AliasTypeInt32Formatter struct{}

int32

func (*AliasTypeInt32Formatter) GetType

func (this *AliasTypeInt32Formatter) GetType(info *AliasInfo) reflect.Type

func (*AliasTypeInt32Formatter) GetValue

func (this *AliasTypeInt32Formatter) GetValue(val string, info *AliasInfo, raw bool) (reflect.Value, error)

type AliasTypeStringFormatter

type AliasTypeStringFormatter struct{}

string

func (*AliasTypeStringFormatter) GetType

func (this *AliasTypeStringFormatter) GetType(info *AliasInfo) reflect.Type

func (*AliasTypeStringFormatter) GetValue

func (this *AliasTypeStringFormatter) GetValue(val string, info *AliasInfo, raw bool) (reflect.Value, error)

type Config

type Config struct {
	Conf []*TableConfig
}

func GetConfig

func GetConfig() *Config

type CustomTypeInfo

type CustomTypeInfo struct {
	Name    string
	Imports []string

	// true means Struct, false means *Struct
	NotRef bool
}

func GetCustomTypeInfo added in v1.0.1

func GetCustomTypeInfo(typ string) (*CustomTypeInfo, error)

type FieldType

type FieldType struct {
	Name              string
	Kind              string
	Type              int
	Repeated          bool
	FieldArray        bool // Field1, Field2, Field3...
	FieldIndex        int
	FieldPrefix       string
	FieldCombine      bool // Val1 Attr1, Val2 Attr2 ...
	FieldCombineName  string
	FieldCombineIndex int
	Meta              map[string]string
	Comment           string
}

func NewFieldType

func NewFieldType(name string, kind string, meta string, comment string) *FieldType

func (*FieldType) GetDefault

func (this *FieldType) GetDefault() string

func (*FieldType) GetKind

func (this *FieldType) GetKind() string

func (*FieldType) GetTag

func (this *FieldType) GetTag() string

func (*FieldType) GetType

func (this *FieldType) GetType() (reflect.Type, error)

func (*FieldType) GetValue

func (this *FieldType) GetValue(val string) (reflect.Value, error)

func (*FieldType) Ignore

func (this *FieldType) Ignore(mode string) bool

func (*FieldType) ParseMeta

func (this *FieldType) ParseMeta(meta string)

func (*FieldType) SetMeta

func (this *FieldType) SetMeta(key string, val string)

type Parser

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

func NewParser

func NewParser(conf *TableConfig) *Parser

func (*Parser) Expired

func (this *Parser) Expired(output string) bool

func (*Parser) Output

func (this *Parser) Output(output string) error

func (*Parser) Parse

func (this *Parser) Parse(output string, combine bool) (err error)

type Printer

type Printer interface {
	Print(string, *SheetInfo) *Stream
	Ext() string
}

func GetPrinter

func GetPrinter(s string) (Printer, error)

type SheetInfo

type SheetInfo struct {
	Config               *TableConfig
	Sheet                *xlsx.Sheet
	Fields               []*FieldType
	TypeInfoS            *TypeInfo
	ValueS               reflect.Value
	TypeInfoC            *TypeInfo
	ValueC               reflect.Value
	SubSheetInfo         map[string]*SheetInfo
	LastSubSheetRowIndex int
}

func NewSheetInfo

func NewSheetInfo(c *TableConfig) *SheetInfo

func (*SheetInfo) ParseH

func (this *SheetInfo) ParseH(combine bool) error

func (*SheetInfo) ParseV

func (this *SheetInfo) ParseV() error

func (*SheetInfo) TypeInfo

func (this *SheetInfo) TypeInfo(mode string) *TypeInfo

func (*SheetInfo) Value

func (this *SheetInfo) Value(mode string) reflect.Value

type Stream

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

func NewStream

func NewStream() *Stream

func (*Stream) Buffer

func (self *Stream) Buffer() *bytes.Buffer

func (*Stream) Len

func (self *Stream) Len() int

func (*Stream) Printf

func (self *Stream) Printf(format string, args ...interface{})

func (*Stream) SetBuffer

func (self *Stream) SetBuffer(buf bytes.Buffer)

func (*Stream) WriteBytes

func (self *Stream) WriteBytes(b []byte)

func (*Stream) WriteFile

func (self *Stream) WriteFile(path string) error

type TableConfig

type TableConfig struct {
	Xlsx     string
	Sheet    string
	Name     string
	GenName  string
	Package  string
	COutput  []string
	SOutput  []string
	Vertical bool

	Excel *xlsx.File
}

func NewTableConfig

func NewTableConfig() *TableConfig

func (*TableConfig) Load

func (this *TableConfig) Load() error

func (*TableConfig) ParseOutput

func (this *TableConfig) ParseOutput(content string)

func (*TableConfig) ParseVertical

func (this *TableConfig) ParseVertical(content string)

func (*TableConfig) String

func (this *TableConfig) String() string

type TypeInfo

type TypeInfo struct {
	Name    string
	Type    reflect.Type
	Kind    reflect.Kind
	Tag     string
	Child   []*TypeInfo
	Imports map[string]string
}

func NewTypeInfo

func NewTypeInfo() *TypeInfo

func (*TypeInfo) Append

func (this *TypeInfo) Append(v *TypeInfo)

func (*TypeInfo) MakeStruct

func (this *TypeInfo) MakeStruct(name string, mode string, sheet *SheetInfo) string

func (*TypeInfo) MakeType

func (this *TypeInfo) MakeType() reflect.Type

func (*TypeInfo) MakeValue

func (this *TypeInfo) MakeValue() reflect.Value

func (*TypeInfo) SetType

func (this *TypeInfo) SetType(v interface{}, tag string) *TypeInfo

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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