From 7bd66d6dab56ebf32f9ef5ecaeb8fc5f56d39fb6 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 13 Dec 2017 18:39:42 +0900 Subject: [PATCH] ing --- modules/config/model/ResultSet.go | 46 ++++++++++++ modules/config/model/ResultSetCol.go | 85 ++++++++++++++++++++++ modules/config/model/ResultSetRow.go | 105 +++++++++++++++++++++++++++ 3 files changed, 236 insertions(+) create mode 100644 modules/config/model/ResultSet.go create mode 100644 modules/config/model/ResultSetCol.go create mode 100644 modules/config/model/ResultSetRow.go diff --git a/modules/config/model/ResultSet.go b/modules/config/model/ResultSet.go new file mode 100644 index 0000000..f5da3ee --- /dev/null +++ b/modules/config/model/ResultSet.go @@ -0,0 +1,46 @@ +package model + +import "encoding/json" + +type ResultSetter interface { + AddRow(row []string) + GetMeta() map[string]json.Number + GetData() map[string]string +} + +type ResultSet struct { + Item *Item `json:"item,omitempty"` + Rows [][]string `json:"rows,omitempty"` + Meta map[string]json.Number `json:"meta,omitempty"` +} + +func (rs *ResultSet) AddRow(row []string) { + rs.Rows = append(rs.Rows, row) +} + +func (rs *ResultSet) GetMeta() map[string]json.Number { + return rs.Meta +} + +func NewResultSet(item *Item) ResultSetter { + if nil == item.MappingInfo { + item.MappingInfo = &MappingInfo{} + } + rs := &ResultSet{} + rs.Item = item + rs.Rows = make([][]string, 0) + + switch item.MappingInfo.ParseDirection { + case "row": + rsr := &ResultSetRow{} + rsr.ResultSet = rs + rsr.setMeta() + return rsr + default: + rsc := &ResultSetCol{} + rsc.ResultSet = rs + rsc.setMeta() + return rsc + } + +} diff --git a/modules/config/model/ResultSetCol.go b/modules/config/model/ResultSetCol.go new file mode 100644 index 0000000..2570698 --- /dev/null +++ b/modules/config/model/ResultSetCol.go @@ -0,0 +1,85 @@ +package model + +import ( + "encoding/json" + "fmt" + "strconv" + "strings" + + cuej "git.loafle.net/commons_go/util/encoding/json" +) + +type ResultSetCol struct { + *ResultSet +} + +func (rsc *ResultSetCol) setMeta() { + meta := rsc.Item.Keys + arrayColumns := rsc.Item.MappingInfo.ArrayColumns + + if nil == rsc.Meta { + rsc.Meta = make(map[string]json.Number, 0) + } + + for i := 0; i < len(meta); i++ { + rsc.Meta[meta[i].Key] = json.Number(strconv.Itoa(i)) + } + + if nil != arrayColumns { + for i := 0; i < len(arrayColumns); i++ { + if _, ok := rsc.Meta[arrayColumns[i]]; ok { + continue + } + rsc.Meta[arrayColumns[i]] = json.Number(strconv.Itoa(i + len(meta))) + } + } +} + +func (rsc *ResultSetCol) GetData() map[string]string { + return rsc.parse() +} + +func (rsc *ResultSetCol) parse() map[string]string { + metrics := rsc.Item.Keys + arrayColumns := rsc.Item.MappingInfo.ArrayColumns + + rm := make(map[string]string, 0) + + for i := 0; i < len(rsc.Rows); i++ { + row := rsc.Rows[i] + for j := 0; j < len(row); j++ { + arrayValue := make([]string, 0) + + if nil != arrayColumns { + for k := 0; k < len(arrayColumns); k++ { + idxN := rsc.Meta[arrayColumns[k]] + idx, _ := cuej.NumberToInt(idxN) + arrayValue = append(arrayValue, row[idx]) + } + } + + for k := 0; k < len(metrics); k++ { + metric := metrics[k].Metric + metric = rsc.convertMetric(metric, arrayValue) + rm[metric] = row[k] + } + + } + } + + return rm +} + +func (rsc *ResultSetCol) convertMetric(metric string, arrayValue []string) string { + if nil == arrayValue || 0 == len(arrayValue) { + return metric + } + + convertChar := "$" + + for i := 0; i < len(arrayValue); i++ { + convertStr := fmt.Sprintf("%s%d", convertChar, i) + metric = strings.Replace(metric, convertStr, arrayValue[i], -1) + } + return metric +} diff --git a/modules/config/model/ResultSetRow.go b/modules/config/model/ResultSetRow.go new file mode 100644 index 0000000..9043077 --- /dev/null +++ b/modules/config/model/ResultSetRow.go @@ -0,0 +1,105 @@ +package model + +import ( + "encoding/json" + "fmt" + "strconv" + "strings" + + cuej "git.loafle.net/commons_go/util/encoding/json" +) + +type ResultSetRow struct { + *ResultSet +} + +func (rsr *ResultSetRow) setMeta() { + meta := make([]string, 0) + arrayColumns := rsr.Item.MappingInfo.ArrayColumns + keyColumns := rsr.Item.MappingInfo.KeyColumns + valueColumn := rsr.Item.MappingInfo.ValueColumn + + if nil != arrayColumns { + for _, c := range arrayColumns { + meta = append(meta, c) + } + } + + if nil != keyColumns { + for _, c := range keyColumns { + meta = append(meta, c) + } + } + + if "" != valueColumn { + meta = append(meta, valueColumn) + } + + if nil == rsr.Meta { + rsr.Meta = make(map[string]json.Number, 0) + } + + for i := 0; i < len(meta); i++ { + rsr.Meta[meta[i]] = json.Number(strconv.Itoa(i)) + } +} + +func (rsr *ResultSetRow) GetData() map[string]string { + return rsr.parse() +} + +func (rsr *ResultSetRow) parse() map[string]string { + valueColumn := rsr.Item.MappingInfo.ValueColumn + + rm := make(map[string]string, 0) + + for _, row := range rsr.Rows { + key := rsr.makeKey(row) + if "" == key { + continue + } + idx, _ := cuej.NumberToInt(rsr.Meta[valueColumn]) + rm[key] = row[idx] + } + + return rm +} + +func (rsr *ResultSetRow) makeKey(data []string) string { + metrics := rsr.Item.Keys + arrayColumns := rsr.Item.MappingInfo.ArrayColumns + keyColumns := rsr.Item.MappingInfo.KeyColumns + keys := rsr.Item.Keys + + findIndex := -1 + +Loop: + for _, keyColumn := range keyColumns { + idx, _ := cuej.NumberToInt(rsr.Meta[keyColumn]) + row := data[idx] + for i := 0; i < len(keys); i++ { + if row == keys[i].Key { + findIndex = i + break Loop + } + } + } + + if 0 > findIndex { + return "" + } + + metric := metrics[findIndex].Metric + + convertChar := "$" + + if nil != arrayColumns { + for i := 0; i < len(arrayColumns); i++ { + convertStr := fmt.Sprintf("%s%d", convertChar, i) + idx, _ := cuej.NumberToInt(rsr.Meta[arrayColumns[i]]) + replaceString := data[idx] + metric = strings.Replace(metric, convertStr, "'"+replaceString+"'", -1) + } + } + return metric +}