Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for value in documentation #2167

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
aaa5c07
Add support for value in documentation
stevengoossensB Oct 16, 2024
f7ad42e
Merge branch 'elastic:main' into main
stevengoossensB Nov 11, 2024
8209729
Add requirement for package to be version 3.2.3 to add value and exam…
stevengoossensB Nov 13, 2024
4c541d8
Merge branch 'main' of https://github.com/stevengoossensB/elastic-pac…
stevengoossensB Nov 13, 2024
800d2aa
Fixed typo
stevengoossensB Nov 13, 2024
281e382
Rename method
stevengoossensB Nov 13, 2024
f8fbb90
Tabs to spaces
stevengoossensB Nov 13, 2024
d244ad4
Update format_version so it uses the new code
stevengoossensB Nov 14, 2024
28a200a
Incorporate PR comments
stevengoossensB Nov 14, 2024
8b28b02
Back to 1.0.0
stevengoossensB Nov 14, 2024
c1d697c
Set format_version to 3.3.1
stevengoossensB Nov 14, 2024
ed9f978
Corner case when there's only constant_keyword fields with values but…
stevengoossensB Nov 15, 2024
fe83b51
Bump format_version of test
stevengoossensB Nov 15, 2024
6152367
Formatting errors
stevengoossensB Nov 18, 2024
4ed69a6
Linting
stevengoossensB Nov 17, 2024
2955da6
Merge remote-tracking branch 'second/main'
stevengoossensB Nov 19, 2024
3cfb2bc
Revert "Merge remote-tracking branch 'second/main'"
stevengoossensB Nov 19, 2024
1c9a408
Revert "Linting"
stevengoossensB Nov 19, 2024
d95667f
Merge branch 'main' into main
stevengoossensB Nov 19, 2024
04cfabf
Sync with upstream
stevengoossensB Nov 19, 2024
16aefef
Fix merge
stevengoossensB Nov 19, 2024
da17e3b
Fix whitespace
stevengoossensB Nov 19, 2024
63b7d5a
revert format version for NGINX and increase format version for Anomali
stevengoossensB Dec 2, 2024
bd802df
Upgrade Anomali test package
stevengoossensB Dec 2, 2024
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
74 changes: 72 additions & 2 deletions internal/docs/exported_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
package docs

import (
"errors"
"fmt"
"sort"
"strings"

"github.com/Masterminds/semver/v3"

"github.com/elastic/elastic-package/internal/fields"
"github.com/elastic/elastic-package/internal/packages"
)

var semver3_2_3 = semver.MustParse("3.2.3")
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
jsoriano marked this conversation as resolved.
Show resolved Hide resolved

type fieldsTableRecord struct {
name string
description string
aType string
unit string
metricType string
value string
example string
}

var escaper = strings.NewReplacer("*", "\\*", "{", "\\{", "}", "\\}", "<", "\\<", ">", "\\>")
Expand Down Expand Up @@ -46,13 +54,37 @@ func renderExportedFields(fieldsParentDir string) (string, error) {
builder.WriteString("(no fields available)\n")
return builder.String(), nil
}
renderFieldsTable(&builder, collected)

packageRoot, found, err := packages.FindPackageRoot()
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", fmt.Errorf("locating package root failed: %w", err)
}
if !found {
return "", errors.New("package root not found, you can only create new data stream in the package context")
}
manifest, err := packages.ReadPackageManifestFromPackageRoot(packageRoot)
if err != nil {
return "", fmt.Errorf("failed to read package manifest from \"%s\"", packageRoot)
}
sv, err := semver.NewVersion(manifest.SpecVersion)
if err != nil {
return "", fmt.Errorf("failed to obtain spec version from package manifest in \"%s\"", packageRoot)
}

if sv.LessThan(semver3_2_3) {
renderFieldsTable(&builder, collected, false, false)
} else {
renderFieldsTable(&builder, collected, true, true)
}
jsoriano marked this conversation as resolved.
Show resolved Hide resolved

return builder.String(), nil
}

func renderFieldsTable(builder *strings.Builder, collected []fieldsTableRecord) {
func renderFieldsTable(builder *strings.Builder, collected []fieldsTableRecord, includeValues bool, includeExamples bool) {
unitsPresent := areUnitsPresent(collected)
metricTypesPresent := areMetricTypesPresent(collected)
valuesPresent := areValuesPresent(collected)
examplePresent := areExamplesPresent(collected)

builder.WriteString("| Field | Description | Type |")
if unitsPresent {
Expand All @@ -61,6 +93,12 @@ func renderFieldsTable(builder *strings.Builder, collected []fieldsTableRecord)
if metricTypesPresent {
builder.WriteString(" Metric Type |")
}
if valuesPresent && includeValues {
builder.WriteString(" Value |")
}
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
if examplePresent && includeExamples {
builder.WriteString(" Example |")
}

builder.WriteString("\n")
builder.WriteString("|---|---|---|")
Expand All @@ -70,6 +108,12 @@ func renderFieldsTable(builder *strings.Builder, collected []fieldsTableRecord)
if metricTypesPresent {
builder.WriteString("---|")
}
if valuesPresent && includeValues {
builder.WriteString("---|")
}
if examplePresent && includeExamples {
builder.WriteString("---|")
}
stevengoossensB marked this conversation as resolved.
Show resolved Hide resolved

builder.WriteString("\n")
for _, c := range collected {
Expand All @@ -84,6 +128,12 @@ func renderFieldsTable(builder *strings.Builder, collected []fieldsTableRecord)
if metricTypesPresent {
builder.WriteString(fmt.Sprintf(" %s |", c.metricType))
}
if valuesPresent && includeValues {
builder.WriteString(fmt.Sprintf(" %s |", c.value))
}
if examplePresent && includeExamples {
builder.WriteString(fmt.Sprintf(" %s |", c.example))
}
builder.WriteString("\n")
}
}
Expand All @@ -106,6 +156,24 @@ func areMetricTypesPresent(collected []fieldsTableRecord) bool {
return false
}

func areValuesPresent(collected []fieldsTableRecord) bool {
for _, c := range collected {
if c.value != "" {
return true
}
}
return false
}

func areExamplesPresent(collected []fieldsTableRecord) bool {
for _, c := range collected {
if c.example != "" {
return true
}
}
return false
}

func collectFieldsFromDefinitions(validator *fields.Validator) []fieldsTableRecord {
var records []fieldsTableRecord

Expand All @@ -130,6 +198,8 @@ func visitFields(namePrefix string, f fields.FieldDefinition, records []fieldsTa
aType: f.Type,
unit: f.Unit,
metricType: f.MetricType,
value: f.Value,
example: f.Example,
})

for _, multiField := range f.MultiFields {
Expand Down
1 change: 1 addition & 0 deletions internal/fields/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type FieldDefinition struct {
Unit string `yaml:"unit"`
MetricType string `yaml:"metric_type"`
External string `yaml:"external"`
Example string `yaml:"example"`
Index *bool `yaml:"index"`
Enabled *bool `yaml:"enabled"`
DocValues *bool `yaml:"doc_values"`
Expand Down