-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: Add typed helpers (config.str, config.bool) (#128)
* Revert strongly-typed values for `config.get` Starting with #120, `config.get` would return values of different types based on the type of the underlying schema field. For example, for toggle fields, the returned value would be a boolean instead of a string. Unfortunatley, there are multiple edge cases where this doesn't work. For example, if a schema field gets deleted but an installation still has a value for it, then we no longer know what type to return. On top of that, for generated fields, we don't have an easy way to tell what type they are. Generated fields aren't currently implemented in Pixlet, but they are available to Tidbyt-internal apps and will be in Pixlet soon. * config: Add typed config helpers You can now use `config.bool` to get a boolean representation of a config value. * Fix indentation in docs
- Loading branch information
1 parent
a555891
commit 7bfb818
Showing
11 changed files
with
235 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package runtime | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/mitchellh/hashstructure/v2" | ||
"go.starlark.net/starlark" | ||
) | ||
|
||
type AppletConfig map[string]string | ||
|
||
func (a AppletConfig) AttrNames() []string { | ||
return []string{ | ||
"get", | ||
"str", | ||
"bool", | ||
} | ||
} | ||
|
||
func (a AppletConfig) Attr(name string) (starlark.Value, error) { | ||
switch name { | ||
|
||
case "get", "str": | ||
return starlark.NewBuiltin("str", a.getString), nil | ||
|
||
case "bool": | ||
return starlark.NewBuiltin("bool", a.getBoolean), nil | ||
|
||
default: | ||
return nil, nil | ||
} | ||
} | ||
|
||
func (a AppletConfig) Get(key starlark.Value) (starlark.Value, bool, error) { | ||
switch v := key.(type) { | ||
case starlark.String: | ||
val, found := a[v.GoString()] | ||
return starlark.String(val), found, nil | ||
default: | ||
return nil, false, nil | ||
} | ||
} | ||
|
||
func (a AppletConfig) String() string { return "AppletConfig(...)" } | ||
func (a AppletConfig) Type() string { return "AppletConfig" } | ||
func (a AppletConfig) Freeze() {} | ||
func (a AppletConfig) Truth() starlark.Bool { return true } | ||
|
||
func (a AppletConfig) Hash() (uint32, error) { | ||
sum, err := hashstructure.Hash(a, hashstructure.FormatV2, nil) | ||
return uint32(sum), err | ||
} | ||
|
||
func (a AppletConfig) getString(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var key starlark.String | ||
var def starlark.Value | ||
def = starlark.None | ||
|
||
if err := starlark.UnpackPositionalArgs( | ||
"str", args, kwargs, 1, | ||
&key, &def, | ||
); err != nil { | ||
return nil, fmt.Errorf("unpacking arguments for config.str: %v", err) | ||
} | ||
|
||
val, ok := a[key.GoString()] | ||
if !ok { | ||
return def, nil | ||
} else { | ||
return starlark.String(val), nil | ||
} | ||
} | ||
|
||
func (a AppletConfig) getBoolean(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var key starlark.String | ||
var def starlark.Value | ||
def = starlark.None | ||
|
||
if err := starlark.UnpackPositionalArgs( | ||
"bool", args, kwargs, 1, | ||
&key, &def, | ||
); err != nil { | ||
return nil, fmt.Errorf("unpacking arguments for config.bool: %v", err) | ||
} | ||
|
||
val, ok := a[key.GoString()] | ||
if !ok { | ||
return def, nil | ||
} else { | ||
b, _ := strconv.ParseBool(val) | ||
return starlark.Bool(b), nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.