Пример #1
0
func xlsInsertValue(tree *XlsObjNode, keys []string, value *xlsx.Cell, offset int) {

	if tree.nodeType == ARRAY_TYPE {
		var valueList = tree.value.([]*XlsObjNode)
		var valueLen = len(valueList)
		var node *XlsObjNode

		// fmt.Println(keys[offset])
		// for _, child := range(tree.value.([]XlsObjNode)){
		// 	if findKey(tre)
		// }
		// needNew := findKey(tree, keys, offset)
		// dfs(tree)

		if valueLen == 0 {
			node = new(XlsObjNode)
			if keys[offset] == "id" {
				node.key = keys[offset]
			}
			node.nodeType = HASH_TYPE
			node.value = make([]*XlsObjNode, 0)
			tree.value = append(valueList, node)
		} else {
			node = valueList[valueLen-1]
		}

		needNew := xlsFindKey(node, keys, offset)

		if needNew {
			node = new(XlsObjNode)
			node.nodeType = HASH_TYPE
			node.value = make([]*XlsObjNode, 0)
			tree.value = append(valueList, node)
		}
		xlsInsertValue(node, keys, value, offset)
		return
	}

	if len(keys) == offset+1 {
		var child = new(XlsObjNode)

		if strings.HasSuffix(keys[offset], "*") {
			child.key = ""
			child.nodeType = VALUE_TYPE
			child.value = value.String()

			var valueList = tree.value.([]*XlsObjNode)
			for _, key := range valueList {
				key2 := keys[offset]
				if key2[:(len(key2)-1)] == key.key {
					// xlsInsertValue(key, keys, value, offset+1)
					key.value = append(key.value.([]*XlsObjNode), child)
					return
				}
			}

			var newXlsObjNode = new(XlsObjNode)
			tree.value = append(valueList, newXlsObjNode)
			newXlsObjNode.nodeType = ARRAY_TYPE
			newXlsObjNode.key = keys[offset][:len(keys[offset])-1]
			newXlsObjNode.value = make([]*XlsObjNode, 1)
			newXlsObjNode.value.([]*XlsObjNode)[0] = child
			return
		}
		child.key = keys[offset]

		if strings.HasSuffix(keys[offset], "$") { //属性类型
			child.nodeType = ATTR_TYPE
			child.key = child.key[:len(child.key)-1]
		} else {
			child.nodeType = VALUE_TYPE
		}
		child.value = value.String()
		tree.value = append(tree.value.([]*XlsObjNode), child)
		if child.key == "id" {
			tree.key = child.value.(string)
			tree.isId = true
		}

		return
	}

	var valueList = tree.value.([]*XlsObjNode)
	for _, key := range valueList {
		key2 := keys[offset]
		if key.key == keys[offset] || (strings.HasSuffix(key2, "*") && key2[:(len(key2)-1)] == key.key) {
			xlsInsertValue(key, keys, value, offset+1)
			return
		}
	}

	var newXlsObjNode = new(XlsObjNode)
	tree.value = append(valueList, newXlsObjNode)
	newXlsObjNode.key = keys[offset]
	newXlsObjNode.value = make([]*XlsObjNode, 0)

	if strings.HasSuffix(keys[offset], "*") { //数组类型
		newXlsObjNode.nodeType = ARRAY_TYPE
		newXlsObjNode.key = keys[offset][:len(keys[offset])-1]
	} else if strings.HasPrefix(keys[offset+1], "id") && len(keys) == 2 {
		newXlsObjNode.nodeType = ARRAY_TYPE
	} else {
		newXlsObjNode.nodeType = HASH_TYPE
	}

	xlsInsertValue(newXlsObjNode, keys, value, offset+1)

}