Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add constructServiceURL function #119

Merged
merged 6 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions v5/core/parameterized_url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package core

// (C) Copyright IBM Corp. 2021.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import (
"fmt"
"sort"
"strings"
)

//
// constructServiceURL returns a service URL that is constructed by formatting a parameterized URL.
//
// Parameters:
//
// parameterizedUrl: URL that contains variable placeholders, e.g. "{scheme}://ibm.com".
//
// defaultUrlVariables: map from variable names to default values.
// Each variable in the parameterized URL must have a default value specified in this map.
//
// providedUrlVariables: map from variable names to desired values.
// If a variable is not provided in this map,
// the default variable value will be used instead.
//
func constructServiceURL(
parameterizedUrl string,
defaultUrlVariables map[string]string,
providedUrlVariables map[string]string,
) (string, error) {

// If null was passed, we set the variables to an empty map.
// This results in all default variable values being used.
if providedUrlVariables == nil {
providedUrlVariables = map[string]string{}
}

// Verify the provided variable names.
for providedName := range providedUrlVariables {
if _, ok := defaultUrlVariables[providedName]; !ok {
// Get all variable names (the keys of the default variables map).
acceptedNames := make([]string, len(defaultUrlVariables))

i := 0
for name := range defaultUrlVariables {
acceptedNames[i] = name
i++
}
sort.Strings(acceptedNames)

return "", fmt.Errorf(
"'%s' is an invalid variable name.\nValid variable names: %s.",
providedName,
acceptedNames,
)
}
}

// Format the URL with provided or default variable values.
formattedUrl := parameterizedUrl

for name, defaultValue := range defaultUrlVariables {
providedValue, ok := providedUrlVariables[name]

// Use the default variable value if none was provided.
if !ok {
providedValue = defaultValue
}
formattedUrl = strings.Replace(formattedUrl, "{"+name+"}", providedValue, 1)
}
return formattedUrl, nil
}
78 changes: 78 additions & 0 deletions v5/core/parameterized_url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// +build all fast

package core

// (C) Copyright IBM Corp. 2021.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import (
"testing"

"github.com/stretchr/testify/assert"
)

const parameterizedUrl = "{scheme}://{domain}:{port}"

var defaultUrlVariables = map[string]string{
"scheme": "http",
"domain": "ibm.com",
"port": "9300",
}

func TestConstructServiceURLWithNil(t *testing.T) {
url, err := constructServiceURL(parameterizedUrl, defaultUrlVariables, nil)

assert.Equal(t, url, "http://ibm.com:9300")
assert.Nil(t, err)
}

func TestConstructServiceURLWithSomeProvidedVariables(t *testing.T) {
providedUrlVariables := map[string]string{
"scheme": "https",
"port": "22",
}

url, err := constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables)

assert.Equal(t, url, "https://ibm.com:22")
assert.Nil(t, err)
}

func TestConstructServiceURLWithAllProvidedVariables(t *testing.T) {
var providedUrlVariables = map[string]string{
"scheme": "https",
"domain": "google.com",
"port": "22",
}

url, err := constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables)

assert.Equal(t, url, "https://google.com:22")
assert.Nil(t, err)
}

func TestConstructServiceURLWithInvalidVariable(t *testing.T) {
var providedUrlVariables = map[string]string{
"server": "value",
}

url, err := constructServiceURL(parameterizedUrl, defaultUrlVariables, providedUrlVariables)

assert.Equal(t, url, "")
assert.EqualError(
t,
err,
"'server' is an invalid variable name.\nValid variable names: [domain port scheme].",
)
}