Skip to content

Commit

Permalink
Fix missing attribute sent along with the data (#40)
Browse files Browse the repository at this point in the history
Problem:

F5 telemetry service requires the dataType attribute present, which
must be equal to the `@df_datatype` annotation in the scheme.

Solution:

Add dataType attribute to the generated Attributes() method.

Note: the attribute only required for the struct for which we generate
the scheme. In the Attribute() method of any embedded structs,
dataType attribute must not be set.

CLOSES #39
  • Loading branch information
pleshakov authored Mar 7, 2024
1 parent e74e64e commit a5ecce5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
15 changes: 11 additions & 4 deletions cmd/generator/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import (
func (d *{{ .StructName }}) Attributes() []attribute.KeyValue {
var attrs []attribute.KeyValue
{{- if .SchemeDataType }}
attrs = append(attrs, attribute.String("dataType", "{{ .SchemeDataType }}"))
{{ end }}
{{ range .Fields -}}
attrs = append(attrs, {{ .AttributesSource }})
{{ end }}
Expand All @@ -49,6 +53,7 @@ type codeGen struct {
TelemetryPackageAlias string
ExportablePackagePrefix string
StructName string
SchemeDataType string
BuildTags string
Fields []codeField
}
Expand All @@ -73,10 +78,11 @@ func getAttributeType(kind types.BasicKind) string {
}

type codeGenConfig struct {
packagePath string
typeName string
buildTags string
fields []field
packagePath string
typeName string
schemeDataType string
buildTags string
fields []field
}

func generateCode(writer io.Writer, cfg codeGenConfig) error {
Expand Down Expand Up @@ -129,6 +135,7 @@ func generateCode(writer io.Writer, cfg codeGenConfig) error {
TelemetryPackageAlias: telemetryPkgAlias,
TelemetryPackagePath: telemetryPkg,
StructName: cfg.typeName,
SchemeDataType: cfg.schemeDataType,
Fields: codeFields,
BuildTags: cfg.buildTags,
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ func main() {
}

codeCfg := codeGenConfig{
packagePath: result.packagePath,
typeName: *typeName,
fields: result.fields,
buildTags: codeGenBuildTags,
packagePath: result.packagePath,
typeName: *typeName,
schemeDataType: *schemeDataFabricDataType,
fields: result.fields,
buildTags: codeGenBuildTags,
}

if err := generateCode(file, codeCfg); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/generator/tests/data_attributes_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

func (d *Data) Attributes() []attribute.KeyValue {
var attrs []attribute.KeyValue
attrs = append(attrs, attribute.String("dataType", "ngf-product-telemetry"))


attrs = append(attrs, attribute.String("SomeString", d.SomeString))
attrs = append(attrs, attribute.Int64("SomeInt", d.SomeInt))
Expand Down
2 changes: 2 additions & 0 deletions cmd/generator/tests/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestData_Attributes(t *testing.T) {
}

expectedAttributes := []attribute.KeyValue{
attribute.String("dataType", "ngf-product-telemetry"),
attribute.String("SomeString", "some string"),
attribute.Int64("SomeInt", 42),
attribute.Float64("SomeFloat", 3.14),
Expand Down Expand Up @@ -65,6 +66,7 @@ func TestData_AttributesEmpty(t *testing.T) {
data := Data{}

expectedAttributes := []attribute.KeyValue{
attribute.String("dataType", "ngf-product-telemetry"),
attribute.String("SomeString", ""),
attribute.Int64("SomeInt", 0),
attribute.Float64("SomeFloat", 0),
Expand Down

0 comments on commit a5ecce5

Please sign in to comment.