refactor: use the same string for API key properties

This commit is contained in:
Jan De Dobbeleer 2023-11-09 17:09:19 +01:00 committed by Jan De Dobbeleer
parent cf3dc7c069
commit 1298129b87
9 changed files with 141 additions and 46 deletions

View file

@ -15,6 +15,7 @@ type Properties interface {
GetInt(property Property, defaultValue int) int
GetKeyValueMap(property Property, defaultValue map[string]string) map[string]string
GetStringArray(property Property, defaultValue []string) []string
Get(property Property, defaultValue any) any
}
// Property defines one property of a segment for context
@ -158,6 +159,15 @@ func (m Map) GetStringArray(property Property, defaultValue []string) []string {
return keyValues
}
func (m Map) Get(property Property, defaultValue any) any {
val, found := m[property]
if !found {
return defaultValue
}
return val
}
func ParseStringArray(param any) []string {
switch v := param.(type) {
default:
@ -207,3 +217,25 @@ func parseKeyValueArray(param any) map[string]string {
return v
}
}
// Generic functions
type Value interface {
string | int | []string | float64 | bool
}
func OneOf[T Value](properties Properties, defaultValue T, props ...Property) T {
for _, prop := range props {
// get value on a generic get, then see if we can cast to T?
val := properties.Get(prop, nil)
if val == nil {
continue
}
if v, ok := val.(T); ok {
return v
}
}
return defaultValue
}

View file

@ -111,3 +111,57 @@ func TestGetFloat64PropertyNotInMap(t *testing.T) {
value := properties.GetFloat64(Foo, expected)
assert.Equal(t, expected, value)
}
func TestOneOf(t *testing.T) {
cases := []struct {
Case string
Map Map
Properties []Property
DefaultValue string
Expected any
}{
{
Case: "one element",
Expected: "1337",
Properties: []Property{Foo},
Map: Map{
Foo: "1337",
},
DefaultValue: "2000",
},
{
Case: "two elements",
Expected: "1337",
Properties: []Property{Foo},
Map: Map{
Foo: "1337",
"Bar": "9001",
},
DefaultValue: "2000",
},
{
Case: "no match",
Expected: "2000",
Properties: []Property{"Moo"},
Map: Map{
Foo: "1337",
"Bar": "9001",
},
DefaultValue: "2000",
},
{
Case: "incorrect type",
Expected: "2000",
Properties: []Property{Foo},
Map: Map{
Foo: 1337,
"Bar": "9001",
},
DefaultValue: "2000",
},
}
for _, tc := range cases {
value := OneOf(tc.Map, tc.DefaultValue, tc.Properties...)
assert.Equal(t, tc.Expected, value, tc.Case)
}
}

View file

@ -52,3 +52,9 @@ func (w *Wrapper) GetStringArray(property Property, defaultValue []string) []str
w.Env.Debug(fmt.Sprintf("%s: %v", property, value))
return value
}
func (w *Wrapper) Get(property Property, defaultValue any) any {
value := w.Properties.Get(property, defaultValue)
w.Env.Debug(fmt.Sprintf("%s: %v", property, value))
return value
}

View file

@ -34,7 +34,6 @@ type Brewfather struct {
const (
BFUserID properties.Property = "user_id"
BFAPIKey properties.Property = "api_key"
BFBatchID properties.Property = "batch_id"
BFDoubleUpIcon properties.Property = "doubleup_icon"
@ -228,7 +227,7 @@ func (bf *Brewfather) getResult() (*Batch, error) {
return nil, errors.New("missing Brewfather user id (user_id)")
}
apiKey := bf.props.GetString(BFAPIKey, "")
apiKey := bf.props.GetString(APIKey, "")
if len(apiKey) == 0 {
return nil, errors.New("missing Brewfather api key (api_key)")
}

View file

@ -148,7 +148,7 @@ func TestBrewfatherSegment(t *testing.T) {
props := properties.Map{
properties.CacheTimeout: tc.CacheTimeout,
BFBatchID: BFFakeBatchID,
BFAPIKey: "FAKE",
APIKey: "FAKE",
BFUserID: "FAKE",
}

View file

@ -23,7 +23,7 @@ type Owm struct {
const (
// APIKey openweathermap api key
APIKey properties.Property = "apikey"
APIKey properties.Property = "api_key"
// Location openweathermap location
Location properties.Property = "location"
// Units openweathermap units
@ -89,7 +89,7 @@ func (d *Owm) getResult() (*owmDataResponse, error) {
}
}
apikey := d.props.GetString(APIKey, ".")
apikey := properties.OneOf[string](d.props, ".", APIKey, "apiKey")
location := d.props.GetString(Location, "De Bilt,NL")
latitude := d.props.GetFloat64(Latitude, 91) // This default value is intentionally invalid since there should not be a default for this and 0 is a valid value
longitude := d.props.GetFloat64(Longitude, 181) // This default value is intentionally invalid since there should not be a default for this and 0 is a valid value

View file

@ -2280,16 +2280,16 @@
"properties": {
"properties": {
"properties": {
"apikey": {
"api_key": {
"type": "string",
"title": "apikey",
"description": "The apikey used for the api call (Required)",
"title": "API key",
"description": "The API key used for the api call (Required)",
"default": "."
},
"location": {
"type": "string",
"title": "location",
"description": "Location to use for the api call. Formatted as <City>,<STATE>,<COUNTRY_CODE>. City name, state code and country code divided by comma. Please, refer to ISO 3166 for the state codes or country codes.",
"description": "Location to use for the API call. Formatted as <City>,<STATE>,<COUNTRY_CODE>. City name, state code and country code divided by comma. Please, refer to ISO 3166 for the state codes or country codes.",
"default": "De Bilt,NL"
},
"units": {
@ -3182,16 +3182,16 @@
"description": "Text/icon to show when stopped",
"default": "\uF04D"
},
"apiKey": {
"api_key": {
"type": "string",
"title": "apiKey",
"description": "The apikey used for the api call (Required)",
"title": "API key",
"description": "The API key used for the API call (Required)",
"default": "."
},
"username": {
"type": "string",
"title": "username",
"description": "The username used for the api call (Required)",
"description": "The username used for the API call (Required)",
"default": "."
},
"http_timeout": {

View file

@ -19,22 +19,24 @@ You **must** request an [API key][api-key] at the LastFM website.
## Sample Configuration
import Config from '@site/src/components/Config.js';
import Config from "@site/src/components/Config.js";
<Config data={{
"background": "p:sky",
"foreground": "p:white",
"powerline_symbol": "\ue0b0",
"properties": {
"apikey": "<YOUR_API_KEY>",
"username": "<LASTFM_USERNAME>",
"http_timeout": 20000,
"cache_timeout": 1
},
"style": "powerline",
"template": " {{ .Icon }}{{ if ne .Status \"stopped\" }}{{ .Full }}{{ end }} ",
"type": "lastfm"
}}/>
<Config
data={{
background: "p:sky",
foreground: "p:white",
powerline_symbol: "\ue0b0",
properties: {
api_key: "<YOUR_API_KEY>",
username: "<LASTFM_USERNAME>",
http_timeout: 20000,
cache_timeout: 1,
},
style: "powerline",
template: ' {{ .Icon }}{{ if ne .Status "stopped" }}{{ .Full }}{{ end }} ',
type: "lastfm",
}}
/>
## Properties
@ -42,7 +44,7 @@ import Config from '@site/src/components/Config.js';
| -------------- | -------- | ------------------------------------------------------ |
| `playing_icon` | `string` | text/icon to show when playing - defaults to `\uE602 ` |
| `stopped_icon` | `string` | text/icon to show when stopped - defaults to `\uF04D ` |
| `apikey` | `string` | your LastFM [API key][api-key] |
| `api_key` | `string` | your LastFM [API key][api-key] |
| `username` | `string` | your LastFM username |
## Template ([info][templates])

View file

@ -15,29 +15,31 @@ The free tier for _Current weather and forecasts collection_ is sufficient.
## Sample Configuration
import Config from '@site/src/components/Config.js';
import Config from "@site/src/components/Config.js";
<Config data={{
"type": "owm",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#ffffff",
"background": "#FF0000",
"template": "{{.Weather}} ({{.Temperature}}{{.UnitIcon}})",
"properties": {
"apikey": "<YOUR_API_KEY>",
"location": "AMSTERDAM,NL",
"units": "metric",
"http_timeout": 20,
"cache_timeout": 10
}
}}/>
<Config
data={{
type: "owm",
style: "powerline",
powerline_symbol: "\uE0B0",
foreground: "#ffffff",
background: "#FF0000",
template: "{{.Weather}} ({{.Temperature}}{{.UnitIcon}})",
properties: {
api_key: "<YOUR_API_KEY>",
location: "AMSTERDAM,NL",
units: "metric",
http_timeout: 20,
cache_timeout: 10,
},
}}
/>
## Properties
| Name | Type | Description |
| --------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `apikey` | `string` | Your API key from [Open Weather Map][owm] |
| `api_key` | `string` | Your API key from [Open Weather Map][owm] |
| `latitude` | `float64` | The latitude of the requested location. |
| `longitude` | `float64` | The longitude of the requested location. |
| `location` | `string` | The requested location. Formatted as <City,STATE,COUNTRY_CODE>. City name, state code and country code divided by comma. Please, refer to ISO 3166 for the state codes or country codes - defaults to `DE BILT,NL` |