-
Notifications
You must be signed in to change notification settings - Fork 126
Add runtime field flag in documentation files #2415
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
Changes from 9 commits
2937d3f
cd2fa7a
8faf018
78c72be
40a1195
52ab78c
7a9ae0e
6b97687
8f9fe13
37f613c
c537098
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,6 @@ import ( | |
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/elastic/elastic-package/internal/common" | ||
) | ||
|
||
// FieldDefinition describes a single field with its properties. | ||
|
@@ -34,6 +32,7 @@ type FieldDefinition struct { | |
Fields FieldDefinitions `yaml:"fields,omitempty"` | ||
MultiFields []FieldDefinition `yaml:"multi_fields,omitempty"` | ||
Reusable *ReusableConfig `yaml:"reusable,omitempty"` | ||
Runtime runtimeField `yaml:"runtime"` | ||
|
||
// disallowAtTopLevel transfers the reusability config from parent groups to nested fields. | ||
// It is negated respect to Reusable.TopLevel, so it is disabled by default. | ||
|
@@ -44,82 +43,6 @@ type ReusableConfig struct { | |
TopLevel bool `yaml:"top_level"` | ||
} | ||
|
||
func (orig *FieldDefinition) Update(fd FieldDefinition) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't find any usages of this It looks like that it was removed in this PR #1335: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, this used to be neccesary 🤔 But good if we don't need it anymore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least, that function is not called anymore... |
||
if fd.Name != "" { | ||
orig.Name = fd.Name | ||
} | ||
if fd.Description != "" { | ||
orig.Description = fd.Description | ||
} | ||
if fd.Type != "" { | ||
orig.Type = fd.Type | ||
} | ||
if fd.ObjectType != "" { | ||
orig.ObjectType = fd.ObjectType | ||
} | ||
if fd.Value != "" { | ||
orig.Value = fd.Value | ||
} | ||
if len(fd.AllowedValues) > 0 { | ||
orig.AllowedValues = fd.AllowedValues | ||
} | ||
if len(fd.ExpectedValues) > 0 { | ||
orig.ExpectedValues = fd.ExpectedValues | ||
} | ||
if fd.Pattern != "" { | ||
orig.Pattern = fd.Pattern | ||
} | ||
if fd.Unit != "" { | ||
orig.Unit = fd.Unit | ||
} | ||
if fd.MetricType != "" { | ||
orig.MetricType = fd.MetricType | ||
} | ||
if fd.External != "" { | ||
orig.External = fd.External | ||
} | ||
if fd.Index != nil { | ||
orig.Index = fd.Index | ||
} | ||
if fd.DocValues != nil { | ||
orig.DocValues = fd.DocValues | ||
} | ||
|
||
if len(fd.Normalize) > 0 { | ||
orig.Normalize = common.StringSlicesUnion(orig.Normalize, fd.Normalize) | ||
} | ||
|
||
if len(fd.Fields) > 0 { | ||
orig.Fields = updateFields(orig.Fields, fd.Fields) | ||
} | ||
|
||
if len(fd.MultiFields) > 0 { | ||
orig.MultiFields = updateFields(orig.MultiFields, fd.MultiFields) | ||
} | ||
} | ||
|
||
func updateFields(origFields, fields []FieldDefinition) []FieldDefinition { | ||
// When a subfield the same name exists, update it. When not, append it. | ||
updatedFields := make([]FieldDefinition, len(origFields)) | ||
copy(updatedFields, origFields) | ||
for _, newField := range fields { | ||
found := false | ||
for i, origField := range origFields { | ||
if origField.Name != newField.Name { | ||
continue | ||
} | ||
|
||
found = true | ||
updatedFields[i].Update(newField) | ||
break | ||
} | ||
if !found { | ||
updatedFields = append(updatedFields, newField) | ||
} | ||
} | ||
return updatedFields | ||
} | ||
|
||
// FieldDefinitions is an array of FieldDefinition, this can be unmarshalled from | ||
// a yaml list or a yaml map. | ||
type FieldDefinitions []FieldDefinition | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed test (file) related to the |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
jsoriano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package fields | ||
|
||
import ( | ||
"strconv" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Code based on the definition of Runtime Field in package-spec | ||
// https://github.com/elastic/package-spec/blob/964c4a69e024cc464c4808720ba0db9f001a82a7/code/go/internal/validator/semantic/types.go#L26 | ||
type runtimeField struct { | ||
enabled bool | ||
script string | ||
} | ||
|
||
// Ensure runtimeField implements yaml.Unmarshaler. | ||
var _ yaml.Unmarshaler = new(runtimeField) | ||
|
||
func (r *runtimeField) IsEnabled() bool { | ||
if r.enabled { | ||
return true | ||
} | ||
if r.script != "" { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (r runtimeField) String() string { | ||
if r.script != "" { | ||
return r.script | ||
} | ||
return strconv.FormatBool(r.enabled) | ||
} | ||
|
||
func (r *runtimeField) unmarshalString(text string) error { | ||
value, err := strconv.ParseBool(text) | ||
if err == nil { | ||
r.enabled = value | ||
return nil | ||
} | ||
|
||
// JSONSchema already checks about the type of this field (e.g. int or float) | ||
r.enabled = true | ||
r.script = text | ||
return nil | ||
} | ||
|
||
// UnmarshalYAML implements the yaml.Marshaler interface for runtime. | ||
func (r *runtimeField) UnmarshalYAML(value *yaml.Node) error { | ||
return r.unmarshalString(value.Value) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.