From 6162f5c1d0ca9251dec97bf2d6af6fdd48236785 Mon Sep 17 00:00:00 2001 From: Andrew Kroh Date: Fri, 15 Sep 2023 21:40:45 -0400 Subject: [PATCH] Add readme (#2) --- README.md | 37 +++++++++++++++++++++++++++++++++++++ ecs_test.go | 23 ++++++++++++++++++++++- internal/version/field.go | 2 +- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..31cd9aa --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# go-ecs + +go-ecs is a library for querying [ECS][ecs] fields by name to obtain the fields +definition (e.g. Elasticsearch field data type, description, etc). + +The library includes data from tagged released of [elastic/ecs][ecs_repo]. + +[ecs]: https://www.elastic.co/guide/en/ecs/current/index.html +[ecs_repo]: https://github.com/elastic/ecs + +## Install + +`go get github.com/andrewkroh/go-ecs@main` + +## Usage + +```go +package main + +import ( + "fmt" + + "github.com/andrewkroh/go-ecs" +) + +func main() { + field, err := ecs.Lookup("host.os.name", "8.10") + if err != nil { + return err + } + + fmt.Println("data_type", field.DataType) + fmt.Println("is array", field.Array) + fmt.Println("pattern", field.Pattern) + fmt.Println("description", field.Description) +} +``` \ No newline at end of file diff --git a/ecs_test.go b/ecs_test.go index c27d724..a2baf4e 100644 --- a/ecs_test.go +++ b/ecs_test.go @@ -17,7 +17,11 @@ package ecs -import "testing" +import ( + "encoding/json" + "fmt" + "testing" +) func TestLookup(t *testing.T) { testCases := []struct { @@ -65,3 +69,20 @@ func BenchmarkLookup(b *testing.B) { } } } + +func ExampleLookup() { + field, err := Lookup("host.os.name", "8.10") + if err != nil { + fmt.Println(err) + return + } + + data, _ := json.MarshalIndent(field, "", " ") + fmt.Printf("%s", data) + // Output: { + // "name": "host.os.name", + // "data_type": "keyword", + // "array": false, + // "description": "Operating system name, without the version." + // } +} diff --git a/internal/version/field.go b/internal/version/field.go index 9aa97e7..4dae7d3 100644 --- a/internal/version/field.go +++ b/internal/version/field.go @@ -29,7 +29,7 @@ type Field struct { Array bool `json:"array" yaml:"array"` // Regular expression pattern that can be used to validate the value. - Pattern string `json:"pattern" yaml:"pattern"` + Pattern string `json:"pattern,omitempty" yaml:"pattern,omitempty"` // Short description of the field. Description string `json:"description" yaml:"description"`