Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
62 changes: 0 additions & 62 deletions input/elasticapm/internal/modeldecoder/generator/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const (
// CodeGenerator creates following struct methods
//
// `IsSet() bool`
// `Reset()`
// `validate() error`
// `validate() error`
// `processNestedSource() error`
Expand Down Expand Up @@ -124,9 +123,6 @@ func (g *CodeGenerator) generate(st structType, key string) error {
if err := g.generateIsSet(st, key); err != nil {
return err
}
if err := g.generateReset(st, key); err != nil {
return err
}
if err := g.generateValidation(st, key); err != nil {
return err
}
Expand Down Expand Up @@ -199,64 +195,6 @@ func generateIsSet(w io.Writer, field structField, fieldSelectorPrefix string) e
}
}

// generateReset creates `Reset` methods for struct fields setting them to
// their zero values or calling their `Reset` methods
// it only considers exported fields
func (g *CodeGenerator) generateReset(structTyp structType, key string) error {
fmt.Fprintf(&g.buf, `
func (val *%s) Reset() {
`, structTyp.name)
if key != "" {
key += "."
}
for _, f := range structTyp.fields {
if !f.Exported() {
continue
}
switch t := f.Type().Underlying().(type) {
case *types.Slice:
// the slice len is set to zero, not returning the underlying
// memory to the garbage collector; when the size of slices differs
// this potentially leads to keeping more memory allocated than required;

// if slice type is a model struct,
// call its Reset() function
if _, ok := g.customStruct(t.Elem()); ok {
fmt.Fprintf(&g.buf, `
for i := range val.%s{
val.%s[i].Reset()
}
`[1:], f.Name(), f.Name())
}
// then reset size of slice to 0
fmt.Fprintf(&g.buf, `
val.%s = val.%s[:0]
`[1:], f.Name(), f.Name())

case *types.Map:
// the map is cleared, not returning the underlying memory to
// the garbage collector; when map size differs this potentially
// leads to keeping more memory allocated than required
fmt.Fprintf(&g.buf, `
for k := range val.%s {
delete(val.%s, k)
}
`[1:], f.Name(), f.Name())

case *types.Struct:
fmt.Fprintf(&g.buf, `
val.%s.Reset()
`[1:], f.Name())
default:
return fmt.Errorf("unhandled type %T for Reset() for '%s%s'", t, key, jsonName(f))
}
}
fmt.Fprint(&g.buf, `
}
`[1:])
return nil
}

// generateNestedSourceProcessor generates code for processing fully nested map of fields
// into flat fields as identified by their JSON tag. The nested source is identified by
// the tag `nested="true"`. If a struct has the nested source tag then all the other struct
Expand Down
58 changes: 0 additions & 58 deletions input/elasticapm/internal/modeldecoder/nullable/nullable.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,6 @@ func (v *String) IsSet() bool {
return v.isSet
}

// Reset sets the String to it's initial state
// where it is not set and has no value
func (v *String) Reset() {
v.Val = ""
v.isSet = false
}

// Int stores an int value and the
// information if the value has been set
type Int struct {
Expand All @@ -194,13 +187,6 @@ func (v *Int) IsSet() bool {
return v.isSet
}

// Reset sets the Int to it's initial state
// where it is not set and has no value
func (v *Int) Reset() {
v.Val = 0
v.isSet = false
}

// Int64 stores an int64 value and the
// information if the value has been set
type Int64 struct {
Expand All @@ -219,13 +205,6 @@ func (v *Int64) IsSet() bool {
return v.isSet
}

// Reset sets the Int64 to it's initial state
// where it is not set and has no value
func (v *Int64) Reset() {
v.Val = 0
v.isSet = false
}

// Float64 stores a float64 value and the
// information if the value has been set
type Float64 struct {
Expand All @@ -244,13 +223,6 @@ func (v *Float64) IsSet() bool {
return v.isSet
}

// Reset sets the Int to it's initial state
// where it is not set and has no value
func (v *Float64) Reset() {
v.Val = 0.0
v.isSet = false
}

// Bool stores a bool value and the
// information if the value has been set
type Bool struct {
Expand All @@ -269,13 +241,6 @@ func (v *Bool) IsSet() bool {
return v.isSet
}

// Reset sets the Int to it's initial state
// where it is not set and has no value
func (v *Bool) Reset() {
v.Val = false
v.isSet = false
}

// Interface stores an interface{} value and the
// information if the value has been set
//
Expand All @@ -296,13 +261,6 @@ func (v *Interface) IsSet() bool {
return v.isSet
}

// Reset sets the Interface to it's initial state
// where it is not set and has no value
func (v *Interface) Reset() {
v.Val = nil
v.isSet = false
}

type TimeMicrosUnix struct {
Val time.Time
isSet bool
Expand All @@ -319,13 +277,6 @@ func (v *TimeMicrosUnix) IsSet() bool {
return v.isSet
}

// Reset sets the Interface to it's initial state
// where it is not set and has no value
func (v *TimeMicrosUnix) Reset() {
v.Val = time.Time{}
v.isSet = false
}

type HTTPHeader struct {
Val http.Header
isSet bool
Expand All @@ -341,12 +292,3 @@ func (v *HTTPHeader) Set(val http.Header) {
func (v *HTTPHeader) IsSet() bool {
return v.isSet
}

// Reset sets the Interface to it's initial state
// where it is not set and has no value
func (v *HTTPHeader) Reset() {
for k := range v.Val {
delete(v.Val, k)
}
v.isSet = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestString(t *testing.T) {
assert.Equal(t, tc.val, testStruct.S.Val)
}

testStruct.S.Reset()
testStruct.S = String{}
assert.False(t, testStruct.S.IsSet())
assert.Empty(t, testStruct.S.Val)

Expand Down Expand Up @@ -104,7 +104,7 @@ func TestInt(t *testing.T) {
assert.Equal(t, tc.val, testStruct.I.Val)
}

testStruct.I.Reset()
testStruct.I = Int{}
assert.False(t, testStruct.I.IsSet())
assert.Empty(t, testStruct.I.Val)

Expand Down Expand Up @@ -141,7 +141,7 @@ func TestInt64(t *testing.T) {
assert.Equal(t, tc.val, testStruct.I64.Val)
}

testStruct.I64.Reset()
testStruct.I64 = Int64{}
assert.False(t, testStruct.I64.IsSet())
assert.Empty(t, testStruct.I64.Val)

Expand Down Expand Up @@ -179,7 +179,7 @@ func TestFloat64(t *testing.T) {
assert.Equal(t, tc.val, testStruct.F.Val)
}

testStruct.F.Reset()
testStruct.F = Float64{}
assert.False(t, testStruct.F.IsSet())
assert.Empty(t, testStruct.F.Val)

Expand Down Expand Up @@ -217,7 +217,7 @@ func TestBool(t *testing.T) {
assert.Equal(t, tc.val, testStruct.B.Val)
}

testStruct.B.Reset()
testStruct.B = Bool{}
assert.False(t, testStruct.B.IsSet())
assert.Empty(t, testStruct.B.Val)

Expand Down Expand Up @@ -255,7 +255,7 @@ func TestInterface(t *testing.T) {
assert.Equal(t, tc.val, testStruct.V.Val)
}

testStruct.V.Reset()
testStruct.V = Interface{}
assert.False(t, testStruct.V.IsSet())
assert.Empty(t, testStruct.V.Val)
})
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestTimeMicrosUnix(t *testing.T) {
assert.Equal(t, tc.val, testStruct.Tms.Val.String())
}

testStruct.Tms.Reset()
testStruct.Tms = TimeMicrosUnix{}
assert.False(t, testStruct.Tms.IsSet())
assert.Zero(t, testStruct.Tms.Val)

Expand Down Expand Up @@ -347,7 +347,7 @@ func TestHTTPHeader(t *testing.T) {
}
}

testStruct.H.Reset()
testStruct.H = HTTPHeader{}
assert.False(t, testStruct.H.IsSet())
assert.Empty(t, testStruct.H.Val)

Expand Down
6 changes: 3 additions & 3 deletions input/elasticapm/internal/modeldecoder/rumv3/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestDecodeMapToErrorModel(t *testing.T) {
modeldecodertest.SetStructValues(&input, otherVal)
input.Exception.Cause = nil
mapToErrorModel(&input, out)
input.Reset()
input = errorEvent{}

// ensure event Metadata are updated where expected
otherVal = modeldecodertest.NonDefaultValues()
Expand Down Expand Up @@ -146,15 +146,15 @@ func TestDecodeMapToErrorModel(t *testing.T) {
modeldecodertest.SetStructValues(&input, defaultVal)
input.Exception.Cause = nil
mapToErrorModel(&input, &out1)
input.Reset()
input = errorEvent{}
modeldecodertest.AssertStructValues(t, out1.Error, exceptions, defaultVal)

// leave event timestamp unmodified if eventTime is zero
out1.Timestamp = reqTime
modeldecodertest.SetStructValues(&input, defaultVal)
input.Exception.Cause = nil
mapToErrorModel(&input, &out1)
input.Reset()
input = errorEvent{}
modeldecodertest.AssertStructValues(t, out1.Error, exceptions, defaultVal)

// reuse input model for different event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestDecodeMetadataMappingToModel(t *testing.T) {

// map an empty modeldecoder metadata to the model
// and assert values are unchanged
input.Reset()
input = metadata{}
modeldecodertest.SetZeroStructValues(&input)
mapToMetadataModel(&input, out)
assert.Equal(t, expected(otherVal.Str, otherVal.IP, otherVal.N), out)
Expand All @@ -243,7 +243,7 @@ func TestDecodeMetadataMappingToModel(t *testing.T) {
// overwrite model metadata with specified Values
// then iterate through model and assert values are overwritten
otherVal := modeldecodertest.NonDefaultValues()
input.Reset()
input = metadata{}
modeldecodertest.SetStructValues(&input, otherVal)
mapToMetadataModel(&input, &out2)
out2.UserAgent = &modelpb.UserAgent{Name: "init", Original: "init"}
Expand Down
Loading