Skip to content

Commit 78f5cd0

Browse files
authored
Add runtime field flag in documentation files (#2415)
This PR updates the generation of fields documentation to include the runtime flag in the corresponding field definitions.
1 parent f61be1a commit 78f5cd0

File tree

8 files changed

+65
-254
lines changed

8 files changed

+65
-254
lines changed

internal/docs/exported_fields.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type fieldsTableRecord struct {
1818
aType string
1919
unit string
2020
metricType string
21+
runtime bool
2122
}
2223

2324
var escaper = strings.NewReplacer("*", "\\*", "{", "\\{", "}", "\\}", "<", "\\<", ">", "\\>")
@@ -74,10 +75,14 @@ func renderFieldsTable(builder *strings.Builder, collected []fieldsTableRecord)
7475
builder.WriteString("\n")
7576
for _, c := range collected {
7677
description := strings.TrimSpace(strings.ReplaceAll(c.description, "\n", " "))
78+
fieldType := c.aType
79+
if c.runtime {
80+
fieldType += " (runtime)"
81+
}
7782
builder.WriteString(fmt.Sprintf("| %s | %s | %s |",
7883
escaper.Replace(c.name),
7984
escaper.Replace(description),
80-
c.aType))
85+
fieldType))
8186
if unitsPresent {
8287
builder.WriteString(fmt.Sprintf(" %s |", c.unit))
8388
}
@@ -130,6 +135,7 @@ func visitFields(namePrefix string, f fields.FieldDefinition, records []fieldsTa
130135
aType: f.Type,
131136
unit: f.Unit,
132137
metricType: f.MetricType,
138+
runtime: f.Runtime.IsEnabled(),
133139
})
134140

135141
for _, multiField := range f.MultiFields {

internal/fields/model.go

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"strings"
1111

1212
"gopkg.in/yaml.v3"
13-
14-
"github.com/elastic/elastic-package/internal/common"
1513
)
1614

1715
// FieldDefinition describes a single field with its properties.
@@ -34,6 +32,7 @@ type FieldDefinition struct {
3432
Fields FieldDefinitions `yaml:"fields,omitempty"`
3533
MultiFields []FieldDefinition `yaml:"multi_fields,omitempty"`
3634
Reusable *ReusableConfig `yaml:"reusable,omitempty"`
35+
Runtime runtimeField `yaml:"runtime"`
3736

3837
// disallowAtTopLevel transfers the reusability config from parent groups to nested fields.
3938
// It is negated respect to Reusable.TopLevel, so it is disabled by default.
@@ -44,82 +43,6 @@ type ReusableConfig struct {
4443
TopLevel bool `yaml:"top_level"`
4544
}
4645

47-
func (orig *FieldDefinition) Update(fd FieldDefinition) {
48-
if fd.Name != "" {
49-
orig.Name = fd.Name
50-
}
51-
if fd.Description != "" {
52-
orig.Description = fd.Description
53-
}
54-
if fd.Type != "" {
55-
orig.Type = fd.Type
56-
}
57-
if fd.ObjectType != "" {
58-
orig.ObjectType = fd.ObjectType
59-
}
60-
if fd.Value != "" {
61-
orig.Value = fd.Value
62-
}
63-
if len(fd.AllowedValues) > 0 {
64-
orig.AllowedValues = fd.AllowedValues
65-
}
66-
if len(fd.ExpectedValues) > 0 {
67-
orig.ExpectedValues = fd.ExpectedValues
68-
}
69-
if fd.Pattern != "" {
70-
orig.Pattern = fd.Pattern
71-
}
72-
if fd.Unit != "" {
73-
orig.Unit = fd.Unit
74-
}
75-
if fd.MetricType != "" {
76-
orig.MetricType = fd.MetricType
77-
}
78-
if fd.External != "" {
79-
orig.External = fd.External
80-
}
81-
if fd.Index != nil {
82-
orig.Index = fd.Index
83-
}
84-
if fd.DocValues != nil {
85-
orig.DocValues = fd.DocValues
86-
}
87-
88-
if len(fd.Normalize) > 0 {
89-
orig.Normalize = common.StringSlicesUnion(orig.Normalize, fd.Normalize)
90-
}
91-
92-
if len(fd.Fields) > 0 {
93-
orig.Fields = updateFields(orig.Fields, fd.Fields)
94-
}
95-
96-
if len(fd.MultiFields) > 0 {
97-
orig.MultiFields = updateFields(orig.MultiFields, fd.MultiFields)
98-
}
99-
}
100-
101-
func updateFields(origFields, fields []FieldDefinition) []FieldDefinition {
102-
// When a subfield the same name exists, update it. When not, append it.
103-
updatedFields := make([]FieldDefinition, len(origFields))
104-
copy(updatedFields, origFields)
105-
for _, newField := range fields {
106-
found := false
107-
for i, origField := range origFields {
108-
if origField.Name != newField.Name {
109-
continue
110-
}
111-
112-
found = true
113-
updatedFields[i].Update(newField)
114-
break
115-
}
116-
if !found {
117-
updatedFields = append(updatedFields, newField)
118-
}
119-
}
120-
return updatedFields
121-
}
122-
12346
// FieldDefinitions is an array of FieldDefinition, this can be unmarshalled from
12447
// a yaml list or a yaml map.
12548
type FieldDefinitions []FieldDefinition

internal/fields/model_test.go

Lines changed: 0 additions & 174 deletions
This file was deleted.

internal/fields/runtimefields.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package fields
6+
7+
import (
8+
"strconv"
9+
10+
"gopkg.in/yaml.v3"
11+
)
12+
13+
// Code based on the definition of Runtime Field in package-spec
14+
// https://github.com/elastic/package-spec/blob/964c4a69e024cc464c4808720ba0db9f001a82a7/code/go/internal/validator/semantic/types.go#L26
15+
type runtimeField struct {
16+
enabled bool
17+
script string
18+
}
19+
20+
// Ensure runtimeField implements yaml.Unmarshaler.
21+
var _ yaml.Unmarshaler = new(runtimeField)
22+
23+
func (r *runtimeField) IsEnabled() bool {
24+
if r.enabled {
25+
return true
26+
}
27+
if r.script != "" {
28+
return true
29+
}
30+
return false
31+
}
32+
33+
func (r runtimeField) String() string {
34+
if r.script != "" {
35+
return r.script
36+
}
37+
return strconv.FormatBool(r.enabled)
38+
}
39+
40+
func (r *runtimeField) unmarshalString(text string) error {
41+
value, err := strconv.ParseBool(text)
42+
if err == nil {
43+
r.enabled = value
44+
return nil
45+
}
46+
47+
// JSONSchema already checks about the type of this field (e.g. int or float)
48+
r.enabled = true
49+
r.script = text
50+
return nil
51+
}
52+
53+
// UnmarshalYAML implements the yaml.Marshaler interface for runtime.
54+
func (r *runtimeField) UnmarshalYAML(value *yaml.Node) error {
55+
return r.unmarshalString(value.Value)
56+
}

internal/testrunner/runners/system/tester.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ func (p *pipelineTrace) UnmarshalJSON(d []byte) error {
10071007
case string:
10081008
*p = append(*p, v)
10091009
case []any:
1010-
// asume it is going to be an array of strings
1010+
// assume it is going to be an array of strings
10111011
for _, value := range v {
10121012
*p = append(*p, fmt.Sprint(value))
10131013
}

0 commit comments

Comments
 (0)