layouts

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: MIT Imports: 11 Imported by: 0

README

Golang Layouts Files Package

Golang version 1.19

Artziel Narvaiza artziel@gmail.com

Features
  • Simplify Excel Layouts reading
  • Validate read row data
Dependencies
  • github.com/xuri/excelize/v2

Get the package

go get github.com/artziel/go-layouts

Use example:

package main

import (
	"fmt"

	DataLayouts "github.com/artziel/go-layouts"
)

type MySampleRow struct {
	DataLayouts.Row
	ID       int    `excelLayout:"column:A,required,min:1,unique"`
	Username string `excelLayout:"column:B,required,minLength:6"`
	Password string `excelLayout:"column:C,required,minLength:8"`
	Avatar   string `excelLayout:"column:D,url"`
	Fullname string `excelLayout:"column:E,required"`
	Email    string `excelLayout:"column:F,required,email"`
	Age      int    `excelLayout:"column:G,required,min:18,max:50"`
}

func main() {

	fmt.Println("Sample....")

	l := DataLayouts.ExcelLayout{}

	err := l.ReadFile(MySampleRow{}, "./sample.xlsx")
	if err != nil {
		for _, e := range l.GetErrors() {
			fmt.Printf("Error >> Cell[%v:%v] %s\n", e.Column, e.RowIndex, e.Error.Error())
		}
	} else {
		rows := l.GetRows()
		for i, r := range rows {
			row := r.(*MySampleRow)
			fmt.Printf("%d) ID:%v, Username: %v\n", i, row.ID, row.Username)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCommaSeparatedInvalid error = errors.New("invalid comma separated expected value")
View Source
var ErrDateFormatInvalid error = errors.New("invalid or unexpected datetime format")
View Source
var ErrDecimalInvalid error = errors.New("invalid integer value")
View Source
var ErrEmailValueRuleFail error = errors.New("email value rule validation fail")
View Source
var ErrExcelNoSheetFound error = errors.New("no sheet found on file")

ExcelLayout Errors

View Source
var ErrExcelValidationFail error = errors.New("file rows validation fail")
View Source
var ErrIntegerInvalid error = errors.New("invalid integer value")
View Source
var ErrMaxLengthValueRuleFail error = errors.New("max length rule fail")
View Source
var ErrMaxValueRuleFail error = errors.New("max value rule fail")
View Source
var ErrMinLengthValueRuleFail error = errors.New("min length rule fail")
View Source
var ErrMinValueRuleFail error = errors.New("min value rule fail")
View Source
var ErrNotUnique error = errors.New("value is not unique")
View Source
var ErrRegexInvalid error = errors.New("invalid regex value")
View Source
var ErrRegexRuleFail error = errors.New("regex matching rule fail")
View Source
var ErrRequiredValueRuleFail error = errors.New("value required rule fail")
View Source
var ErrTagEmptyFieldTag error = errors.New("empty \"excelLayout\" tag found")
View Source
var ErrTagInvalidMaxMinLengthValues error = errors.New("the \"maxLength\" value should be greater than \"minLength\" value tag entry")
View Source
var ErrTagInvalidMaxMinValues error = errors.New("the \"max\" value should be greater than \"min\" value tag entry")
View Source
var ErrTagMaxForbidden error = errors.New("the use of value \"max\" tag entry is not allow for strings")
View Source
var ErrTagMaxLengthForbidden error = errors.New("the use of value \"maxLength\" tag entry is not allow for numbers")
View Source
var ErrTagMinForbidden error = errors.New("the use of value \"min\" tag entry is not allow for strings")
View Source
var ErrTagMinLengthForbidden error = errors.New("the use of value \"minLength\" tag entry is not allow for numbers")
View Source
var ErrTagMissingColumnValue error = errors.New("expected value for \"column\" tag entry")
View Source
var ErrTagMissingDateFormatValue error = errors.New("expected value for \"dateformat\" tag entry")
View Source
var ErrTagMissingMaxLengthValue error = errors.New("expected value for \"maxLength\" tag entry")
View Source
var ErrTagMissingMaxValue error = errors.New("expected value for \"max\" tag entry")
View Source
var ErrTagMissingMinLengthValue error = errors.New("expected value for \"minLength\" tag entry")
View Source
var ErrTagMissingMinValue error = errors.New("expected value for \"min\" tag entry")
View Source
var ErrTagMissingRegexValue error = errors.New("expected value for \"regex\" tag entry")
View Source
var ErrTagNoFieldTag error = errors.New("no \"excelLayout\" tag found")

Parser Errors

View Source
var ErrUrlValueRuleFail error = errors.New("url value rule validation fail")

Functions

This section is empty.

Types

type Error

type Error struct {
	RowIndex int
	Error    error
	Column   string
	Value    string
}

type ExcelLayout

type ExcelLayout struct {
	Layout
	// contains filtered or unexported fields
}

func (*ExcelLayout) GetFilePath

func (l *ExcelLayout) GetFilePath() string

func (*ExcelLayout) ParseCells

func (l *ExcelLayout) ParseCells(r interface{}, cells []string) []Error

func (*ExcelLayout) ParseStruct

func (l *ExcelLayout) ParseStruct(r interface{}) []Error

func (*ExcelLayout) ReadFile

func (l *ExcelLayout) ReadFile(rowType interface{}, filePath string) error

type FiledTagsParserTests

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

*

  • Field Tag Parser Test struct

type Layout

type Layout struct {
	Rows interface{}

	IgnoreEmpty bool
	// contains filtered or unexported fields
}

func NewLayout

func NewLayout(t interface{}) Layout

func (*Layout) AddError

func (l *Layout) AddError(err Error)

func (*Layout) AddErrors added in v1.0.6

func (l *Layout) AddErrors(errs []Error)

func (*Layout) AddRow

func (l *Layout) AddRow(r interface{}) error

func (*Layout) CountRows

func (l *Layout) CountRows() int

func (*Layout) GetErrors

func (l *Layout) GetErrors() []Error

func (*Layout) GetRows

func (l *Layout) GetRows() []interface{}

func (*Layout) HasErrors

func (l *Layout) HasErrors() bool

func (*Layout) Iterate

func (l *Layout) Iterate(fnc func(i int, r interface{}) error) error

type Row

type Row struct {
	Index int
}

type RowParserTests

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

*

  • Row Parser Test struct

func (*RowParserTests) IsErrorExpected

func (rt *RowParserTests) IsErrorExpected(e error) bool

*

  • Check if an error is in expected errors list

type StructParserTests

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

func (*StructParserTests) IsErrorExpected

func (rt *StructParserTests) IsErrorExpected(e error) bool

*

  • Check if an error is in expected errors list

type TestRow

type TestRow struct {
	Row
	ID       int    `excelLayout:"column:A,required,min:1,unique"`
	Username string `excelLayout:"column:B,required,minLength:6"`
	Password string `excelLayout:"column:C,required,minLength:8"`
	Avatar   string `excelLayout:"column:D,url"`
	Fullname string `excelLayout:"column:E,required,maxLength:25"`
	Email    string `excelLayout:"column:F,required,email"`
	Age      int    `excelLayout:"column:G,required,min:18,max:50"`
	Key      string `excelLayout:"column:H,required,regex:p([a-z]+)ch"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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