Skip to content

Commit

Permalink
Add config option to exclude GroupVersion (#3)
Browse files Browse the repository at this point in the history
* Add config option to exclude GroupVersion

* Add tests
  • Loading branch information
charith-elastic authored Apr 30, 2020
1 parent bdc856d commit 235188f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 26 additions & 4 deletions processor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions processor/config_test.go
Original file line number Diff line number Diff line change
@@ -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"))
})
}
4 changes: 4 additions & 0 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 235188f

Please sign in to comment.