Skip to content

Commit

Permalink
wip: update config
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jan 22, 2025
1 parent a0b5065 commit 015f829
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"encoding/json"

"github.com/pkg/errors"
"github.com/zeiss/pkg/cast"
)

Expand Down Expand Up @@ -106,6 +107,31 @@ func (c *Config) Marshal() ([]byte, error) {
return json.Marshal(c)
}

// Unmarshal ...
func (c *Config) Unmarshal(data []byte) error {
cfg := struct {
Host *string `json:"host,omitempty"`
Port *int `json:"port,omitempty"`
HTTPPort *int `json:"http_port,omitempty"`
Gateway *Gateway
ClientAdvertise *string `json:"client_advertise,omitempty"`
TLS *TLS `json:"tls,omitempty"`
}{}

if err := json.Unmarshal(data, &cfg); err != nil {

Check failure on line 121 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / lint

the given struct should be annotated with the `json` tag (musttag)
return errors.WithStack(err)
}

c.Host = cfg.Host
c.Port = cfg.Port
c.HTTPPort = cfg.HTTPPort
c.Gateway = cfg.Gateway
c.ClientAdvertise = cfg.ClientAdvertise
c.TLS = cfg.TLS

return nil
}

// Gateway ...
type Gateway struct {
// Name ...
Expand All @@ -124,6 +150,17 @@ type Gateway struct {
Advertise *string `json:"advertise,omitempty"`
// ConnectTimeout ...
ConnectRetries *int `json:"connect_retries,omitempty"`
// Gateways ...
}

// GatewayEntry ...
type GatewayEntry struct {
// Name ...
Name string `json:"name"`
// URLS ...
URLS []string `json:"urls"`
// TLS ...
TLS *TLS `json:"tls,omitempty"`
}

// Authorization ...
Expand Down
27 changes: 27 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/require"
"github.com/zeiss/natz-operator/pkg/config"
"github.com/zeiss/pkg/cast"
)

func TestNew(t *testing.T) {
Expand All @@ -24,3 +25,29 @@ func TestDefault(t *testing.T) {
require.NoError(t, err)
require.JSONEq(t, `{"host":"0.0.0.0","port":4222}`, string(json))
}

func TestUnmarshal(t *testing.T) {
t.Parallel()

cfg := config.New()
require.NotNil(t, cfg)

err := cfg.Unmarshal([]byte(`{"host": "localhost", "port": 4223}`))
require.NoError(t, err)
require.Equal(t, "localhost", *cfg.Host)
require.Equal(t, 4223, *cfg.Port)
}

func TestMarshal(t *testing.T) {
t.Parallel()

cfg := config.New()
require.NotNil(t, cfg)

cfg.Host = cast.Ptr("localhost")
cfg.Port = cast.Ptr(4223)

json, err := cfg.Marshal()
require.NoError(t, err)
require.JSONEq(t, `{"host":"localhost","port":4223}`, string(json))
}

0 comments on commit 015f829

Please sign in to comment.