Skip to content

Commit

Permalink
move Field/Fields to asset package from common
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch committed Mar 11, 2019
1 parent 7ee2049 commit 9e75599
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 252 deletions.
2 changes: 1 addition & 1 deletion libbeat/common/field.go → libbeat/asset/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package common
package asset

import (
"fmt"
Expand Down
23 changes: 12 additions & 11 deletions libbeat/common/field_test.go → libbeat/asset/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package common
package asset

import (
"strings"
Expand All @@ -25,6 +25,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/go-ucfg/yaml"
)

Expand Down Expand Up @@ -298,44 +299,44 @@ func TestGetField(t *testing.T) {

func TestFieldValidate(t *testing.T) {
tests := map[string]struct {
cfg MapStr
cfg common.MapStr
field Field
err bool
}{
"top level object type config": {
cfg: MapStr{"object_type": "scaled_float", "object_type_mapping_type": "float", "scaling_factor": 10},
cfg: common.MapStr{"object_type": "scaled_float", "object_type_mapping_type": "float", "scaling_factor": 10},
field: Field{ObjectType: "scaled_float", ObjectTypeMappingType: "float", ScalingFactor: 10},
err: false,
},
"multiple object type configs": {
cfg: MapStr{"object_type_params": []MapStr{
cfg: common.MapStr{"object_type_params": []common.MapStr{
{"object_type": "scaled_float", "object_type_mapping_type": "float", "scaling_factor": 100}}},
field: Field{ObjectTypeParams: []ObjectTypeCfg{{ObjectType: "scaled_float", ObjectTypeMappingType: "float", ScalingFactor: 100}}},
err: false,
},
"invalid config mixing object_type and object_type_params": {
cfg: MapStr{
cfg: common.MapStr{
"object_type": "scaled_float",
"object_type_params": []MapStr{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}},
"object_type_params": []common.MapStr{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}},
err: true,
},
"invalid config mixing object_type_mapping_type and object_type_params": {
cfg: MapStr{
cfg: common.MapStr{
"object_type_mapping_type": "float",
"object_type_params": []MapStr{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}},
"object_type_params": []common.MapStr{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}},
err: true,
},
"invalid config mixing scaling_factor and object_type_params": {
cfg: MapStr{
cfg: common.MapStr{
"scaling_factor": 100,
"object_type_params": []MapStr{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}},
"object_type_params": []common.MapStr{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}},
err: true,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
cfg, err := NewConfigFrom(test.cfg)
cfg, err := common.NewConfigFrom(test.cfg)
require.NoError(t, err)
var f Field
err = cfg.Unpack(&f)
Expand Down
3 changes: 2 additions & 1 deletion libbeat/idxmgmt/idxmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/beats/libbeat/asset"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/idxmgmt/ilm"
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestDefaultSupport_TemplateConfig(t *testing.T) {

cloneCfg := func(c template.TemplateConfig) template.TemplateConfig {
if c.AppendFields != nil {
tmp := make(common.Fields, len(c.AppendFields))
tmp := make(asset.Fields, len(c.AppendFields))
copy(tmp, c.AppendFields)
c.AppendFields = tmp
}
Expand Down
25 changes: 13 additions & 12 deletions libbeat/kibana/fields_transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ import (
"errors"
"fmt"

"github.com/elastic/beats/libbeat/asset"
"github.com/elastic/beats/libbeat/common"
)

var v640 = common.MustNewVersion("6.4.0")

type fieldsTransformer struct {
fields common.Fields
fields asset.Fields
transformedFields []common.MapStr
transformedFieldFormatMap common.MapStr
version *common.Version
keys map[string]int
migration bool
}

func newFieldsTransformer(version *common.Version, fields common.Fields, migration bool) (*fieldsTransformer, error) {
func newFieldsTransformer(version *common.Version, fields asset.Fields, migration bool) (*fieldsTransformer, error) {
if version == nil {
return nil, errors.New("Version must be given")
}
Expand Down Expand Up @@ -64,10 +65,10 @@ func (t *fieldsTransformer) transform() (transformed common.MapStr, err error) {
// add some meta fields
truthy := true
falsy := false
t.add(common.Field{Path: "_id", Type: "keyword", Index: &falsy, Analyzed: &falsy, DocValues: &falsy, Searchable: &falsy, Aggregatable: &falsy})
t.add(common.Field{Path: "_type", Type: "keyword", Index: &falsy, Analyzed: &falsy, DocValues: &falsy, Searchable: &truthy, Aggregatable: &truthy})
t.add(common.Field{Path: "_index", Type: "keyword", Index: &falsy, Analyzed: &falsy, DocValues: &falsy, Searchable: &falsy, Aggregatable: &falsy})
t.add(common.Field{Path: "_score", Type: "integer", Index: &falsy, Analyzed: &falsy, DocValues: &falsy, Searchable: &falsy, Aggregatable: &falsy})
t.add(asset.Field{Path: "_id", Type: "keyword", Index: &falsy, Analyzed: &falsy, DocValues: &falsy, Searchable: &falsy, Aggregatable: &falsy})
t.add(asset.Field{Path: "_type", Type: "keyword", Index: &falsy, Analyzed: &falsy, DocValues: &falsy, Searchable: &truthy, Aggregatable: &truthy})
t.add(asset.Field{Path: "_index", Type: "keyword", Index: &falsy, Analyzed: &falsy, DocValues: &falsy, Searchable: &falsy, Aggregatable: &falsy})
t.add(asset.Field{Path: "_score", Type: "integer", Index: &falsy, Analyzed: &falsy, DocValues: &falsy, Searchable: &falsy, Aggregatable: &falsy})

transformed = common.MapStr{
"fields": t.transformedFields,
Expand All @@ -76,7 +77,7 @@ func (t *fieldsTransformer) transform() (transformed common.MapStr, err error) {
return
}

func (t *fieldsTransformer) transformFields(commonFields common.Fields, path string) {
func (t *fieldsTransformer) transformFields(commonFields asset.Fields, path string) {
for _, f := range commonFields {
f.Path = f.Name
if path != "" {
Expand Down Expand Up @@ -119,7 +120,7 @@ func (t *fieldsTransformer) transformFields(commonFields common.Fields, path str
}
}

func (t *fieldsTransformer) update(target *common.MapStr, override common.Field) error {
func (t *fieldsTransformer) update(target *common.MapStr, override asset.Field) error {
field, _ := transformField(t.version, override)
if override.Type == "" || (*target)["type"] == field["type"] {
target.Update(field)
Expand All @@ -133,7 +134,7 @@ func (t *fieldsTransformer) update(target *common.MapStr, override common.Field)
return fmt.Errorf("field <%s> is duplicated", override.Path)
}

func (t *fieldsTransformer) add(f common.Field) {
func (t *fieldsTransformer) add(f asset.Field) {
if idx := t.keys[f.Path]; idx > 0 {
target := &t.transformedFields[idx-1] // 1-indexed
if err := t.update(target, f); err != nil {
Expand All @@ -150,7 +151,7 @@ func (t *fieldsTransformer) add(f common.Field) {
}
}

func transformField(version *common.Version, f common.Field) (common.MapStr, common.MapStr) {
func transformField(version *common.Version, f asset.Field) (common.MapStr, common.MapStr) {
field := common.MapStr{
"name": f.Path,
"count": f.Count,
Expand Down Expand Up @@ -217,7 +218,7 @@ func getVal(valP *bool, def bool) bool {
return def
}

func addParams(format *common.MapStr, version *common.Version, f common.Field) {
func addParams(format *common.MapStr, version *common.Version, f asset.Field) {
addFormatParam(format, "pattern", f.Pattern)
addFormatParam(format, "inputFormat", f.InputFormat)
addFormatParam(format, "outputFormat", f.OutputFormat)
Expand Down Expand Up @@ -248,7 +249,7 @@ func addFormatParam(f *common.MapStr, key string, val interface{}) {
}

// takes the highest version where major version <= given version
func addVersionedFormatParam(f *common.MapStr, version *common.Version, key string, val []common.VersionizedString) {
func addVersionedFormatParam(f *common.MapStr, version *common.Version, key string, val []asset.VersionizedString) {
if len(val) == 0 {
return
}
Expand Down
Loading

0 comments on commit 9e75599

Please sign in to comment.