Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed Feb 5, 2024
1 parent e2c2a6b commit f9ba7a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
20 changes: 10 additions & 10 deletions internal/storage/types/location_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"net/url"
"reflect"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -48,27 +47,28 @@ func (c LocationConfig) MarshalJSON() ([]byte, error) {
}

func (c *LocationConfig) UnmarshalJSON(blob []byte) error {
types := configTypes{}
// Raise an error if the doc describes multiple configs.
keys := map[string]json.RawMessage{}
err := json.Unmarshal(blob, &keys)
if err != nil {
return err
}
if len(keys) > 1 {
return errors.New("multiple config values have been assigned")
}

types := configTypes{}
if err := json.Unmarshal(blob, &types); err != nil {
return err
}

errMultipleConfig := errors.New("multiple config values have been assigned")
switch {
case types.S3 != nil:
c.Value = types.S3
case types.SFTPConfig != nil:
if reflect.ValueOf(c.Value).IsValid() {
return errMultipleConfig
}
c.Value = types.SFTPConfig
case types.URLConfig != nil:
if reflect.ValueOf(c.Value).IsValid() {
return errMultipleConfig
}
c.Value = types.URLConfig

default:
return errors.New("undefined configuration document")
}
Expand Down
10 changes: 10 additions & 0 deletions internal/storage/types/location_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ func TestLocationConfig(t *testing.T) {
err = json.Unmarshal(blob, &cfg)
assert.Error(t, err, "undefined configuration document")
assert.DeepEqual(t, cfg, types.LocationConfig{})

// It rejects multiple configs.
blob = []byte(`{
"s3":{"bucket":"perma-aips-1","region":"eu-west-1"},
"sftp":{"address":"sftp:22","username":"user","password":"secret","directory":"upload"}
}`)
cfg = types.LocationConfig{}
err = json.Unmarshal(blob, &cfg)
assert.Error(t, err, "multiple config values have been assigned")
assert.DeepEqual(t, cfg, types.LocationConfig{})
})
}

Expand Down

0 comments on commit f9ba7a3

Please sign in to comment.