diff pkg/imports/gm.go @ 3264:9ae43313b463

Handle some possibly missing elements in NtS response The NtS XSD does not guarantee that value, value_min and value_max are present in a response. This led to circumventing the NOT NULL constraint for the waterlevel value by silently persisting missing values as zero and filling up missing confidence interval values especially for measurements with zeros.
author Tom Gottfried <tom@intevation.de>
date Wed, 15 May 2019 12:31:57 +0200
parents 4acbee65275d
children 3dee5cf16a58
line wrap: on
line diff
--- a/pkg/imports/gm.go	Wed May 15 12:04:14 2019 +0200
+++ b/pkg/imports/gm.go	Wed May 15 12:31:57 2019 +0200
@@ -188,7 +188,7 @@
 }
 
 // rescale returns a scaling function to bring the unit all to cm.
-func rescale(unit string) (func(float32) float32, error) {
+func rescale(unit string) (func(*float32), error) {
 
 	var scale float32
 
@@ -209,7 +209,11 @@
 		return nil, fmt.Errorf("unknown unit '%s'", unit)
 	}
 
-	fn := func(x float32) float32 { return scale * x }
+	fn := func(x *float32) {
+		if x != nil {
+			*x = scale * *x
+		}
+	}
 	return fn, nil
 }
 
@@ -264,6 +268,10 @@
 				if err != nil {
 					return nil, err
 				}
+				convert(measure.Value)
+				convert(measure.Value_min)
+				convert(measure.Value_max)
+
 				isWaterlevel := *measure.Measure_code == nts.Measure_code_enumWAL
 				err = insertStmt.QueryRowContext(
 					ctx,
@@ -278,11 +286,11 @@
 					msg.Identification.Country_code,
 					msg.Identification.Date_issue,
 					referenceCode,
-					convert(measure.Value),
+					measure.Value,
 					measure.Predicted,
 					isWaterlevel,
-					convert(measure.Value_min),
-					convert(measure.Value_max),
+					measure.Value_min,
+					measure.Value_max,
 					msg.Identification.Date_issue,
 					msg.Identification.Originator,
 					true, // staging_done