xlsToJson

package module
v0.0.0-...-77b3a6e Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

README

Excel2JsonTree

Excel2JsonTree is basically written to easily convert the excel data to json object with dynamic struct binding. It also solves the main problem of converting the excel to nested json object by simply passing struct.

Features:
  1. Converts excel data into simple json object.
  2. Converts excel data into json with nested object.
  3. Dynamically typecast Excel cell value to type of struct key. Converts excel data into int, string, float, boolean, slice, struct, slice of struct.
  4. Excel cell value can be directly assign to cell or cell value can be processed and assign to cell.
  5. Allows to add custome field values to the struct other than excel fields such as timestamp etc.
Installation:
go get github.com/onkarvhanumante/Excel2JsonTree
Basic Usage:

(Excel and program could be found in example folder).

excel file:

Excel file

struct:

type MenuInfo struct {
	ParentName  string     `json:"parentName"`
	DisplayName string     `json:"displayName" excel:"level"`
	MenuType    string     `json:"menuType" excel:"menuType"`
	Menu        string     `json:"menu" excel:"menu"`
	Ordinal     string     `json:"ordinal" excel:"ordinal"`
	ValidTill   time.Time  `json:"validTill" excel:"ValidTill"`
	RoleList    []string   `json:"roleList" excel:"RoleList"`
	HasChild    bool       `json:"hasChild"`
	MenuList    []MenuInfo `json:"menuList"`
	Level       int        `json:"level"`
}

define struct with excel tag to map struct key excel column name

Step1 : Get variable holder Pointer:

variableHolderObjPtr := xlsToJson.GetVariableHolderPtr()

variableHolderObjPtr can be used to invoke package built in functions.

Step2 : Set name of column(s) to be refered for generating json:

variableHolderObjPtr.SetNodesColumnNameMap([]string{"level"})

SetNodesColumnNameMap accepts the list of columns name that specifies the level of nodes in excel. In above screenshot level is column name that specifies the level of nodes.

Step3: Map the struct key with functions that would process corresponding excel cell value:

This steps is optional may depend on what to be implemented.

SetKeyFunctionMap accepts the struct key and function of type UserDefinedFunction as argument.

SetInputParametersToUserDefinedFunc can be used to set input that to UserDefinedFunction function.

IdDatabase := make(map[string]string)
IdDatabase["1"] = "user1"
IdDatabase["2"] = "user2"
variableHolderObjPtr.SetInputParametersToUserDefinedFunc("IdDatabase", IdDatabase)
	
variableHolderObjPtr.SetKeyFunctionMap("ValidTill", generateValidTillAttributeValue)
variableHolderObjPtr.SetKeyFunctionMap("ParentName", assignParentName)
variableHolderObjPtr.SetKeyFunctionMap("HasChild", assignCurrentNodeChildStatus)
variableHolderObjPtr.SetKeyFunctionMap("CreatedBy", assignCreatedBy)
variableHolderObjPtr.SetKeyFunctionMap("OptionLevel", assignOptionLevel)

Defining UserDefinedFunction function:

var assignCreatedBy xlsToJson.UserDefinedFunction = func(inputMap map[string]interface{}, excelCellValue string, v *xlsToJson.VariableHolder) (interface{}, error) {
	var output interface{}
	idDb := inputMap["IdDatabase"].(map[string]string)
	val, ok := idDb[excelCellValue]
	if ok {
		output = val
	} else {
		return nil, errors.New("Id not present in db")
	}
	return output, nil
}

assignCreatedBy function receives 3 argument:

arg1: input set using SetInputParametersToUserDefinedFunc function.

arg2: excel cell value to be processed.

arg3: variable holder instance to call built in public methods - GetCurrentNodeLevel(), GetParentNodeForChild(), GetCurrentNode(), GetCurrentNodeChildStatus() etc.

Step4: Assign excel data to struct:

Val, _ := variableHolderObjPtr.ConvertXslToJSON(excelFile.Sheets[0], MenuInfo{}, "MenuList")

ConvertXslToJSON returns the Val which can asserted to MenuInfo further.

ConvertXslToJSON accepts the three arguments: arg1: excel sheet. arg2: struct to which data need to be assigned. arg3: struct key in which children nodes would be appended.

output:

[
    {
        "parentName": "NA",
        "displayName": "File",
        "menuDescription": "",
        "ordinal": "1",
        "validTill": "2019-02-08T00:00:00Z",
        "roleList": [
            "admin",
            "user"
        ],
        "hasChild": true,
        "menuList": [
            {
                "parentName": "File",
                "displayName": "New",
                "menuDescription": "New option",
                "ordinal": "1",
                "validTill": "2019-02-08T00:00:00Z",
                "roleList": [
                    "user"
                ],
                "hasChild": false,
                "menuList": null,
                "optionLevel": 1,
                "createdBy": "user2"
            },
            {
                "parentName": "File",
                "displayName": "Open",
                "menuDescription": "Open option",
                "ordinal": "2",
                "validTill": "2019-06-08T00:00:00Z",
                "roleList": [
                    "admin"
                ],
                "hasChild": false,
                "menuList": null,
                "optionLevel": 1,
                "createdBy": "user1"
            },
            {
                "parentName": "File",
                "displayName": "save",
                "menuDescription": "Save option",
                "ordinal": "3",
                "validTill": "2019-06-08T00:00:00Z",
                "roleList": [
                    "user"
                ],
                "hasChild": false,
                "menuList": null,
                "optionLevel": 1,
                "createdBy": "user2"
            },
            {
                "parentName": "File",
                "displayName": "save as",
                "menuDescription": "Save as option",
                "ordinal": "4",
                "validTill": "2019-06-08T00:00:00Z",
                "roleList": [
                    "user"
                ],
                "hasChild": true,
                "menuList": [
                    {
                        "parentName": "save as",
                        "displayName": "docs",
                        "menuDescription": "Save as doc",
                        "ordinal": "1",
                        "validTill": "2019-06-08T00:00:00Z",
                        "roleList": [
                            "user"
                        ],
                        "hasChild": false,
                        "menuList": null,
                        "optionLevel": 2,
                        "createdBy": "user2"
                    },
                    {
                        "parentName": "save as",
                        "displayName": "pdf",
                        "menuDescription": "Save as pdf",
                        "ordinal": "2",
                        "validTill": "2019-06-08T00:00:00Z",
                        "roleList": [
                            "user"
                        ],
                        "hasChild": false,
                        "menuList": null,
                        "optionLevel": 2,
                        "createdBy": "user1"
                    }
                ],
                "optionLevel": 1,
                "createdBy": "user1"
            },
            {
                "parentName": "File",
                "displayName": "exit",
                "menuDescription": "exit",
                "ordinal": "5",
                "validTill": "2019-06-08T00:00:00Z",
                "roleList": [
                    "user"
                ],
                "hasChild": false,
                "menuList": null,
                "optionLevel": 1,
                "createdBy": "user2"
            }
        ],
        "optionLevel": 0,
        "createdBy": "user1"
    }
]
Built-in functions:
  1. GetVariableHolderPtr() :

    Returns the VariableHolder instance pointer that can be used to Invoke built in package function.

  2. SetNodesColumnNameMap(list []string):

    Accepts the list of columns name that specifies the level of nodes in excel.

  3. SetAttributeMappingMap(structXslMap map[string]string):

    Accepts map that maps the excel header row with struct key.

  4. SetKeyFunctionMap(structKey string, functionVariable UserDefinedFunction): Maps the struct key with the function of type UserDefinedFunction

  5. SetInputParametersToUserDefinedFunc(key string, value interface{}):

    Used to set input that can be accessed in UserDefinedFunction.

  6. ConvertXslToJSON(sheet *xlsx.Sheet, structReference interface{}, childrenArrayKey string):

    Assigns excel data to struct. Accepts three arguments I.e sheet, structReference (struct in which excel data to be attached) and childrenArrayKey (struct key in which children nodes would be appended).

  7. SetListStringSeperator(seperator string):

    Sets the separator used to split the string while converting cell data to slice. By default assumes comma(,) as separator.

  8. SetExcelHeaderRowIndexNo(indexNo int):

    Sets the row index to be referred for accessing excel header row. By default assumes 0 as index forexcel header row.

  9. GetParentNodeForChild():

    Returns the parent node for current child node.

  10. GetCurrentNodeLevel():

    Return current node level.

  11. GetCurrentNode():

    Return current node.

  12. GetCurrentNodeChildrenStatus():

    Return status whether current node has child or not.

Contributors:

Onkar Hanumante : www.linkedin.com/in/onkar-hanumante

Suvarna Rokade : https://www.linkedin.com/in/suvarna-rokade-b469aa125/

Documentation

Index

Constants

View Source
const (
	ERR_CODE_EXCEL_NOT_OPEN                                                 = "EXCEL_CANNOT_BE_OPENED"
	ERR_CODE_EXCEL_SHEET_EMPTY                                              = "EXCEL_SHEET_EMPTY"
	ERR_CODE_SHEET_HEADER_ROW_EMPTY                                         = "EXCEL_SHEET_HEADER_ROW_EMPTY"
	ERR_CODE_MAP_RECEIVED_EMPTY                                             = "EMPTY_STRUCT_XSL_MAP_RECEIVED"
	ERR_CODE_NODE_COLUMN_NAME_NOT_PRESENT_IN_MAP                            = "NODE_COLUMN_NAME_NOT_PRESENT_IN_MAP"
	ERR_CODE_HEADER_EMPTY                                                   = "EXCEL_HEADER_EMPTY"
	ERR_CODE_UNABLE_TO_SET_STRUCT_INFO                                      = "UNABLE_TO_SET_STRUCT_INFO"
	ERR_CODE_INCORRECT_NUMBER_OF_ATTRIBUTE_SET                              = "NUMBER_OF_KEYS_IN_STRUCT_AND_SetAttributeMappingMap_MISMATCHED"
	ERR_CODE_CHILDREN_ARRAY_KEY_NOT_SET                                     = "CHILDREN_ARRAY_KEY_NOT_SET_IN_SetKeyToAppendChildrenNodes"
	ERR_CODE_KEYS_COUNT_MISMATCHED_IN_INPUT_SEND_TO_SETATTRIBUTEMAPPINGMAP  = "HEADER_ROW_COLUMN_AND_MAP_KEYS_COUNT_MISMATCHED_IN_INPUT_SEND_TO_SetAttributeMappingMap"
	ERR_CODE_KEYS_MISMATCHED_IN_ATTRIBUTEMAPPINGMAP                         = "KEYS_MISMATCHED_IN_INPUT_SEND_TO_SetAttributeMappingMap"
	ERR_CODE_KEYS_MISMATCHED_IN_STRUCTKEYVALUEMAP                           = "KEYS_MISMATCHED_IN_INPUT_SEND_TO_SetAttributeMappingMap_AND_struct"
	ERR_CODE_INTERFACE_RECEIVED_IS_NOT_STRUCT                               = "INTERFACE_RECEIVED_IS_NOT_STRUCT"
	ERR_CODE_DATE_FORMAT_PARSING_ERROR                                      = "DATE_FORMAT_PARSING_ERROR"
	ERR_CODE_USERDEFINED_FUNCTION_RETURN_VALUE_AND_STRUCT_KEY_TYPE_MISMATCH = "USERDEFINED_FUNCTION_RETURN_VALUE_AND_STRUCT_KEY_TYPE_MISMATCH"
	ERR_CODE_KEY_NOT_PRESENT_IN_STRUCT                                      = "KEY_NOT_PRESENT_IN_EXCEL"
	ERR_CODE_EMPTY_LIST_RECEIVED                                            = "EMPTY_LIST_RECEIVED"
)

Defined Error constant

View Source
const (

	//EMPTY_STR - empty string
	EMPTY_STR = ""

	// DEFAULT_LIST_STRING_SEPERATOR - default seperator
	DEFAULT_LIST_STRING_SEPERATOR = ","

	//DEFAULT_DATE_FORMAT - default layout to parse time value
	DEFAULT_DATE_FORMAT_LAYOUT = "01-02-2006"

	//DEFAULT_HEADER_ROW_INDEX	- default index of header row
	DEFAULT_HEADER_ROW_INDEX = 0

	//OPEN_CLOSE_SQUARE_BRACKET - constant for reflect type squrare bracket opening and closing square bracket
	OPEN_CLOSE_SQUARE_BRACKET = "[]"

	//STRING - constant for reflect type string
	STRING = "string"

	//INT - constant for reflect type int
	INT = "int"

	//INT8 - constant for reflect type int8
	INT8 = "int8"

	//INT32 - constant for reflect type int32
	INT32 = "int32"

	//INT64 - constant for reflect type int64
	INT64 = "int64"

	//FLOAT32 - constant for reflect type float32
	FLOAT32 = "float32"

	//FLOAT64 - constant for reflect type float64
	FLOAT64 = "float64"

	//BOOLEAN - constant for reflect type bool
	BOOLEAN = "bool"

	//STRUCT - constant for reflect Kind bool
	STRUCT = "struct"

	//SLICE - constant for reflect Kind slice
	SLICE = "slice"

	// TIME_DOT_TIME - constanst for reflect type time.time
	TIME_DOT_TIME = "time.Time"

	//INT_SLICE - constant for reflect type slice of int ([]int)
	INT_SLICE = OPEN_CLOSE_SQUARE_BRACKET + INT

	//INT8_SLICE - constant for reflect type slice of int8 ([]int8)
	INT8_SLICE = OPEN_CLOSE_SQUARE_BRACKET + INT8

	//INT32_SLICE - constant for reflect type slice of int32 ([]int32)
	INT32_SLICE = OPEN_CLOSE_SQUARE_BRACKET + INT32

	//INT64_SLICE - constant for reflect type slice of int64 ([]int64)
	INT64_SLICE = OPEN_CLOSE_SQUARE_BRACKET + INT64

	//FLOAT_32SLICE - constant for reflect type slice of float32 ([]float32)
	FLOAT32_SLICE = OPEN_CLOSE_SQUARE_BRACKET + FLOAT32

	//INT64_SLICE - constant for reflect type slice of float64 ([]float64)
	FLOAT64_SLICE = OPEN_CLOSE_SQUARE_BRACKET + FLOAT64

	//STRING_SLICE - constant for reflect type slice of string ([]string)
	STRING_SLICE = OPEN_CLOSE_SQUARE_BRACKET + STRING

	//BOOLEAN_SLICE - constant for reflect type slice of bool ([]bool)
	BOOLEAN_SLICE = OPEN_CLOSE_SQUARE_BRACKET + BOOLEAN

	// NODE FLAGS - indicate level of flag
	APPEND_NODE_AT_START_LEVEL    = "APPEND_NODE_AT_START_LEVEL"
	APPEND_NODE_AT_NEXT_LEVEL     = "APPEND_NODE_AT_NEXT_LEVEL"
	APPEND_NODE_AT_PREVIOUS_LEVEL = "APPEND_NODE_AT_PREVIOUS_LEVEL"
	APPEND_NODE_AT_SAME_LEVEL     = "APPEND_NODE_AT_SAME_LEVEL"

	// EXCEL_TAG - tag to be used while mapping excel column key with struct key
	EXCEL_TAG = "excel"
)

Variables

This section is empty.

Functions

func ReadExcel

func ReadExcel(excelPath string) (*xlsx.File, error)

ReadExcel - reads excel file

Types

type UserDefinedFunction

type UserDefinedFunction func(map[string]interface{}, string, *VariableHolder) (interface{}, error)

UserDefinedFunction - user defined functionality type

type VariableHolder

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

VariableHolder - stores variables required

func GetVariableHolderPtr

func GetVariableHolderPtr() *VariableHolder

GetVariableHolderPtr - returns the VariableHolder instance

func (*VariableHolder) ConvertXslToJSON

func (v *VariableHolder) ConvertXslToJSON(sheet *xlsx.Sheet, structReference interface{}, childrenArrayKey string) (interface{}, error)

ConvertXslToJSON - converts the data from sheet to JSON

func (*VariableHolder) GetCurrentNode

func (v *VariableHolder) GetCurrentNode() interface{}

GetCurrentNode - returns the current node

func (*VariableHolder) GetCurrentNodeChildrenStatus

func (v *VariableHolder) GetCurrentNodeChildrenStatus() bool

GetCurrentNodeChildrenStatus - return true if current node has child

func (*VariableHolder) GetCurrentNodeLevel

func (v *VariableHolder) GetCurrentNodeLevel() int

GetCurrentNodeLevel - returns the current node level

func (*VariableHolder) GetParentNodeForChild

func (v *VariableHolder) GetParentNodeForChild() interface{}

GetParentNodeForChild - returns the parent for current node

func (*VariableHolder) SetExcelHeaderRowIndexNo

func (v *VariableHolder) SetExcelHeaderRowIndexNo(indexNo int)

SetExcelHeaderRowIndexNo - index no of excel header row

func (*VariableHolder) SetInputParametersToUserDefinedFunc

func (v *VariableHolder) SetInputParametersToUserDefinedFunc(key string, value interface{})

SetInputParametersToUserDefinedFunc - sets the input in methodsInputParameterMap that can be used in user defined function

func (*VariableHolder) SetKeyFunctionMap

func (v *VariableHolder) SetKeyFunctionMap(structKey string, bindingFunction UserDefinedFunction)

SetKeyFunctionMap - sets the function to be executed on particular key

func (*VariableHolder) SetListStringSeperator

func (v *VariableHolder) SetListStringSeperator(seperator string)

SetListStringSeperator - used to assgin the seperator to split the string

func (*VariableHolder) SetNodesColumnNameMap

func (v *VariableHolder) SetNodesColumnNameMap(nodeNameColumnList []string) error

SetNodesColumnNameMap - sets the name of column to be searched sends error if map received is empty

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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