parser

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: MIT Imports: 20 Imported by: 5

Documentation

Index

Constants

View Source
const (
	ExcelTypeSow                = "sowexcel"
	ExcelTypePartialHarvest     = "partialharvestexcel"
	ExcelTypeFinalHarvest       = "finalharvestexcel"
	ExcelTypeDailyMonitoring    = "dailymonitoringexcel"
	ExcelTypeSampling           = "samplingexcel"
	ExcelTypeProductionPlan     = "productionplanexcel"
	ExcelTypeFarmTechnician     = "farmtechnicianexcel"
	ExcelTypeLabAnalyst         = "labanalystexcel"
	ExcelTypeOrderBulkAquaCheck = "bulkorderaquacheck"
	ExcelTypeHealthIndex        = "healthindexexcel"
	MetricName                  = "metric"

	ParsingErrMessage      = "err:%+v mapper:%+v row:%d  column: %d "
	ExcelTypeDynamicParser = "dynamic"
	SheetIndexRegex        = `^([a-zA-Z\d]+)(.*)$`
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CsvInterface

type CsvInterface interface {
	SetOptionsRead(opt CsvOptions)
	SetOptionsWrite(opt CsvOptions)
	Marshal(orig interface{}) ([]byte, error)
	MarshalWithoutHeaders(orig interface{}) ([]byte, error)
	Unmarshal(blob []byte, dest interface{}) error
}

type CsvOptions

type CsvOptions struct {
	Separator  rune
	LazyQuotes bool
}

type ExcelError

type ExcelError []struct {
	RowNum   int
	ColNum   int
	Value    string // Old Value
	Position string // "A1, B2, C3"
	Message  string
}

TODO: implemented on one template in curent service, later will be moved here

type ExcelHeader

type ExcelHeader struct {
	Parent      string
	ParentValue string
	Name        string
	Value       string
	ColIndex    int
}

type ExcelInput

type ExcelInput struct {
	SheetName  string
	SheetValue map[string]string
}

ExcelInput is used as the structure to generate new excel file

type ExcelInterface

type ExcelInterface interface {
	// General Excel
	Unmarshal(ctx context.Context, blob []byte) ([]map[string]string, error)

	// Opiniated Excel
	UnmarshalTransform(ctx context.Context, blob []byte, t Transformer) (ExcelResult, error)
	UnmarshalTransformV2(ctx context.Context, blob []byte, t Transformer) (ExcelResult, error)
	Validate(ctx context.Context, blob []byte, t Transformer) error
	UnmarshalWithAxis(ctx context.Context, blob []byte, t Transformer) (ExcelResult, error)
	UnmarshalTransformMultipleSheet(ctx context.Context, blob []byte, opt ExcelOption) (map[string]ExcelResult, error)
	UnmarshalTransformMultipleSheetV2(ctx context.Context, blob []byte, opt ExcelOption) (map[string]ExcelResult, error)
	UnmarshalTransformYAxis(ctx context.Context, blob []byte, t Transformer) (ExcelResult, error)

	// Generate excel file
	Marshal(ctx context.Context, input []ExcelInput, pathToFile string) error

	WriteReader(ctx context.Context, blob []byte, ioReader io.Writer) error
	InjectValue(ctx context.Context, blob []byte, sheetName string, values map[string]interface{}) ([]byte, error)
}

type ExcelOption

type ExcelOption struct {
	Type      string        `json:"type"`
	Version   string        `json:"version"`
	Parser    Transformer   `json:"parser"`
	Parsers   []Transformer `json:"parsers"`
	ParserMap map[string]Transformer
}

type ExcelOptions

type ExcelOptions map[string]ExcelOption

Note For next iteration parse column dynamically, read from column name and automatically map it

type ExcelPoint

type ExcelPoint struct {
	Value    string
	RowNum   int
	ColNum   int
	Position string
}

func EmptyRow

func EmptyRow(nCol int) []ExcelPoint

type ExcelResult

type ExcelResult struct {
	Header  ExcelResultHeader
	Data    []ExcelResultData
	Details []ExcelResultDetails
}

type ExcelResultData

type ExcelResultData map[string]interface{}

type ExcelResultDetails

type ExcelResultDetails map[string]ExcelPoint

type ExcelResultHeader

type ExcelResultHeader map[string]ExcelHeader

type Filter

type Filter struct {
	ColNum int
	Regex  string // regex -> pass data when regex is matched
}

type JsonInterface

type JsonInterface interface {
	// Marshal go structs into bytes
	Marshal(orig interface{}) ([]byte, error)
	// Marshal go structs into bytes and validates returned bytes
	MarshalWithSchemaValidation(sch string, orig interface{}) ([]byte, error)
	// Unmarshal bytes into go structs
	Unmarshal(blob []byte, dest interface{}) error
	// Validates input bytes and Unmarshal
	UnmarshalWithSchemaValidation(sch string, blob []byte, dest interface{}) error
}

type JsonOptions

type JsonOptions struct {
	Config                        jsonConfig
	IndentionStep                 int
	MarshalFloatWith6Digits       bool
	EscapeHTML                    bool
	SortMapKeys                   bool
	UseNumber                     bool
	DisallowUnknownFields         bool
	TagKey                        string
	OnlyTaggedField               bool
	ValidateJSONRawMessage        bool
	ObjectFieldMustBeSimpleString bool
	CaseSensitive                 bool
	// Schema contains schema definitions with key as schema name and value as source path
	// schema sources can be file or URL. Schema definition will be iniatialized during
	// JSON parser object initialization.
	Schema map[string]string
}

type Mapper

type Mapper struct {
	Name        string
	ColNum      int
	Type        string
	Relation    string
	Axis        string
	RelationMap map[string]int64
}

func (*Mapper) ParseString

func (m *Mapper) ParseString(input string) (interface{}, error)

type MapperV2

type MapperV2 struct {
	Name        string
	Type        string
	Relation    string
	RelationMap map[string]int64
	Multiple    bool
	MultiColumn bool
	FilterRegex string
	Mappers     []Mapper
}

func (MapperV2) ParseString

func (m MapperV2) ParseString(input string) (interface{}, error)

func (MapperV2) ParseStruct

func (m MapperV2) ParseStruct(name string, input []ExcelPoint) (interface{}, error)

type MapperYAxis

type MapperYAxis struct {
	KeysColumn []string
	MappersVal map[string]Mapper
}

type Options

type Options struct {
	JsonOptions  JsonOptions
	CsvOptions   CsvOptions
	ExcelOptions ExcelOptions
}

type Parser

type Parser interface {
	JsonParser() JsonInterface
	CsvParser() CsvInterface
	ExcelParser() ExcelInterface
}

func InitParser

func InitParser(log logger.Interface, opt Options) Parser

type Transformer

type Transformer struct {
	Name            string
	SheetType       string
	SheetName       string
	SheetRegex      string
	SheetSeparators string

	SlicerInitialSkipRows int
	SlicerInitialSkipCols int
	SlicerNumRows         int
	SlicerNumCols         int
	IsDynamicHeader       bool
	Filters               []Filter

	Mappers []Mapper // Beware it's not exactly same number with the excel col number

	MappersV2   map[string]MapperV2
	MapperYAxis MapperYAxis
}

Jump to

Keyboard shortcuts

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