Skip to content

Commit

Permalink
refactor: remove extra unmarshalling code
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziosestito committed Aug 1, 2023
1 parent 294d431 commit f0fc519
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
18 changes: 6 additions & 12 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ func (s *Settings) Valid() (bool, error) {
}

func (s *Settings) UnmarshalJSON(data []byte) error {
// This is needed becaus golang-set v2.3.0 has a bug that prevents
// the correct unmarshalling of ThreadUnsafeSet types.
rawSettings := struct {
DeniedAnnotations []string `json:"denied_annotations"`
MandatoryAnnotations []string `json:"mandatory_annotations"`
ConstrainedAnnotations map[string]string `json:"constrained_annotations"`
DeniedAnnotations []string `json:"denied_annotations"`
MandatoryAnnotations []string `json:"mandatory_annotations"`
ConstrainedAnnotations map[string]*RegularExpression `json:"constrained_annotations"`
}{}

err := json.Unmarshal(data, &rawSettings)
Expand All @@ -128,15 +130,7 @@ func (s *Settings) UnmarshalJSON(data []byte) error {

s.DeniedAnnotations = mapset.NewThreadUnsafeSet[string](rawSettings.DeniedAnnotations...)
s.MandatoryAnnotations = mapset.NewThreadUnsafeSet[string](rawSettings.MandatoryAnnotations...)

s.ConstrainedAnnotations = make(map[string]*RegularExpression)
for key, value := range rawSettings.ConstrainedAnnotations {
re, err := CompileRegularExpression(value)
if err != nil {
return fmt.Errorf("Cannot compile regexp %s: %v", value, err)
}
s.ConstrainedAnnotations[key] = re
}
s.ConstrainedAnnotations = rawSettings.ConstrainedAnnotations

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestDetectNotValidSettingsDueToBrokenRegexp(t *testing.T) {
t.Error("Expected settings to not be valid")
}

if *response.Message != "Provided settings are not valid: Cannot compile regexp cc-[a+: error parsing regexp: missing closing ]: `[a+`" {
if *response.Message != "Provided settings are not valid: error parsing regexp: missing closing ]: `[a+`" {
t.Errorf("Unexpected validation error message: %s", *response.Message)
}
}
Expand Down

0 comments on commit f0fc519

Please sign in to comment.