From 044d02c560052f1cd1cfd1cab075fb063c0ea4a1 Mon Sep 17 00:00:00 2001 From: docktermj Date: Fri, 7 Jun 2024 14:03:54 -0400 Subject: [PATCH] #122 Savepoint --- README.md | 2 +- main.go | 6 +- settings/doc.go | 2 +- settings/main.go | 2 +- settings/settings.go | 32 +-- settings/settings_darwin.go | 8 +- settings/settings_darwin_test.go | 10 +- settings/settings_examples_test.go | 36 +-- settings/settings_linux.go | 4 +- settings/settings_test.go | 46 ++-- settings/settings_windows.go | 12 +- settings/settings_windows_test.go | 18 +- settingsparser/doc.go | 2 +- settingsparser/main.go | 34 ++- ...tingsparser.go => settingsparser_basic.go} | 143 ++++++------ .../settingsparser_examples_test.go | 132 +++++++++++ settingsparser/settingsparser_test.go | 213 ++++-------------- 17 files changed, 372 insertions(+), 330 deletions(-) rename settingsparser/{settingsparser.go => settingsparser_basic.go} (70%) create mode 100644 settingsparser/settingsparser_examples_test.go diff --git a/README.md b/README.md index a0bac13..0e40447 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ The following helpers have been created: ### Helper functions -- `engineconfigurationjson`: A package for creating the `SENZING_ENGINE_CONFIGURATION_JSON` Document. +- `settings`: A package for creating the `SENZING_ENGINE_CONFIGURATION_JSON` Document. ### Test data diff --git a/main.go b/main.go index 95a49f0..b3eaaa2 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,7 @@ func main() { // --- Build JSON from environment variables. // ------------------------------------------------------------------------ - iniParams, err := settings.BuildSimpleSystemConfigurationJSONUsingEnvVars() + iniParams, err := settings.BuildSimpleSettingsUsingEnvVars() if err != nil { panic(err) } @@ -46,7 +46,7 @@ func main() { // --- Verify parameters // ------------------------------------------------------------------------ - err = settings.VerifySenzingEngineConfigurationJSON(ctx, iniParams) + err = settings.VerifySettings(ctx, iniParams) if err != nil { panic(err) } @@ -63,7 +63,7 @@ func main() { "supportPath": "/final/path/to/support", } - iniParams2, err := settings.BuildSimpleSystemConfigurationJSONUsingMap(attributeMap) + iniParams2, err := settings.BuildSimpleSettingsUsingMap(attributeMap) if err != nil { panic(err) } diff --git a/settings/doc.go b/settings/doc.go index c23d35f..f9e9b00 100644 --- a/settings/doc.go +++ b/settings/doc.go @@ -1,4 +1,4 @@ /* -The engineconfigurationjson package is used to create the JSON needed for initializing the Senzing runtime objects. +The settings package is used to create the JSON needed for initializing the Senzing runtime objects. */ package settings diff --git a/settings/main.go b/settings/main.go index ad4e247..05aa6a1 100644 --- a/settings/main.go +++ b/settings/main.go @@ -1,4 +1,4 @@ -// The engineconfigurationjson package helps configure SENZING_ENGINE_CONFIGURATION_JSON. +// The settings package helps configure SENZING_ENGINE_CONFIGURATION_JSON. package settings // ---------------------------------------------------------------------------- diff --git a/settings/settings.go b/settings/settings.go index dfb1c9b..9e49956 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -1,5 +1,5 @@ /* -Package engineconfigurationjson is used to generate the JSON document used to configure a Senzing client. +Package settings is used to generate the JSON document used to configure a Senzing client. */ package settings @@ -18,23 +18,23 @@ import ( // ---------------------------------------------------------------------------- /* -The BuildSimpleSystemConfigurationJSONUsingEnvVars method is a convenience method -for invoking BuildSimpleSystemConfigurationJsonUsingMap without any mapped values. +The BuildSimpleSettingsUsingEnvVars method is a convenience method +for invoking BuildSimpleSettingsUsingMap without any mapped values. In other words, only environment variables will be used. -See BuildSimpleSystemConfigurationJsonUsingMap() for information on the environment variables used. +See BuildSimpleSettingsUsingMap() for information on the environment variables used. Output - A string containing a JSON document use when calling Senzing's Init(...) methods. See the example output. */ -func BuildSimpleSystemConfigurationJSONUsingEnvVars() (string, error) { +func BuildSimpleSettingsUsingEnvVars() (string, error) { attributeMap := map[string]string{} - return BuildSimpleSystemConfigurationJSONUsingMap(attributeMap) + return BuildSimpleSettingsUsingMap(attributeMap) } /* -The BuildSimpleSystemConfigurationJSONUsingMap method returns a JSON document for use with Senzing's Init(...) methods. +The BuildSimpleSettingsUsingMap method returns a JSON document for use with Senzing's Init(...) methods. If the environment variable SENZING_TOOLS_ENGINE_CONFIGURATION_JSON is set, the value of SENZING_TOOLS_ENGINE_CONFIGURATION_JSON will be returned unchanged. @@ -66,7 +66,7 @@ Output - A string containing a JSON document use when calling Senzing's Init(...) methods. See the example output. */ -func BuildSimpleSystemConfigurationJSONUsingMap(attributeMap map[string]string) (string, error) { +func BuildSimpleSettingsUsingMap(attributeMap map[string]string) (string, error) { var err error // If SENZING_TOOLS_ENGINE_CONFIGURATION_JSON is set, use it. @@ -132,23 +132,23 @@ func BuildSimpleSystemConfigurationJSONUsingMap(attributeMap map[string]string) } /* -The VerifySenzingEngineConfigurationJSON method inspects the Senzing engine configuration JSON to see if it is misconfigured. +The VerifySettings method inspects the Senzing engine configuration JSON to see if it is misconfigured. Errors are documented at https://hub.senzing.com/go-helpers/errors. Input - ctx: A context to control lifecycle. - - senzingEngineConfigurationJson: A JSON string. See https://github.com/senzing-garage/knowledge-base/blob/main/lists/environment-variables.md#senzing_tools_engine_configuration_json + - settings: A JSON string. See https://github.com/senzing-garage/knowledge-base/blob/main/lists/environment-variables.md#senzing_tools_engine_configuration_json */ -func VerifySenzingEngineConfigurationJSON(ctx context.Context, senzingEngineConfigurationJSON string) error { +func VerifySettings(ctx context.Context, settings string) error { var err error - parser := settingsparser.BasicEngineConfigurationJSONParser{ - EngineConfigurationJSON: senzingEngineConfigurationJSON, + parser := settingsparser.BasicSettingsParser{ + Settings: settings, } // Check database URLs. - databaseURLs, err := parser.GetDatabaseUrls(ctx) + databaseURLs, err := parser.GetDatabaseURLs(ctx) if err != nil { return err } @@ -210,13 +210,13 @@ func VerifySenzingEngineConfigurationJSON(ctx context.Context, senzingEngineConf // Os / Arch specific calls - err = verifySenzingEngineConfigurationJSON(ctx, senzingEngineConfigurationJSON) + err = verifySettings(ctx, settings) return err } // ---------------------------------------------------------------------------- -// Internal methods +// Private functions // ---------------------------------------------------------------------------- func getOsEnv(variableName string) (string, error) { diff --git a/settings/settings_darwin.go b/settings/settings_darwin.go index 43b666e..2c02fe0 100644 --- a/settings/settings_darwin.go +++ b/settings/settings_darwin.go @@ -1,6 +1,6 @@ //go:build darwin -package engineconfigurationjson +package settings import ( "context" @@ -47,7 +47,7 @@ func buildStruct(attributeMap map[string]string) SzConfiguration { ResourcePath: mapWithDefault(attributeMap, "resourcePath", resourcePath), SupportPath: mapWithDefault(attributeMap, "supportPath", supportPath), }, - Sql: SzConfigurationSql{ + SQL: SzConfigurationSQL{ Connection: databaseUrl, }, } @@ -60,9 +60,9 @@ func buildStruct(attributeMap map[string]string) SzConfiguration { return result } -func verifySenzingEngineConfigurationJson(ctx context.Context, engineConfigurationJson string) error { +func verifySettings(ctx context.Context, settings string) error { _ = ctx - _ = engineConfigurationJson + _ = settings var err error = nil return err } diff --git a/settings/settings_darwin_test.go b/settings/settings_darwin_test.go index 1bd43dd..9392120 100644 --- a/settings/settings_darwin_test.go +++ b/settings/settings_darwin_test.go @@ -1,16 +1,16 @@ //go:build darwin -package engineconfigurationjson +package settings var testCasesForOsArch = []testCaseMetadata{ { name: "sqlite3-001", - databaseUrl: "sqlite3://na:na@/var/opt/senzing/sqlite/G2C.db", - databaseUrlPath: "/var/opt/senzing/sqlite/G2C.db", + databaseURL: "sqlite3://na:na@/var/opt/senzing/sqlite/G2C.db", + databaseURLPath: "/var/opt/senzing/sqlite/G2C.db", }, { name: "sqlite3-002", - databaseUrl: `sqlite3://na:na@hostname/var/opt/senzing/sqlite/G2C.db`, - databaseUrlPath: "/var/opt/senzing/sqlite/G2C.db", + databaseURL: `sqlite3://na:na@hostname/var/opt/senzing/sqlite/G2C.db`, + databaseURLPath: "/var/opt/senzing/sqlite/G2C.db", }, } diff --git a/settings/settings_examples_test.go b/settings/settings_examples_test.go index 87704b1..a7a32af 100644 --- a/settings/settings_examples_test.go +++ b/settings/settings_examples_test.go @@ -10,11 +10,11 @@ import ( // Examples for godoc documentation // ---------------------------------------------------------------------------- -func ExampleBuildSimpleSystemConfigurationJSONUsingMap() { +func ExampleBuildSimpleSettingsUsingMap() { aMap := map[string]string{ "databaseUrl": "postgresql://username:password@hostname:5432/G2", } - result, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + result, err := BuildSimpleSettingsUsingMap(aMap) if err != nil { fmt.Println(err) } @@ -22,11 +22,11 @@ func ExampleBuildSimpleSystemConfigurationJSONUsingMap() { // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"CONNECTION":"postgresql://username:password@hostname:5432:G2/"}} } -func ExampleBuildSimpleSystemConfigurationJSONUsingMap_db2() { +func ExampleBuildSimpleSettingsUsingMap_db2() { aMap := map[string]string{ "databaseUrl": "db2://username:password@hostname:50000/G2", } - result, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + result, err := BuildSimpleSettingsUsingMap(aMap) if err != nil { fmt.Println(err) } @@ -34,11 +34,11 @@ func ExampleBuildSimpleSystemConfigurationJSONUsingMap_db2() { // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"CONNECTION":"db2://username:password@G2"}} } -func ExampleBuildSimpleSystemConfigurationJSONUsingMap_db2WithSchema() { +func ExampleBuildSimpleSettingsUsingMap_db2WithSchema() { aMap := map[string]string{ "databaseUrl": "db2://username:password@hostname:50000/G2/?schema=schemaname", } - result, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + result, err := BuildSimpleSettingsUsingMap(aMap) if err != nil { fmt.Println(err) } @@ -46,11 +46,11 @@ func ExampleBuildSimpleSystemConfigurationJSONUsingMap_db2WithSchema() { // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"CONNECTION":"db2://username:password@G2/?schema=schemaname"}} } -func ExampleBuildSimpleSystemConfigurationJSONUsingMap_oci() { +func ExampleBuildSimpleSettingsUsingMap_oci() { aMap := map[string]string{ "databaseUrl": "oci://username:password@hostname:1521/G2", } - result, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + result, err := BuildSimpleSettingsUsingMap(aMap) if err != nil { fmt.Println(err) } @@ -58,11 +58,11 @@ func ExampleBuildSimpleSystemConfigurationJSONUsingMap_oci() { // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"CONNECTION":"oci://username:password@G2"}} } -func ExampleBuildSimpleSystemConfigurationJSONUsingMap_mssql() { +func ExampleBuildSimpleSettingsUsingMap_mssql() { aMap := map[string]string{ "databaseUrl": "mssql://username:password@hostname:1433/G2", } - result, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + result, err := BuildSimpleSettingsUsingMap(aMap) if err != nil { fmt.Println(err) } @@ -70,11 +70,11 @@ func ExampleBuildSimpleSystemConfigurationJSONUsingMap_mssql() { // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"CONNECTION":"mssql://username:password@G2"}} } -func ExampleBuildSimpleSystemConfigurationJSONUsingMap_mysql() { +func ExampleBuildSimpleSettingsUsingMap_mysql() { aMap := map[string]string{ "databaseUrl": "mysql://username:password@hostname:3306/G2", } - result, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + result, err := BuildSimpleSettingsUsingMap(aMap) if err != nil { fmt.Println(err) } @@ -82,11 +82,11 @@ func ExampleBuildSimpleSystemConfigurationJSONUsingMap_mysql() { // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"CONNECTION":"mysql://username:password@hostname:3306/?schema=G2"}} } -func ExampleBuildSimpleSystemConfigurationJSONUsingMap_postgresql() { +func ExampleBuildSimpleSettingsUsingMap_postgresql() { aMap := map[string]string{ "databaseUrl": "postgresql://username:password@hostname:5432/G2", } - result, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + result, err := BuildSimpleSettingsUsingMap(aMap) if err != nil { fmt.Println(err) } @@ -94,22 +94,22 @@ func ExampleBuildSimpleSystemConfigurationJSONUsingMap_postgresql() { // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"CONNECTION":"postgresql://username:password@hostname:5432:G2/"}} } -func ExampleBuildSimpleSystemConfigurationJSONUsingMap_postgresqlWithSchema() { +func ExampleBuildSimpleSettingsUsingMap_postgresqlWithSchema() { aMap := map[string]string{ "databaseUrl": "postgresql://username:password@hostname:5432/G2/?schema=schemaname", } - result, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + result, err := BuildSimpleSettingsUsingMap(aMap) if err != nil { fmt.Println(err) } fmt.Println(result) // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"CONNECTION":"postgresql://username:password@hostname:5432:G2/?schema=schemaname"}} } -func ExampleBuildSimpleSystemConfigurationJSONUsingMap_sqlite() { +func ExampleBuildSimpleSettingsUsingMap_sqlite() { aMap := map[string]string{ "databaseUrl": "sqlite3://na:na@/var/opt/senzing/sqlite/G2C.db", } - result, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + result, err := BuildSimpleSettingsUsingMap(aMap) if err != nil { fmt.Println(err) } diff --git a/settings/settings_linux.go b/settings/settings_linux.go index 60befb2..66d6a3c 100644 --- a/settings/settings_linux.go +++ b/settings/settings_linux.go @@ -35,8 +35,8 @@ func buildStruct(attributeMap map[string]string) SzConfiguration { return result } -func verifySenzingEngineConfigurationJSON(ctx context.Context, engineConfigurationJSON string) error { - _ = engineConfigurationJSON +func verifySettings(ctx context.Context, settings string) error { + _ = settings _ = ctx var err error return err diff --git a/settings/settings_test.go b/settings/settings_test.go index 23c82c7..cbc9edb 100644 --- a/settings/settings_test.go +++ b/settings/settings_test.go @@ -63,48 +63,61 @@ var testCases = append(testCasesForMultiPlatform, testCasesForOsArch...) // Test Public functions // ---------------------------------------------------------------------------- -func TestBuildSimpleSystemConfigurationJSONUsingEnvVars(test *testing.T) { - _, err := BuildSimpleSystemConfigurationJSONUsingEnvVars() +func TestBuildSimpleSettingsUsingEnvVars(test *testing.T) { + _, err := BuildSimpleSettingsUsingEnvVars() testError(test, err) } -func TestBuildSimpleSystemConfigurationJSONUsingMap(test *testing.T) { +func TestBuildSimpleSettingsUsingMap(test *testing.T) { for _, testCase := range testCases { test.Run(testCase.name, func(test *testing.T) { aMap := buildMap(testCase) - _, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + _, err := BuildSimpleSettingsUsingMap(aMap) testError(test, err) }) } } -func TestBuildSimpleSystemConfigurationJSONUsingMap_using_SENZING_TOOLS_ENGINE_CONFIGURATION_JSON(test *testing.T) { +func TestBuildSimpleSettingsUsingMap_using_SENZING_TOOLS_ENGINE_CONFIGURATION_JSON(test *testing.T) { expected := "test value" test.Setenv("SENZING_TOOLS_ENGINE_CONFIGURATION_JSON", expected) - actual, err := BuildSimpleSystemConfigurationJSONUsingMap(map[string]string{}) + actual, err := BuildSimpleSettingsUsingMap(map[string]string{}) require.NoError(test, err) assert.Equal(test, expected, actual) } -func TestBuildSimpleSystemConfigurationJSONUsingMap_using_SENZING_ENGINE_CONFIGURATION_JSON(test *testing.T) { +func TestBuildSimpleSettingsUsingMap_using_SENZING_ENGINE_CONFIGURATION_JSON(test *testing.T) { expected := "test value" test.Setenv("SENZING_ENGINE_CONFIGURATION_JSON", expected) - actual, err := BuildSimpleSystemConfigurationJSONUsingMap(map[string]string{}) + actual, err := BuildSimpleSettingsUsingMap(map[string]string{}) require.NoError(test, err) assert.Equal(test, expected, actual) } -func TestBuildSimpleSystemConfigurationJSONUsingMap_ParseResult(test *testing.T) { +func TestBuildSimpleSettingsUsingMap_using_SENZING_TOOLS_LICENSE_STRING_BASE64(test *testing.T) { + ctx := context.TODO() + expected := "A1B2C3D4" + test.Setenv("SENZING_TOOLS_LICENSE_STRING_BASE64", expected) + actual, err := BuildSimpleSettingsUsingMap(map[string]string{}) + require.NoError(test, err) + parsedActual, err := settingsparser.New(actual) + require.NoError(test, err) + licenseStringBase64, err := parsedActual.GetLicenseStringBase64(ctx) + require.NoError(test, err) + assert.Equal(test, expected, licenseStringBase64) +} + +func TestBuildSimpleSettingsUsingMap_ParseResult(test *testing.T) { ctx := context.TODO() for _, testCase := range testCases { if len(testCase.databaseURLPath) > 0 { test.Run(testCase.name, func(test *testing.T) { aMap := buildMap(testCase) - engineConfigurationJSON, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + settings, err := BuildSimpleSettingsUsingMap(aMap) testError(test, err) - parsedEngineConfigurationJSON, err := settingsparser.New(engineConfigurationJSON) + parsedSettings, err := settingsparser.New(settings) testError(test, err) - databaseUrls, err := parsedEngineConfigurationJSON.GetDatabaseUrls(ctx) + databaseUrls, err := parsedSettings.GetDatabaseURLs(ctx) testError(test, err) parsedDatabaseURL, err := url.Parse(databaseUrls[0]) testError(test, err) @@ -114,22 +127,19 @@ func TestBuildSimpleSystemConfigurationJSONUsingMap_ParseResult(test *testing.T) } } -func TestVerifySenzingEngineConfigurationJson(test *testing.T) { +func TestVerifySettings(test *testing.T) { ctx := context.TODO() for _, testCase := range testCases { test.Run(testCase.name, func(test *testing.T) { aMap := buildMap(testCase) - testJSON, err := BuildSimpleSystemConfigurationJSONUsingMap(aMap) + testJSON, err := BuildSimpleSettingsUsingMap(aMap) testError(test, err) - err = VerifySenzingEngineConfigurationJSON(ctx, testJSON) + err = VerifySettings(ctx, testJSON) testError(test, err) }) } } -// func TestVerifySenzingEngineConfigurationJson_badDatabaseURL(test *testing.T) { -// } - // ---------------------------------------------------------------------------- // Test private functions // ---------------------------------------------------------------------------- diff --git a/settings/settings_windows.go b/settings/settings_windows.go index 8a9e267..35d4312 100644 --- a/settings/settings_windows.go +++ b/settings/settings_windows.go @@ -1,6 +1,6 @@ //go:build windows -package engineconfigurationjson +package settings import ( "context" @@ -23,7 +23,7 @@ func mapWithDefault(aMap map[string]string, key string, defaultValue string) str func buildStruct(attributeMap map[string]string) SzConfiguration { var result SzConfiguration - databaseUrl, ok := attributeMap["databaseUrl"] + databaseURL, ok := attributeMap["databaseUrl"] if !ok { return result } @@ -46,8 +46,8 @@ func buildStruct(attributeMap map[string]string) SzConfiguration { ResourcePath: mapWithDefault(attributeMap, "resourcePath", resourcePath), SupportPath: mapWithDefault(attributeMap, "supportPath", supportPath), }, - Sql: SzConfigurationSql{ - Connection: databaseUrl, + SQL: SzConfigurationSQL{ + Connection: databaseURL, }, } @@ -59,9 +59,9 @@ func buildStruct(attributeMap map[string]string) SzConfiguration { return result } -func verifySenzingEngineConfigurationJson(ctx context.Context, engineConfigurationJson string) error { +func verifySettings(ctx context.Context, settings string) error { _ = ctx - _ = engineConfigurationJson + _ = settings var err error = nil return err } diff --git a/settings/settings_windows_test.go b/settings/settings_windows_test.go index bc041dd..e08a8f2 100644 --- a/settings/settings_windows_test.go +++ b/settings/settings_windows_test.go @@ -1,26 +1,26 @@ //go:build windows -package engineconfigurationjson +package settings var testCasesForOsArch = []testCaseMetadata{ { name: "sqlite3-001", - databaseUrl: "sqlite3://na:na@nowhere/C:\\Temp\\sqlite\\G2C.db", - databaseUrlPath: "/C:\\Temp\\sqlite\\G2C.db", + databaseURL: "sqlite3://na:na@nowhere/C:\\Temp\\sqlite\\G2C.db", + databaseURLPath: "/C:\\Temp\\sqlite\\G2C.db", }, { name: "sqlite3-002", - databaseUrl: "sqlite3://na:na@nowhere/C:\\Temp\\sqlite\\G2C.db", - databaseUrlPath: `/C:\Temp\sqlite\G2C.db`, + databaseURL: "sqlite3://na:na@nowhere/C:\\Temp\\sqlite\\G2C.db", + databaseURLPath: `/C:\Temp\sqlite\G2C.db`, }, { name: "sqlite3-003", - databaseUrl: `sqlite3://na:na@nowhere/C:\Temp\sqlite\G2C.db`, - databaseUrlPath: "/C:\\Temp\\sqlite\\G2C.db", + databaseURL: `sqlite3://na:na@nowhere/C:\Temp\sqlite\G2C.db`, + databaseURLPath: "/C:\\Temp\\sqlite\\G2C.db", }, { name: "sqlite3-004", - databaseUrl: `sqlite3://na:na@nowhere/C:\Temp\sqlite\G2C.db`, - databaseUrlPath: `/C:\Temp\sqlite\G2C.db`, + databaseURL: `sqlite3://na:na@nowhere/C:\Temp\sqlite\G2C.db`, + databaseURLPath: `/C:\Temp\sqlite\G2C.db`, }, } diff --git a/settingsparser/doc.go b/settingsparser/doc.go index 4455e55..c7335d5 100644 --- a/settingsparser/doc.go +++ b/settingsparser/doc.go @@ -1,5 +1,5 @@ /* -The engineconfigurationjsonparser package is used to pull information from the _ENGINE_CONFIGURATION_JSON string. +The settingsparser package is used to pull information from the _ENGINE_CONFIGURATION_JSON string. Single-database example of a _ENGINE_CONFIGURATION_JSON string: diff --git a/settingsparser/main.go b/settingsparser/main.go index a42ae56..0d1a0b3 100644 --- a/settingsparser/main.go +++ b/settingsparser/main.go @@ -1,15 +1,19 @@ -// The engineconfigurationjsonparser package helps parse the _ENGINE_CONFIGURATION_JSON. +// The settingsparser package helps parse the _ENGINE_CONFIGURATION_JSON. package settingsparser -import "context" +import ( + "context" + "fmt" +) // ---------------------------------------------------------------------------- // Types - interface // ---------------------------------------------------------------------------- -type EngineConfigurationJSONParser interface { +type SettingsParser interface { GetConfigPath(ctx context.Context) (string, error) - GetDatabaseUrls(ctx context.Context) ([]string, error) + GetDatabaseURLs(ctx context.Context) ([]string, error) + GetLicenseStringBase64(ctx context.Context) (string, error) GetResourcePath(ctx context.Context) (string, error) GetSupportPath(ctx context.Context) (string, error) RedactedJSON(ctx context.Context) (string, error) @@ -20,9 +24,10 @@ type EngineConfigurationJSONParser interface { // ---------------------------------------------------------------------------- type EngineConfigurationPipeline struct { - ConfigPath string `json:"CONFIGPATH"` - ResourcePath string `json:"RESOURCEPATH"` - SupportPath string `json:"SUPPORTPATH"` + ConfigPath string `json:"CONFIGPATH"` + LicenseStringBase64 string `json:"LICENSESTRINGBASE64"` + ResourcePath string `json:"RESOURCEPATH"` + SupportPath string `json:"SUPPORTPATH"` } type EngineConfigurationSQL struct { @@ -41,3 +46,18 @@ type EngineConfiguration struct { // Identfier of the package found messages having the format "senzing-6401xxxx". const ComponentID = 6401 + +// ---------------------------------------------------------------------------- +// Constructor methods +// ---------------------------------------------------------------------------- + +func New(settings string) (SettingsParser, error) { + var err error + if !isJSON(settings) { + return nil, fmt.Errorf("incorrect JSON syntax in %s", settings) + } + result := &BasicSettingsParser{ + Settings: settings, + } + return result, err +} diff --git a/settingsparser/settingsparser.go b/settingsparser/settingsparser_basic.go similarity index 70% rename from settingsparser/settingsparser.go rename to settingsparser/settingsparser_basic.go index a695c9c..5e94107 100644 --- a/settingsparser/settingsparser.go +++ b/settingsparser/settingsparser_basic.go @@ -1,12 +1,11 @@ /* -Package engineconfigurationjsonparser is used to generate the JSON document used to configure a Senzing client. +Package settingsparser is used to generate the JSON document used to configure a Senzing client. */ package settingsparser import ( "context" "encoding/json" - "fmt" "net/url" "strconv" "strings" @@ -16,61 +15,9 @@ import ( // Types // ---------------------------------------------------------------------------- -// BasicEngineConfigurationJSONParser is the default implementation of the EngineConfigurationJsonParser interface. -type BasicEngineConfigurationJSONParser struct { - EngineConfigurationJSON string -} - -// ---------------------------------------------------------------------------- -// Internal methods -// ---------------------------------------------------------------------------- - -func contains(haystack []string, needle string) bool { - for _, value := range haystack { - if value == needle { - return true - } - } - return false -} - -func isJSON(unknownString string) bool { - unknownStringUnescaped, err := strconv.Unquote(unknownString) - if err != nil { - unknownStringUnescaped = unknownString - } - var jsonString json.RawMessage - return json.Unmarshal([]byte(unknownStringUnescaped), &jsonString) == nil -} - -func redactURL(aURL string) (string, error) { - parsedURL, err := url.Parse(aURL) - if err != nil { - if strings.HasPrefix(aURL, "postgresql") { - index := strings.LastIndex(aURL, ":") - aURL := aURL[:index] + "/" + aURL[index+1:] - parsedURL, err = url.Parse(aURL) - } - if err != nil { - return "", err - } - } - return parsedURL.Redacted(), nil -} - -// ---------------------------------------------------------------------------- -// Constructor methods -// ---------------------------------------------------------------------------- - -func New(engineConfigurationJSON string) (EngineConfigurationJSONParser, error) { - var err error - if !isJSON(engineConfigurationJSON) { - return nil, fmt.Errorf("incorrect JSON syntax in %s", engineConfigurationJSON) - } - result := &BasicEngineConfigurationJSONParser{ - EngineConfigurationJSON: engineConfigurationJSON, - } - return result, err +// BasicSettingsParser is the default implementation of the SettingsParser interface. +type BasicSettingsParser struct { + Settings string } // ---------------------------------------------------------------------------- @@ -86,11 +33,10 @@ Input Output - A string containing the value of a PIPELINE.CONFIGPATH. */ -func (parser *BasicEngineConfigurationJSONParser) GetConfigPath(ctx context.Context) (string, error) { +func (parser *BasicSettingsParser) GetConfigPath(ctx context.Context) (string, error) { _ = ctx engineConfiguration := &EngineConfiguration{} - - err := json.Unmarshal([]byte(parser.EngineConfigurationJSON), &engineConfiguration) + err := json.Unmarshal([]byte(parser.Settings), &engineConfiguration) if err != nil { return "", err } @@ -106,12 +52,11 @@ Input Output - A string containing the value of a PIPELINE.CONFIGPATH. */ -func (parser *BasicEngineConfigurationJSONParser) GetDatabaseUrls(ctx context.Context) ([]string, error) { +func (parser *BasicSettingsParser) GetDatabaseURLs(ctx context.Context) ([]string, error) { _ = ctx var result []string - engineConfiguration := &EngineConfiguration{} - err := json.Unmarshal([]byte(parser.EngineConfigurationJSON), &engineConfiguration) + err := json.Unmarshal([]byte(parser.Settings), &engineConfiguration) if err != nil { return result, err } @@ -123,7 +68,7 @@ func (parser *BasicEngineConfigurationJSONParser) GetDatabaseUrls(ctx context.Co if len(backend) > 0 && backend != "SQL" { var dictionary map[string]interface{} var databaseJSONKeys []string - err = json.Unmarshal([]byte(parser.EngineConfigurationJSON), &dictionary) + err = json.Unmarshal([]byte(parser.Settings), &dictionary) if err != nil { return result, err } @@ -154,6 +99,25 @@ func (parser *BasicEngineConfigurationJSONParser) GetDatabaseUrls(ctx context.Co return result, err } +/* +The GetLicenseStringBase64 method returns the PIPELINE.LICENSESTRINGBASE64 value of _ENGINE_CONFIGURATION_JSON. + +Input + - ctx: A context to control lifecycle. + +Output + - A string containing the value of a PIPELINE.LICENSESTRINGBASE64. +*/ +func (parser *BasicSettingsParser) GetLicenseStringBase64(ctx context.Context) (string, error) { + _ = ctx + engineConfiguration := &EngineConfiguration{} + err := json.Unmarshal([]byte(parser.Settings), &engineConfiguration) + if err != nil { + return "", err + } + return engineConfiguration.Pipeline.LicenseStringBase64, err +} + /* The GetResourcePath method returns the PIPELINE.RESOURCEPATH value of _ENGINE_CONFIGURATION_JSON. @@ -163,10 +127,10 @@ Input Output - A string containing the value of a PIPELINE.RESOURCEPATH. */ -func (parser *BasicEngineConfigurationJSONParser) GetResourcePath(ctx context.Context) (string, error) { +func (parser *BasicSettingsParser) GetResourcePath(ctx context.Context) (string, error) { _ = ctx engineConfiguration := &EngineConfiguration{} - err := json.Unmarshal([]byte(parser.EngineConfigurationJSON), &engineConfiguration) + err := json.Unmarshal([]byte(parser.Settings), &engineConfiguration) if err != nil { return "", err } @@ -182,10 +146,10 @@ Input Output - A string containing the value of a PIPELINE.SUPPORTPATH. */ -func (parser *BasicEngineConfigurationJSONParser) GetSupportPath(ctx context.Context) (string, error) { +func (parser *BasicSettingsParser) GetSupportPath(ctx context.Context) (string, error) { _ = ctx engineConfiguration := &EngineConfiguration{} - err := json.Unmarshal([]byte(parser.EngineConfigurationJSON), &engineConfiguration) + err := json.Unmarshal([]byte(parser.Settings), &engineConfiguration) if err != nil { return "", err } @@ -201,12 +165,12 @@ Input Output - The Senzing engine configuration JSON string with database URLs having redacted passwords. */ -func (parser *BasicEngineConfigurationJSONParser) RedactedJSON(ctx context.Context) (string, error) { - result := parser.EngineConfigurationJSON +func (parser *BasicSettingsParser) RedactedJSON(ctx context.Context) (string, error) { + result := parser.Settings // Get list of database URLs in the Senzing engine configuration json. - databaseUrls, err := parser.GetDatabaseUrls(ctx) + databaseUrls, err := parser.GetDatabaseURLs(ctx) if err != nil { return "", err } @@ -225,3 +189,40 @@ func (parser *BasicEngineConfigurationJSONParser) RedactedJSON(ctx context.Conte result = strings.Join(strings.Fields(result), "") return result, err } + +// ---------------------------------------------------------------------------- +// Internal methods +// ---------------------------------------------------------------------------- + +func contains(haystack []string, needle string) bool { + for _, value := range haystack { + if value == needle { + return true + } + } + return false +} + +func isJSON(unknownString string) bool { + unknownStringUnescaped, err := strconv.Unquote(unknownString) + if err != nil { + unknownStringUnescaped = unknownString + } + var jsonString json.RawMessage + return json.Unmarshal([]byte(unknownStringUnescaped), &jsonString) == nil +} + +func redactURL(aURL string) (string, error) { + parsedURL, err := url.Parse(aURL) + if err != nil { + if strings.HasPrefix(aURL, "postgresql") { + index := strings.LastIndex(aURL, ":") + aURL := aURL[:index] + "/" + aURL[index+1:] + parsedURL, err = url.Parse(aURL) + } + if err != nil { + return "", err + } + } + return parsedURL.Redacted(), nil +} diff --git a/settingsparser/settingsparser_examples_test.go b/settingsparser/settingsparser_examples_test.go new file mode 100644 index 0000000..95a5978 --- /dev/null +++ b/settingsparser/settingsparser_examples_test.go @@ -0,0 +1,132 @@ +package settingsparser + +import ( + "context" + "fmt" +) + +// ---------------------------------------------------------------------------- +// Examples for godoc documentation +// ---------------------------------------------------------------------------- + +func ExampleBasicSettingsParser_GetConfigPath() { + // For more information, visit https://github.com/senzing-garage/go-helpers/blob/main/settingsparser/settingsparser_test.go + ctx := context.TODO() + parser := getParser(ctx) + configPath, err := parser.GetConfigPath(ctx) + if err != nil { + fmt.Println(err) + } + fmt.Println(configPath) + // Output: /etc/opt/senzing +} + +func ExampleBasicSettingsParser_GetDatabaseURLs() { + // For more information, visit https://github.com/senzing-garage/go-helpers/blob/main/settingsparser/settingsparser_test.go + ctx := context.TODO() + parser := getParser(ctx) + configPath, err := parser.GetDatabaseURLs(ctx) + if err != nil { + fmt.Println(err) + } + fmt.Println(configPath) + // Output: [postgresql://username:password@db.example.com:5432:G2] +} + +func ExampleBasicSettingsParser_GetResourcePath() { + // For more information, visit https://github.com/senzing-garage/go-helpers/blob/main/settingsparser/settingsparser_test.go + ctx := context.TODO() + parser := getParser(ctx) + configPath, err := parser.GetResourcePath(ctx) + if err != nil { + fmt.Println(err) + } + fmt.Println(configPath) + // Output: /opt/senzing/g2/resources +} + +func ExampleBasicSettingsParser_GetSupportPath() { + // For more information, visit https://github.com/senzing-garage/go-helpers/blob/main/settingsparser/settingsparser_test.go + ctx := context.TODO() + parser := getParser(ctx) + configPath, err := parser.GetSupportPath(ctx) + if err != nil { + fmt.Println(err) + } + fmt.Println(configPath) + // Output: /opt/senzing/data +} + +func ExampleBasicSettingsParser_RedactedJSON_single() { + ctx := context.TODO() + parser := &BasicSettingsParser{ + Settings: ` + { + "PIPELINE": { + "CONFIGPATH": "/etc/opt/senzing", + "LICENSESTRINGBASE64": "${SENZING_LICENSE_BASE64_ENCODED}", + "RESOURCEPATH": "/opt/senzing/g2/resources", + "SUPPORTPATH": "/opt/senzing/data" + }, + "SQL": { + "BACKEND": "SQL", + "CONNECTION": "postgresql://username:password@db.example.com:5432:G2" + } + } + `, + } + + actual, err := parser.RedactedJSON(ctx) + if err != nil { + fmt.Println(err) + } + fmt.Println(actual) + // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","LICENSESTRINGBASE64":"${SENZING_LICENSE_BASE64_ENCODED}","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"BACKEND":"SQL","CONNECTION":"postgresql://username:xxxxx@db.example.com:5432/G2"}} +} + +func ExampleBasicSettingsParser_RedactedJSON_multiple() { + ctx := context.TODO() + + settings := ` + { + "PIPELINE": { + "CONFIGPATH": "/etc/opt/senzing", + "LICENSESTRINGBASE64": "${SENZING_LICENSE_BASE64_ENCODED}", + "RESOURCEPATH": "/opt/senzing/g2/resources", + "SUPPORTPATH": "/opt/senzing/data" + }, + "SQL": { + "BACKEND": "HYBRID", + "CONNECTION": "postgresql://username:password@db-1.example.com:5432:G2" + }, + "C1": { + "CLUSTER_SIZE": "1", + "DB_1": "postgresql://username:password@db-2.example.com:5432:G2" + }, + "C2": { + "CLUSTER_SIZE": "1", + "DB_1": "postgresql://username:password@db-3.example.com:5432:G2" + }, + "HYBRID": { + "RES_FEAT": "C1", + "RES_FEAT_EKEY": "C1", + "RES_FEAT_LKEY": "C1", + "RES_FEAT_STAT": "C1", + "LIB_FEAT": "C2", + "LIB_FEAT_HKEY": "C2" + } + } + ` + + parser, err := New(settings) + if err != nil { + fmt.Println(err) + } + + actual, err := parser.RedactedJSON(ctx) + if err != nil { + fmt.Println(err) + } + fmt.Println(actual) + // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","LICENSESTRINGBASE64":"${SENZING_LICENSE_BASE64_ENCODED}","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"BACKEND":"HYBRID","CONNECTION":"postgresql://username:xxxxx@db-1.example.com:5432/G2"},"C1":{"CLUSTER_SIZE":"1","DB_1":"postgresql://username:xxxxx@db-2.example.com:5432/G2"},"C2":{"CLUSTER_SIZE":"1","DB_1":"postgresql://username:xxxxx@db-3.example.com:5432/G2"},"HYBRID":{"RES_FEAT":"C1","RES_FEAT_EKEY":"C1","RES_FEAT_LKEY":"C1","RES_FEAT_STAT":"C1","LIB_FEAT":"C2","LIB_FEAT_HKEY":"C2"}} +} diff --git a/settingsparser/settingsparser_test.go b/settingsparser/settingsparser_test.go index 6c72dba..bd0903a 100644 --- a/settingsparser/settingsparser_test.go +++ b/settingsparser/settingsparser_test.go @@ -2,58 +2,21 @@ package settingsparser import ( "context" - "fmt" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var ( - engineConfigurationJSONParserSingleton *BasicEngineConfigurationJSONParser + settingsParserSingleton SettingsParser ) // ---------------------------------------------------------------------------- -// Internal functions -// ---------------------------------------------------------------------------- - -func getTestObject(ctx context.Context, test *testing.T) *BasicEngineConfigurationJSONParser { - _ = test - return getParser(ctx) -} - -func getParser(ctx context.Context) *BasicEngineConfigurationJSONParser { - _ = ctx - if engineConfigurationJSONParserSingleton == nil { - engineConfigurationJSONParserSingleton = &BasicEngineConfigurationJSONParser{ - EngineConfigurationJSON: ` - { - "PIPELINE": { - "CONFIGPATH": "/etc/opt/senzing", - "LICENSESTRINGBASE64": "${SENZING_LICENSE_BASE64_ENCODED}", - "RESOURCEPATH": "/opt/senzing/g2/resources", - "SUPPORTPATH": "/opt/senzing/data" - }, - "SQL": { - "CONNECTION": "postgresql://username:password@db.example.com:5432:G2" - } - } - `, - } - } - return engineConfigurationJSONParserSingleton -} - -func testError(test *testing.T, err error) { - if err != nil { - assert.FailNow(test, err.Error()) - } -} - -// ---------------------------------------------------------------------------- -// Test interface functions +// Test interface methods // ---------------------------------------------------------------------------- -func TestEngineConfigurationJsonParserImpl_GetConfigPath(test *testing.T) { +func TestSettingsParser_GetConfigPath(test *testing.T) { ctx := context.TODO() parser := getTestObject(ctx, test) actual, err := parser.GetConfigPath(ctx) @@ -61,10 +24,10 @@ func TestEngineConfigurationJsonParserImpl_GetConfigPath(test *testing.T) { assert.Equal(test, "/etc/opt/senzing", actual) } -func TestEngineConfigurationJsonParserImpl_GetDatabaseUrls(test *testing.T) { +func TestSettingsParser_GetDatabaseURLs(test *testing.T) { ctx := context.TODO() - parser := &BasicEngineConfigurationJSONParser{ - EngineConfigurationJSON: ` + parser := &BasicSettingsParser{ + Settings: ` { "PIPELINE": { "CONFIGPATH": "/etc/opt/senzing", @@ -79,15 +42,15 @@ func TestEngineConfigurationJsonParserImpl_GetDatabaseUrls(test *testing.T) { } `, } - actual, err := parser.GetDatabaseUrls(ctx) + actual, err := parser.GetDatabaseURLs(ctx) testError(test, err) assert.Equal(test, []string{"postgresql://username:password@db.example.com:5432:G2"}, actual) } -func TestEngineConfigurationJsonParserImpl_GetDatabaseUrls_Multi(test *testing.T) { +func TestSettingsParser_GetDatabaseURLs_Multi(test *testing.T) { ctx := context.TODO() - parser := &BasicEngineConfigurationJSONParser{ - EngineConfigurationJSON: ` + parser := &BasicSettingsParser{ + Settings: ` { "PIPELINE": { "CONFIGPATH": "/etc/opt/senzing", @@ -118,7 +81,7 @@ func TestEngineConfigurationJsonParserImpl_GetDatabaseUrls_Multi(test *testing.T } `, } - actual, err := parser.GetDatabaseUrls(ctx) + actual, err := parser.GetDatabaseURLs(ctx) testError(test, err) assert.Len(test, actual, 3) assert.True(test, contains(actual, "postgresql://username:password@db-1.example.com:5432:G2")) @@ -126,10 +89,9 @@ func TestEngineConfigurationJsonParserImpl_GetDatabaseUrls_Multi(test *testing.T assert.True(test, contains(actual, "postgresql://username:password@db-3.example.com:5432:G2")) } -func TestEngineConfigurationJsonParserImpl_New(test *testing.T) { +func TestSettingsParser_New(test *testing.T) { ctx := context.TODO() - - enginConfigurationJSON := ` + settings := ` { "PIPELINE": { "CONFIGPATH": "/etc/opt/senzing", @@ -160,9 +122,9 @@ func TestEngineConfigurationJsonParserImpl_New(test *testing.T) { } ` - parser, err := New(enginConfigurationJSON) + parser, err := New(settings) testError(test, err) - actual, err := parser.GetDatabaseUrls(ctx) + actual, err := parser.GetDatabaseURLs(ctx) testError(test, err) assert.Len(test, actual, 3) assert.True(test, contains(actual, "postgresql://username:password@db-1.example.com:5432:G2")) @@ -170,128 +132,45 @@ func TestEngineConfigurationJsonParserImpl_New(test *testing.T) { assert.True(test, contains(actual, "postgresql://username:password@db-3.example.com:5432:G2")) } -// ---------------------------------------------------------------------------- -// Examples for godoc documentation -// ---------------------------------------------------------------------------- - -func ExampleBasicEngineConfigurationJSONParser_GetConfigPath() { - // For more information, visit https://github.com/senzing-garage/go-helpers/blob/main/engineconfigurationjsonparser/engineconfigurationjsonparser_test.go - ctx := context.TODO() - parser := getParser(ctx) - configPath, err := parser.GetConfigPath(ctx) - if err != nil { - fmt.Println(err) - } - fmt.Println(configPath) - // Output: /etc/opt/senzing +func TestSettingsParser_New_badJSON(test *testing.T) { + settings := "}{" + _, err := New(settings) + require.Error(test, err) } -func ExampleBasicEngineConfigurationJSONParser_GetDatabaseUrls() { - // For more information, visit https://github.com/senzing-garage/go-helpers/blob/main/engineconfigurationjsonparser/engineconfigurationjsonparser_test.go - ctx := context.TODO() - parser := getParser(ctx) - configPath, err := parser.GetDatabaseUrls(ctx) - if err != nil { - fmt.Println(err) - } - fmt.Println(configPath) - // Output: [postgresql://username:password@db.example.com:5432:G2] -} - -func ExampleBasicEngineConfigurationJSONParser_GetResourcePath() { - // For more information, visit https://github.com/senzing-garage/go-helpers/blob/main/engineconfigurationjsonparser/engineconfigurationjsonparser_test.go - ctx := context.TODO() - parser := getParser(ctx) - configPath, err := parser.GetResourcePath(ctx) - if err != nil { - fmt.Println(err) - } - fmt.Println(configPath) - // Output: /opt/senzing/g2/resources -} +// ---------------------------------------------------------------------------- +// Internal functions +// ---------------------------------------------------------------------------- -func ExampleBasicEngineConfigurationJSONParser_GetSupportPath() { - // For more information, visit https://github.com/senzing-garage/go-helpers/blob/main/engineconfigurationjsonparser/engineconfigurationjsonparser_test.go - ctx := context.TODO() - parser := getParser(ctx) - configPath, err := parser.GetSupportPath(ctx) - if err != nil { - fmt.Println(err) - } - fmt.Println(configPath) - // Output: /opt/senzing/data +func getTestObject(ctx context.Context, test *testing.T) SettingsParser { + _ = test + return getParser(ctx) } -func ExampleBasicEngineConfigurationJSONParser_RedactedJSON_single() { - ctx := context.TODO() - parser := &BasicEngineConfigurationJSONParser{ - EngineConfigurationJSON: ` - { - "PIPELINE": { - "CONFIGPATH": "/etc/opt/senzing", - "LICENSESTRINGBASE64": "${SENZING_LICENSE_BASE64_ENCODED}", - "RESOURCEPATH": "/opt/senzing/g2/resources", - "SUPPORTPATH": "/opt/senzing/data" - }, - "SQL": { - "BACKEND": "SQL", - "CONNECTION": "postgresql://username:password@db.example.com:5432:G2" +func getParser(ctx context.Context) SettingsParser { + _ = ctx + if settingsParserSingleton == nil { + settingsParserSingleton = &BasicSettingsParser{ + Settings: ` + { + "PIPELINE": { + "CONFIGPATH": "/etc/opt/senzing", + "LICENSESTRINGBASE64": "${SENZING_LICENSE_BASE64_ENCODED}", + "RESOURCEPATH": "/opt/senzing/g2/resources", + "SUPPORTPATH": "/opt/senzing/data" + }, + "SQL": { + "CONNECTION": "postgresql://username:password@db.example.com:5432:G2" + } } - } - `, - } - - actual, err := parser.RedactedJSON(ctx) - if err != nil { - fmt.Println(err) + `, + } } - fmt.Println(actual) - // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","LICENSESTRINGBASE64":"${SENZING_LICENSE_BASE64_ENCODED}","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"BACKEND":"SQL","CONNECTION":"postgresql://username:xxxxx@db.example.com:5432/G2"}} + return settingsParserSingleton } -func ExampleBasicEngineConfigurationJSONParser_RedactedJSON_multiple() { - ctx := context.TODO() - - engineConfigurationJSON := ` - { - "PIPELINE": { - "CONFIGPATH": "/etc/opt/senzing", - "LICENSESTRINGBASE64": "${SENZING_LICENSE_BASE64_ENCODED}", - "RESOURCEPATH": "/opt/senzing/g2/resources", - "SUPPORTPATH": "/opt/senzing/data" - }, - "SQL": { - "BACKEND": "HYBRID", - "CONNECTION": "postgresql://username:password@db-1.example.com:5432:G2" - }, - "C1": { - "CLUSTER_SIZE": "1", - "DB_1": "postgresql://username:password@db-2.example.com:5432:G2" - }, - "C2": { - "CLUSTER_SIZE": "1", - "DB_1": "postgresql://username:password@db-3.example.com:5432:G2" - }, - "HYBRID": { - "RES_FEAT": "C1", - "RES_FEAT_EKEY": "C1", - "RES_FEAT_LKEY": "C1", - "RES_FEAT_STAT": "C1", - "LIB_FEAT": "C2", - "LIB_FEAT_HKEY": "C2" - } - } - ` - - parser, err := New(engineConfigurationJSON) - if err != nil { - fmt.Println(err) - } - - actual, err := parser.RedactedJSON(ctx) +func testError(test *testing.T, err error) { if err != nil { - fmt.Println(err) + assert.FailNow(test, err.Error()) } - fmt.Println(actual) - // Output: {"PIPELINE":{"CONFIGPATH":"/etc/opt/senzing","LICENSESTRINGBASE64":"${SENZING_LICENSE_BASE64_ENCODED}","RESOURCEPATH":"/opt/senzing/g2/resources","SUPPORTPATH":"/opt/senzing/data"},"SQL":{"BACKEND":"HYBRID","CONNECTION":"postgresql://username:xxxxx@db-1.example.com:5432/G2"},"C1":{"CLUSTER_SIZE":"1","DB_1":"postgresql://username:xxxxx@db-2.example.com:5432/G2"},"C2":{"CLUSTER_SIZE":"1","DB_1":"postgresql://username:xxxxx@db-3.example.com:5432/G2"},"HYBRID":{"RES_FEAT":"C1","RES_FEAT_EKEY":"C1","RES_FEAT_LKEY":"C1","RES_FEAT_STAT":"C1","LIB_FEAT":"C2","LIB_FEAT_HKEY":"C2"}} }