From 235188f7b56997456591cb83dd9c95d777faa00a Mon Sep 17 00:00:00 2001 From: Charith Ellawala <52399125+charith-elastic@users.noreply.github.com> Date: Thu, 30 Apr 2020 09:53:31 +0100 Subject: [PATCH] Add config option to exclude GroupVersion (#3) * Add config option to exclude GroupVersion * Add tests --- config/config.go | 7 ++++--- processor/config.go | 30 ++++++++++++++++++++++++++---- processor/config_test.go | 36 ++++++++++++++++++++++++++++++++++++ processor/processor.go | 4 ++++ 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 processor/config_test.go diff --git a/config/config.go b/config/config.go index 8a7371f..e693fd0 100644 --- a/config/config.go +++ b/config/config.go @@ -29,9 +29,10 @@ type Config struct { } type ProcessorConfig struct { - MaxDepth int `json:"maxDepth"` - IgnoreTypes []string `json:"ignoreTypes"` - IgnoreFields []string `json:"ignoreFields"` + MaxDepth int `json:"maxDepth"` + IgnoreTypes []string `json:"ignoreTypes"` + IgnoreFields []string `json:"ignoreFields"` + IgnoreGroupVersions []string `json:"ignoreGroupVersions"` } type RenderConfig struct { diff --git a/processor/config.go b/processor/config.go index dc0cdc5..5fa03a5 100644 --- a/processor/config.go +++ b/processor/config.go @@ -29,8 +29,9 @@ func compileConfig(conf *config.Config) (cc *compiledConfig, err error) { } cc = &compiledConfig{ - ignoreTypes: make([]*regexp.Regexp, len(conf.Processor.IgnoreTypes)), - ignoreFields: make([]*regexp.Regexp, len(conf.Processor.IgnoreFields)), + ignoreTypes: make([]*regexp.Regexp, len(conf.Processor.IgnoreTypes)), + ignoreFields: make([]*regexp.Regexp, len(conf.Processor.IgnoreFields)), + ignoreGroupVersions: make([]*regexp.Regexp, len(conf.Processor.IgnoreGroupVersions)), } for i, t := range conf.Processor.IgnoreTypes { @@ -45,12 +46,33 @@ func compileConfig(conf *config.Config) (cc *compiledConfig, err error) { } } + for i, gv := range conf.Processor.IgnoreGroupVersions { + if cc.ignoreGroupVersions[i], err = regexp.Compile(gv); err != nil { + return nil, fmt.Errorf("failed to compile group-version regex '%s': %w", gv, err) + } + } + return } type compiledConfig struct { - ignoreTypes []*regexp.Regexp - ignoreFields []*regexp.Regexp + ignoreTypes []*regexp.Regexp + ignoreFields []*regexp.Regexp + ignoreGroupVersions []*regexp.Regexp +} + +func (cc *compiledConfig) shouldIgnoreGroupVersion(gv string) bool { + if cc == nil { + return false + } + + for _, re := range cc.ignoreGroupVersions { + if re.MatchString(gv) { + return true + } + } + + return false } func (cc *compiledConfig) shouldIgnoreType(fqn string) bool { diff --git a/processor/config_test.go b/processor/config_test.go new file mode 100644 index 0000000..d7e0b0b --- /dev/null +++ b/processor/config_test.go @@ -0,0 +1,36 @@ +package processor + +import ( + "testing" + + "github.com/elastic/crd-ref-docs/config" + "github.com/stretchr/testify/require" +) + +func TestCompiledConfig(t *testing.T) { + conf := &config.Config{ + Processor: config.ProcessorConfig{ + IgnoreTypes: []string{"typex$"}, + IgnoreFields: []string{`mytype\.Fieldy$`}, + IgnoreGroupVersions: []string{"groupz/v1$"}, + }, + } + + cc, err := compileConfig(conf) + require.NoError(t, err) + + t.Run("ignoreType", func(t *testing.T) { + require.True(t, cc.shouldIgnoreType("mytypex")) + require.False(t, cc.shouldIgnoreType("typexyz")) + }) + + t.Run("ignoreField", func(t *testing.T) { + require.True(t, cc.shouldIgnoreField("mytype", "Fieldy")) + require.False(t, cc.shouldIgnoreField("mytype", "Fieldyz")) + }) + + t.Run("ignoreGroupVersion", func(t *testing.T) { + require.True(t, cc.shouldIgnoreGroupVersion("groupz/v1")) + require.False(t, cc.shouldIgnoreGroupVersion("groupz/v1beta1")) + }) +} diff --git a/processor/processor.go b/processor/processor.go index 8dc1a9a..6be6218 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -152,6 +152,10 @@ func (p *processor) findAPITypes(directory string) error { continue } + if p.shouldIgnoreGroupVersion(gvInfo.GroupVersion.String()) { + continue + } + // let the parser know that we need this package p.parser.AddPackage(pkg)