Mercurial > gemma
changeset 4030:040a5dc95eb9
AGM import: Moved parsing of CVS header line to separate function.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 23 Jul 2019 10:36:56 +0200 |
parents | b17453420eff |
children | 4bf1c8d91bac |
files | pkg/imports/agm.go |
diffstat | 1 files changed, 46 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/imports/agm.go Mon Jul 22 19:19:00 2019 +0200 +++ b/pkg/imports/agm.go Tue Jul 23 10:36:56 2019 +0200 @@ -213,6 +213,47 @@ var errContinue = errors.New("continue") +func parseAGMHeaders(headers []string, fkGaugeIDIdx, measureDateIdx, valueIdx *int) error { + + headerFields := []struct { + idx *int + name string + }{ + {fkGaugeIDIdx, "fk_gauge_id"}, + {measureDateIdx, "measure_date"}, + {valueIdx, "value"}, // "water_level", + } + +nextHeader: + for i, f := range headers { + h := strings.Replace(strings.ToLower( + strings.TrimSpace(f)), " ", "_", -1) + + for j := range headerFields { + if headerFields[j].name == h { + if *headerFields[j].idx != -1 { + return fmt.Errorf( + "There is more than one column namend '%s'", h) + } + *headerFields[j].idx = i + continue nextHeader + } + } + } + + var missing []string + for i := range headerFields { + if headerFields[i].name != "unit" && *headerFields[i].idx == -1 { + missing = append(missing, headerFields[i].name) + } + } + if len(missing) > 0 { + return fmt.Errorf("Missing columns: %s", strings.Join(missing, ", ")) + } + + return nil +} + // Do executes the actual approved gauge measurements import. func (agm *ApprovedGaugeMeasurements) Do( ctx context.Context, @@ -244,40 +285,11 @@ valueIdx = -1 ) - headerFields := []struct { - idx *int - name string - }{ - {&fkGaugeIDIdx, "fk_gauge_id"}, - {&measureDateIdx, "measure_date"}, - {&valueIdx, "value"}, // "water_level", - } - -nextHeader: - for i, f := range headers { - h := strings.Replace(strings.ToLower( - strings.TrimSpace(f)), " ", "_", -1) - - for j := range headerFields { - if headerFields[j].name == h { - if *headerFields[j].idx != -1 { - return nil, fmt.Errorf( - "There is more than one column namend '%s'", h) - } - *headerFields[j].idx = i - continue nextHeader - } - } - } - - var missing []string - for i := range headerFields { - if headerFields[i].name != "unit" && *headerFields[i].idx == -1 { - missing = append(missing, headerFields[i].name) - } - } - if len(missing) > 0 { - return nil, fmt.Errorf("Missing columns: %s", strings.Join(missing, ", ")) + if err := parseAGMHeaders( + headers, + &fkGaugeIDIdx, &measureDateIdx, &valueIdx, + ); err != nil { + return nil, err } gaugeCheckStmt, err := conn.PrepareContext(ctx, agmGaugeCheckSQL)