From ba076ed5cc8d158028b4ea3b3ec1e029b92e9126 Mon Sep 17 00:00:00 2001 From: Jo Asplin Date: Mon, 18 Dec 2023 13:22:42 +0100 Subject: [PATCH] Used reflection to avoid explicit field references --- .../postgresql/getobservations.go | 3 ++ .../storagebackend/postgresql/postgresql.go | 37 ++++--------------- .../postgresql/putobservations.go | 2 +- 3 files changed, 12 insertions(+), 30 deletions(-) diff --git a/datastore/storagebackend/postgresql/getobservations.go b/datastore/storagebackend/postgresql/getobservations.go index 1fcde71..2357379 100644 --- a/datastore/storagebackend/postgresql/getobservations.go +++ b/datastore/storagebackend/postgresql/getobservations.go @@ -17,6 +17,7 @@ import ( var ( tsStringMdataGoNames []string // Go names for time series metadata of type string + tsStringMdataPBNames []string // protobuf names for time series metadata of type string obspb2go map[string]string // association between observation specific protobuf name and // (generated by protoc) Go name. @@ -33,10 +34,12 @@ var ( func init() { tsStringMdataGoNames = []string{} + tsStringMdataPBNames = []string{} for _, field := range reflect.VisibleFields(reflect.TypeOf(datastore.TSMetadata{})) { if field.IsExported() && (field.Type.Kind() == reflect.String) { goName := field.Name tsStringMdataGoNames = append(tsStringMdataGoNames, goName) + tsStringMdataPBNames = append(tsStringMdataPBNames, common.ToSnakeCase(goName)) } } diff --git a/datastore/storagebackend/postgresql/postgresql.go b/datastore/storagebackend/postgresql/postgresql.go index 78f1049..271abc5 100644 --- a/datastore/storagebackend/postgresql/postgresql.go +++ b/datastore/storagebackend/postgresql/postgresql.go @@ -92,41 +92,20 @@ func NewPostgreSQL() (*PostgreSQL, error) { // getTSMdataCols returns time series metadata column names. func getTSMdataCols() []string { - // ### TODO: modify to use reflection instead of explicit field referrals - return []string{ - // --- BEGIN non-string metadata --------------------------- + + // initialize cols with non-string metadata + cols := []string{ "link_href", "link_rel", "link_type", "link_hreflang", "link_title", - // --- END non-string metadata --------------------------- - - // --- BEGIN string metadata --------------------------- - "version", - "type", - "title", - "summary", - "keywords", - "keywords_vocabulary", - "license", - "conventions", - "naming_authority", - "creator_type", - "creator_name", - "creator_email", - "creator_url", - "institution", - "project", - "source", - "platform", - "platform_vocabulary", - "standard_name", - "unit", - "instrument", - "instrument_vocabulary", - // --- END string metadata --------------------------- } + + // complete cols with string metadata + cols = append(cols, tsStringMdataPBNames...) + + return cols } // createPlaceholders returns the list of n placeholder strings for diff --git a/datastore/storagebackend/postgresql/putobservations.go b/datastore/storagebackend/postgresql/putobservations.go index 8225a11..2ff5d31 100644 --- a/datastore/storagebackend/postgresql/putobservations.go +++ b/datastore/storagebackend/postgresql/putobservations.go @@ -81,7 +81,7 @@ func getTSColVals(tsMdata *datastore.TSMetadata) ([]interface{}, error) { // --- BEGIN string metadata --------------------------- - // ### TODO: modify to use reflection instead of explicit field referrals + // ### TODO: modify to use reflection instead of explicit field referencing colVals = append(colVals, []interface{}{ tsMdata.GetVersion(),