Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion code/go/ecs/base.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion code/go/ecs/container.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions code/go/ecs/dns.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions code/go/ecs/event.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion code/go/ecs/file.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions code/go/ecs/host.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions code/go/ecs/observer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion code/go/ecs/registry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions code/go/ecs/related.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion code/go/ecs/rule.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions code/go/ecs/threat.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions code/go/ecs/tls.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion code/go/ecs/vulnerability.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions code/go/ecs/x509.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 46 additions & 15 deletions scripts/cmd/gocodegen/gocodegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (

wordwrap "github.com/mitchellh/go-wordwrap"

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

Expand Down Expand Up @@ -111,6 +110,16 @@ type Field struct {
JSONKey string
}

type YamlField struct {
Name string `config:"name"`
Type string `config:"type"`
Description string `config:"description"`
Format string `config:"format"`
Fields []YamlField `config:"fields"`
MultiFields []YamlField `config:"multi_fields"`
Normalize []string `config:"normalize"`
}

// Flags
var (
schemaDir string
Expand Down Expand Up @@ -138,9 +147,9 @@ func main() {
}

// Load schema files.
fields := common.Fields{}
fields := []YamlField{}
for _, path := range paths {
f := common.Fields{}
f := []YamlField{}

cfg, err := yaml.NewConfigWithFile(path)
if err != nil {
Expand Down Expand Up @@ -170,7 +179,7 @@ func main() {
}

for _, field := range group.Fields {
dataType := goDataType(field.Name, field.Type)
dataType := goDataType(field)
if strings.HasPrefix(dataType, "time.") {
t.ImportTime = true
}
Expand Down Expand Up @@ -215,6 +224,15 @@ func main() {
}
}

func isArrayField(field YamlField) bool {
for _, normalizations := range field.Normalize {
if normalizations == "array" {
return true
}
}
return false
}

// isSeparate returns true if the character is a field name separator. This is
// used to detect the separators in fields like ephemeral_id or instance.name.
func isSeparator(c rune) bool {
Expand Down Expand Up @@ -264,33 +282,46 @@ func trimTrailingWhitespace(text string) string {
}

// goDataType returns the Go type to use for Elasticsearch mapping data type.
func goDataType(fieldName, elasticsearchDataType string) string {
func goDataType(field YamlField) string {
dataType, special := nonNormalizedGoDataType(field)
if !special && isArrayField(field) {
return "[]" + dataType
}
return dataType
}

// nonNormalizedGoDataType returns the Go type without consideration of normalizations
// it also returns whether or not this was a "special" case that avoids additional normalizations
func nonNormalizedGoDataType(field YamlField) (string, bool) {
fieldName := field.Name
elasticsearchDataType := field.Type

// Special cases.
switch {
case fieldName == "duration" && elasticsearchDataType == "long":
return "time.Duration"
return "time.Duration", true
case fieldName == "args" && elasticsearchDataType == "keyword":
return "[]string"
return "[]string", true
}

switch elasticsearchDataType {
case "keyword", "text", "ip", "geo_point":
return "string"
return "string", false
case "long":
return "int64"
return "int64", false
case "integer":
return "int32"
return "int32", false
case "float":
return "float64"
return "float64", false
case "date":
return "time.Time"
return "time.Time", false
case "boolean":
return "bool"
return "bool", false
case "object":
return "map[string]interface{}"
return "map[string]interface{}", false
default:
log.Fatalf("no translation for %v (field %s)", elasticsearchDataType, fieldName)
return ""
return "", false
}
}

Expand Down