xlsxcfg

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: MIT Imports: 12 Imported by: 0

README

xlsxcfg

xlsxcfg load config data from excel sheets.

Installation

Use one of the following ways:

  1. Download from release.

    Recommended to add it to your PATH env.

  2. Run the following command:

    ⚠need golang sdk installed

    go install github.com/dashengyeah/xlsxcfg/bin/xlsxcfg

Usage

Usage:
  xlsxcfg [flags] [xlsx files...]
Flags:
  -c, --config string    config file (default "xlsxcfg.yaml")
      --example-config   export an example config file here
  -h, --help             help for xlsxcfg

Dir structure:

./
|- proto
|  |
|  |- example.proto
|
|- example.xlsx
|- example1.xlsx
|- example2.xlsx
|- xlsxcfg.yaml

xlsxcfg.yaml

proto:
  files: ["proto/example.proto"]
  import_path: ["proto"]
sheet:
  comment_rows: [1]
  meta_row: 2
  data_row_start: 3
  type_suffix: "Sheet"
  list_field_name: "List"
  row_type_suffix: "SheetRow"
output:
  dir: "."
  write_json: true
  json_indent: "  "
  write_bytes: true
  1. full example:

    xlsxcfg -c xlsxcfg.yaml example.xlsx

  2. if the config file is named xlsxcfg.yaml and is in current dir, the config file flag can be ommitted.

    xlsxcfg example.xlsx

  3. it's allowed to specified more than one excel files in the same time.

    xlsxcfg -c xlsxcfg.yaml example.xlsx example1.xlsx example2.xlsx

Config

Default config file xlsxcfg.yaml:

# Protocol Buffer source config
proto:
  # proto files need to parse.
  files: ["example.proto"]
  # proto message import paths.
  import_path: ["."]
# xlsx sheet config
sheet:
  # rows that is comment, will ignore
  # in parsing
  comment_rows: [1]
  # row contains metadata
  meta_row: 2
  # rows contain config data from this
  data_row_start: 3
  # sheet name should add this suffix
  # to find the proto message.
  type_suffix: "Sheet"
  # config data rows' list name in the
  # sheet proto message.
  list_field_name: "List"
  # sheet name should add this suffix
  # to find the data row proto message.
  row_type_suffix: "SheetRow"
# output file config
output:
  # output dir
  dir: "."
  # json
  write_json: true
  json_indent: "  "
  # proto bytes
  write_bytes: true

Examples

  1. Basic
  • Protocol Buffer code:
message PhoneNumber {
    int64  Region = 1;
    int64  No     = 2;
    string Ext    = 3;
}

message MemberSheetRow {
    int32                ID      = 1;
    string               Name    = 2;
    string               Address = 3;
    PhoneNumber          Phone   = 4;
    repeated string      Cities  = 5;
    repeated PhoneNumber PP      = 6;
}
message MemberSheet {
    repeated MemberSheetRow List = 1;
}
  • Excel sheet:

image

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsStrField

func IsStrField(md *desc.MessageDescriptor, path ...string) bool

func LoadXlsxFiles

func LoadXlsxFiles(ctx context.Context, param *Parameter, files ...string) (data map[string][]any, err error)

Types

type Config

type Config struct {
	Proto struct {
		Files      []string `yaml:"files"`
		ImportPath []string `yaml:"import_path"`
	} `yaml:"proto"`
	Sheet struct {
		CommentRows   []int  `yaml:"comment_rows"`
		MetaRow       int    `yaml:"meta_row"`
		DataRowStart  int    `yaml:"data_row_start"`
		TypeSuffix    string `yaml:"type_suffix"`
		ListFieldName string `yaml:"list_field_name"`
		RowTypeSuffix string `yaml:"row_type_suffix"`
	} `yaml:"sheet"`
	Output struct {
		Dir        string `yaml:"dir"`
		WriteJSON  bool   `yaml:"write_json"`
		JSONIndent string `yaml:"json_indent"`
		WriteBytes bool   `yaml:"write_bytes"`
	} `yaml:"output"`
}

func ConfigFromFile

func ConfigFromFile(f string) (*Config, error)

type Parameter

type Parameter struct {
	*Config
	// contains filtered or unexported fields
}

func NewParameter

func NewParameter(cfg *Config, tp TypeProvidor) *Parameter

func (*Parameter) IsComment

func (p *Parameter) IsComment(i int, row []string) bool

func (*Parameter) IsData

func (p *Parameter) IsData(i int, row []string) bool

func (*Parameter) IsMeta

func (p *Parameter) IsMeta(i int, row []string) bool

func (*Parameter) IsStrField

func (p *Parameter) IsStrField(typeName, fieldPath string) bool

type TypeProvidor

type TypeProvidor interface {
	MessageByName(string) *desc.MessageDescriptor
}

func LoadProtoFiles

func LoadProtoFiles(importPaths []string, fileNames ...string) (tp TypeProvidor, err error)

Directories

Path Synopsis
bin

Jump to

Keyboard shortcuts

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