Skip to content

Commit 7754c15

Browse files
authored
Movelibbeat/opt and libbeat/common/transform packages (#33)
1 parent df72f6a commit 7754c15

File tree

7 files changed

+731
-2
lines changed

7 files changed

+731
-2
lines changed

api/npipe/listener_windows_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ func TestHTTPOverNamedPipe(t *testing.T) {
4444
})
4545

4646
go func() {
47-
err := http.Serve(l, mux)
48-
require.NoError(t, err)
47+
_ = http.Serve(l, mux)
4948
}()
5049

5150
c := http.Client{

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.17
44

55
require (
66
github.com/Microsoft/go-winio v0.5.2
7+
github.com/elastic/go-structform v0.0.9
78
github.com/elastic/go-ucfg v0.8.4
89
github.com/fatih/color v1.13.0
910
github.com/hashicorp/go-multierror v1.1.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
9898
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9999
github.com/elastic/go-licenser v0.3.1 h1:RmRukU/JUmts+rpexAw0Fvt2ly7VVu6mw8z4HrEzObU=
100100
github.com/elastic/go-licenser v0.3.1/go.mod h1:D8eNQk70FOCVBl3smCGQt/lv7meBeQno2eI1S5apiHQ=
101+
github.com/elastic/go-structform v0.0.9 h1:HpcS7xljL4kSyUfDJ8cXTJC6rU5ChL1wYb6cx3HLD+o=
102+
github.com/elastic/go-structform v0.0.9/go.mod h1:CZWf9aIRYY5SuKSmOhtXScE5uQiLZNqAFnwKR4OrIM4=
101103
github.com/elastic/go-sysinfo v1.1.1 h1:ZVlaLDyhVkDfjwPGU55CQRCRolNpc7P0BbyhhQZQmMI=
102104
github.com/elastic/go-sysinfo v1.1.1/go.mod h1:i1ZYdU10oLNfRzq4vq62BEwD2fH8KaWh6eh0ikPT9F0=
103105
github.com/elastic/go-ucfg v0.8.4 h1:OAHTnubzXKsYYYWVzl8psLcS5mCbNKjXxtMY41itthk=

opt/format_types.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package opt
19+
20+
// The types here work as wrappers for the sake of go-structform
21+
// Due to structform's streaming type, we can't declare nested types
22+
// inside of struct tag directives, so in the numerous cases where we have
23+
// fields like thing.bytes, thing.pct, etc, use these wrappers to keep it somewhat clean
24+
25+
// BytesOpt wraps a uint64 byte value in an option type
26+
type BytesOpt struct {
27+
Bytes Uint `json:"bytes" struct:"bytes"`
28+
}
29+
30+
// IsZero returns true if the underlying value nil
31+
func (opt BytesOpt) IsZero() bool {
32+
return opt.Bytes.IsZero()
33+
}
34+
35+
// Bytes wraps a uint64 byte value
36+
type Bytes struct {
37+
Bytes uint64 `json:"bytes" struct:"bytes"`
38+
}
39+
40+
// Us wraps a uint64 microseconds value
41+
type Us struct {
42+
Us uint64 `json:"us" struct:"us"`
43+
}
44+
45+
// Pct wraps a float64 percent value
46+
type Pct struct {
47+
Pct float64 `json:"pct" struct:"pct"`
48+
}
49+
50+
// PctOpt wraps a float64 percent value in an option type
51+
type PctOpt struct {
52+
Pct Float `json:"pct" struct:"pct"`
53+
}
54+
55+
// IsZero returns true if the underlying value nil
56+
func (opt PctOpt) IsZero() bool {
57+
return opt.Pct.IsZero()
58+
}

opt/opt.go

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package opt
19+
20+
import "github.com/elastic/go-structform"
21+
22+
// ZeroInterface is a type interface for cases where we need to cast from a void pointer
23+
type ZeroInterface interface {
24+
IsZero() bool
25+
}
26+
27+
// Int
28+
29+
// Int is a wrapper for "optional" types, with the bool value indicating
30+
// if the stored int is a legitimate value.
31+
type Int struct {
32+
exists bool
33+
value int
34+
}
35+
36+
// NewUintNone returns a new OptUint wrapper
37+
func NewIntNone() Int {
38+
return Int{
39+
exists: false,
40+
value: 0,
41+
}
42+
}
43+
44+
// UintWith returns a new OptUint wrapper with a given int
45+
func IntWith(i int) Int {
46+
return Int{
47+
exists: true,
48+
value: i,
49+
}
50+
}
51+
52+
// IsZero returns true if the underlying value nil
53+
func (opt Int) IsZero() bool {
54+
return !opt.exists
55+
}
56+
57+
// Exists returns true if the underlying value exists
58+
func (opt Int) Exists() bool {
59+
return opt.exists
60+
}
61+
62+
// ValueOr returns the stored value, or a given int
63+
// Please do not use this for populating reported data,
64+
// as we actually want to avoid sending zeros where values are functionally null
65+
func (opt Int) ValueOr(i int) int {
66+
if opt.exists {
67+
return opt.value
68+
}
69+
return i
70+
}
71+
72+
// Fold implements the folder interface for OptUint
73+
func (opt *Int) Fold(v structform.ExtVisitor) error {
74+
if opt.exists {
75+
value := opt.value
76+
_ = v.OnInt(value)
77+
} else {
78+
_ = v.OnNil()
79+
}
80+
return nil
81+
}
82+
83+
// Uint
84+
85+
// Uint is a wrapper for "optional" types, with the bool value indicating
86+
// if the stored int is a legitimate value.
87+
type Uint struct {
88+
exists bool
89+
value uint64
90+
}
91+
92+
// NewUintNone returns a new OptUint wrapper
93+
func NewUintNone() Uint {
94+
return Uint{
95+
exists: false,
96+
value: 0,
97+
}
98+
}
99+
100+
// UintWith returns a new OptUint wrapper with a given int
101+
func UintWith(i uint64) Uint {
102+
return Uint{
103+
exists: true,
104+
value: i,
105+
}
106+
}
107+
108+
// IsZero returns true if the underlying value nil
109+
func (opt Uint) IsZero() bool {
110+
return !opt.exists
111+
}
112+
113+
// Exists returns true if the underlying value exists
114+
func (opt Uint) Exists() bool {
115+
return opt.exists
116+
}
117+
118+
// ValueOr returns the stored value, or a given int
119+
// Please do not use this for populating reported data,
120+
// as we actually want to avoid sending zeros where values are functionally null
121+
func (opt Uint) ValueOr(i uint64) uint64 {
122+
if opt.exists {
123+
return opt.value
124+
}
125+
return i
126+
}
127+
128+
// SumOptUint sums a list of OptUint values
129+
func SumOptUint(opts ...Uint) uint64 {
130+
var sum uint64
131+
for _, opt := range opts {
132+
sum += opt.ValueOr(0)
133+
}
134+
return sum
135+
}
136+
137+
// Fold implements the folder interface for OptUint
138+
func (opt *Uint) Fold(v structform.ExtVisitor) error {
139+
if opt.exists {
140+
value := opt.value
141+
_ = v.OnUint64(value)
142+
} else {
143+
_ = v.OnNil()
144+
}
145+
return nil
146+
}
147+
148+
// Float
149+
150+
// Float is a wrapper for "optional" types, with the bool value indicating
151+
// if the stored int is a legitimate value.
152+
type Float struct {
153+
exists bool
154+
value float64
155+
}
156+
157+
// NewFloatNone returns a new uint wrapper
158+
func NewFloatNone() Float {
159+
return Float{
160+
exists: false,
161+
value: 0,
162+
}
163+
}
164+
165+
// FloatWith returns a new uint wrapper for the specified value
166+
func FloatWith(f float64) Float {
167+
return Float{
168+
exists: true,
169+
value: f,
170+
}
171+
}
172+
173+
// IsZero returns true if the underlying value nil
174+
func (opt Float) IsZero() bool {
175+
return !opt.exists
176+
}
177+
178+
// Exists returns true if the underlying value exists
179+
func (opt Float) Exists() bool {
180+
return opt.exists
181+
}
182+
183+
// ValueOr returns the stored value, or zero
184+
// Please do not use this for populating reported data,
185+
// as we actually want to avoid sending zeros where values are functionally null
186+
func (opt Float) ValueOr(f float64) float64 {
187+
if opt.exists {
188+
return opt.value
189+
}
190+
return f
191+
}
192+
193+
// Fold implements the folder interface for OptUint
194+
func (opt *Float) Fold(v structform.ExtVisitor) error {
195+
if opt.exists {
196+
value := opt.value
197+
_ = v.OnFloat64(value)
198+
} else {
199+
_ = v.OnNil()
200+
}
201+
return nil
202+
}

0 commit comments

Comments
 (0)