-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide filter and override to code generator
- Loading branch information
Showing
13 changed files
with
484 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Copyright (c) F5, Inc. | ||
* | ||
* This source code is licensed under the Apache License, Version 2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/nginxinc/nginx-go-crossplane/internal/generator" | ||
) | ||
|
||
type filterFlag struct { | ||
filter map[string]struct{} | ||
} | ||
|
||
func (f *filterFlag) Set(value string) error { | ||
if f.filter == nil { | ||
f.filter = make(map[string]struct{}) | ||
} | ||
f.filter[value] = struct{}{} | ||
return nil | ||
} | ||
|
||
func (f *filterFlag) String() string { | ||
return fmt.Sprintf("%v", *f) | ||
} | ||
|
||
type overrideItem struct { | ||
directive string | ||
masks []generator.Mask | ||
} | ||
|
||
func (item *overrideItem) UnmarshalText(text []byte) error { | ||
rawOverride := string(text) | ||
|
||
// rawStr should follow the format: directive:bitmask00|bitmask01|...,bitmask10|bitmask11|... | ||
directive, definition, found := strings.Cut(rawOverride, ":") | ||
if !found { | ||
return errors.New("colon not found") | ||
} | ||
directive = strings.TrimSpace(directive) | ||
|
||
item.directive = directive | ||
if directive == "" { | ||
return errors.New("directive name is empty") | ||
} | ||
|
||
definition = strings.TrimSpace(definition) | ||
if definition == "" { | ||
return errors.New("directive definition is empty") | ||
} | ||
|
||
for _, varNamesStr := range strings.Split(definition, ",") { | ||
varNamesList := strings.Split(varNamesStr, "|") | ||
varNamesNum := len(varNamesList) | ||
directiveMask := make(generator.Mask, varNamesNum) | ||
|
||
for idx, varName := range varNamesList { | ||
trimmedName := strings.TrimSpace(varName) | ||
if trimmedName == "" { | ||
return errors.New("one directive bitmask is empty, check if there are unnecessary |") | ||
} | ||
|
||
directiveMask[idx] = trimmedName | ||
} | ||
item.masks = append(item.masks, directiveMask) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type override map[string][]generator.Mask | ||
|
||
func (ov *override) String() string { | ||
if ov == nil { | ||
return "nil" | ||
} | ||
return fmt.Sprintf("%v", *ov) | ||
} | ||
|
||
func (ov *override) Set(value string) error { | ||
if *ov == nil { | ||
*ov = override{} | ||
} | ||
var item overrideItem | ||
err := item.UnmarshalText([]byte(value)) | ||
if err != nil { | ||
return fmt.Errorf("invalid override %s:%w", value, err) | ||
} | ||
|
||
(*ov)[item.directive] = item.masks | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* Copyright (c) F5, Inc. | ||
* | ||
* This source code is licensed under the Apache License, Version 2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nginxinc/nginx-go-crossplane/internal/generator" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
//nolint:funlen | ||
func TestOverrideParser(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
input string | ||
expected overrideItem | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "normalFormat_pass", | ||
input: "location:ngxHTTPMainConf|ngxConfTake12,ngxStreamMainConf", | ||
|
||
expected: overrideItem{ | ||
directive: "location", | ||
masks: []generator.Mask{ | ||
{"ngxHTTPMainConf", "ngxConfTake12"}, | ||
{"ngxStreamMainConf"}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "withSpaces_pass", | ||
input: "hash:ngxHTTPUpsConf | ngxConfTake12, ngxStreamUpsConf | ngxConfTake12", | ||
expected: overrideItem{ | ||
directive: "hash", | ||
masks: []generator.Mask{ | ||
{"ngxHTTPUpsConf", "ngxConfTake12"}, | ||
{"ngxStreamUpsConf", "ngxConfTake12"}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "withoutColon_fail", | ||
input: "hashngxHTTPUpsConf | ngxConfTake12,ngxStreamUpsConf | ngxConfTake12", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "colonLeftsideEmpty_fail", | ||
input: " :ngxHTTPUpsConf | ngxConfTake12,ngxStreamUpsConf | ngxConfTake12", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "colonRightsideEmpty_fail", | ||
input: "hash: ", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "emptyBitmask_fail", | ||
input: "hash: ngxHTTPUpsConf| ", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
var got overrideItem | ||
err := got.UnmarshalText([]byte(tc.input)) | ||
|
||
if tc.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
|
||
// If the testcase wants an error and there is an error, skip the output file validation. | ||
// Output makes no sense when there is an error. | ||
if err != nil { | ||
return | ||
} | ||
|
||
require.Equal(t, tc.expected, got) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.