Skip to content

Commit bea8e30

Browse files
committed
Use yaml instead of JSON
Signed-off-by: Pavol Loffay <[email protected]>
1 parent f190d98 commit bea8e30

File tree

12 files changed

+625
-682
lines changed

12 files changed

+625
-682
lines changed

cmd/mdatagen/internal/samplereceiver/generated_component_test.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/generated_package_test.go

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/internal/metadata/schemas/config_schema.json

Lines changed: 0 additions & 114 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
$schema: https://json-schema.org/draft/2020-12/schema
2+
title: sample receiver configuration
3+
type: object
4+
properties:
5+
api_key:
6+
description: APIKey is a secret API key (opaque string).
7+
type: string
8+
batch_size:
9+
description: BatchSize is the number of items to send in each batch.
10+
type: integer
11+
custom_string:
12+
description: CustomString is a custom string.
13+
type: string
14+
enabled:
15+
description: Enabled controls whether the exporter is active.
16+
type: boolean
17+
endpoint:
18+
description: Endpoint is the target URL to send data to.
19+
type: string
20+
endpoints:
21+
description: Endpoints is a list of endpoint configurations.
22+
type: array
23+
items:
24+
type: object
25+
properties:
26+
priority:
27+
description: Priority is the endpoint priority.
28+
type: integer
29+
url:
30+
description: URL is the endpoint URL.
31+
type: string
32+
headers:
33+
description: Headers are additional headers to include in requests.
34+
type: object
35+
additionalProperties:
36+
type: string
37+
host:
38+
description: Host is the network host.
39+
type: string
40+
id:
41+
description: ID is the component identifier.
42+
type: string
43+
optional_retry:
44+
description: OptionalRetry is an optional retry configuration.
45+
type: object
46+
properties:
47+
initial_interval:
48+
description: InitialInterval is the initial retry interval.
49+
type: string
50+
pattern: ^(0|[-+]?((\d+(\.\d*)?|\.\d+)(ns|us|µs|μs|ms|s|m|h))+)$
51+
max_retries:
52+
description: MaxRetries is the maximum number of retries.
53+
type: integer
54+
port:
55+
description: Port is the network port.
56+
type: integer
57+
retry:
58+
description: Retry contains retry configuration.
59+
type: object
60+
properties:
61+
initial_interval:
62+
description: InitialInterval is the initial retry interval.
63+
type: string
64+
pattern: ^(0|[-+]?((\d+(\.\d*)?|\.\d+)(ns|us|µs|μs|ms|s|m|h))+)$
65+
max_retries:
66+
description: MaxRetries is the maximum number of retries.
67+
type: integer
68+
secrets:
69+
description: Secrets is a list of secret key-value pairs.
70+
type: object
71+
start_time:
72+
description: StartTime is the time when the receiver should start collecting data.
73+
type: string
74+
format: date-time
75+
tags:
76+
description: Tags are optional tags to attach.
77+
type: array
78+
items:
79+
type: string
80+
timeout:
81+
description: Timeout is the maximum time to wait for a response.
82+
type: string
83+
pattern: ^(0|[-+]?((\d+(\.\d*)?|\.\d+)(ns|us|µs|μs|ms|s|m|h))+)$

cmd/mdatagen/internal/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal"
55

6-
// SchemaConfig holds configuration for JSON schema generation.
6+
// SchemaConfig holds configuration for YAML schema generation.
77
type SchemaConfig struct {
88
// Enabled controls whether schema generation is enabled (default: false).
99
Enabled bool `mapstructure:"enabled"`

cmd/mdatagen/internal/schemagen/generator.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
package schemagen // import "go.opentelemetry.io/collector/cmd/mdatagen/internal/schemagen"
55

66
import (
7-
"encoding/json"
87
"fmt"
98
"maps"
109
"os"
1110
"path/filepath"
1211
"strings"
12+
13+
"gopkg.in/yaml.v3"
1314
)
1415

1516
const (
@@ -37,7 +38,7 @@ func NewSchemaGenerator(outputDir string, analyzer *PackageAnalyzer) *SchemaGene
3738
}
3839
}
3940

40-
// GenerateSchema generates a JSON schema for the component's config.
41+
// GenerateSchema generates a YAML schema for the component's config.
4142
func (g *SchemaGenerator) GenerateSchema(componentKind, componentName, configTypeName string) error {
4243
structInfo, err := g.analyzer.analyzeConfig(configTypeName)
4344
if err != nil {
@@ -52,15 +53,12 @@ func (g *SchemaGenerator) GenerateSchema(componentKind, componentName, configTyp
5253
}
5354

5455
// Write schema to file
55-
outputPath := filepath.Join(g.outputDir, "config_schema.json")
56-
data, err := json.MarshalIndent(schema, "", " ")
56+
outputPath := filepath.Join(g.outputDir, "config_schema.yaml")
57+
data, err := yaml.Marshal(schema)
5758
if err != nil {
5859
return fmt.Errorf("failed to marshal schema: %w", err)
5960
}
6061

61-
// Add trailing newline
62-
data = append(data, '\n')
63-
6462
if err := os.WriteFile(outputPath, data, 0o600); err != nil {
6563
return fmt.Errorf("failed to write schema: %w", err)
6664
}

cmd/mdatagen/internal/schemagen/generator_test.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestSchemaGenerator_GenerateSchema(t *testing.T) {
2727
require.NoError(t, err)
2828

2929
// Check that schema file was created
30-
schemaPath := filepath.Join(outputDir, "config_schema.json")
30+
schemaPath := filepath.Join(outputDir, "config_schema.yaml")
3131
_, err = os.Stat(schemaPath)
3232
require.NoError(t, err, "schema file was not created")
3333

@@ -36,12 +36,21 @@ func TestSchemaGenerator_GenerateSchema(t *testing.T) {
3636
require.NoError(t, err)
3737

3838
// Read the expected schema from testdata
39-
expectedPath := filepath.Join("testdata", "config_schema.json")
39+
expectedPath := filepath.Join("testdata", "config_schema.yaml")
4040
expectedData, err := os.ReadFile(expectedPath) //#nosec G304 -- test file path
4141
require.NoError(t, err)
4242

43-
// Compare JSON content (ignoring formatting differences)
44-
assert.JSONEq(t, string(expectedData), string(generatedData))
43+
// Compare YAML content by parsing both and comparing as JSON
44+
var generatedSchema, expectedSchema any
45+
require.NoError(t, yaml.Unmarshal(generatedData, &generatedSchema))
46+
require.NoError(t, yaml.Unmarshal(expectedData, &expectedSchema))
47+
48+
generatedJSON, err := json.Marshal(generatedSchema)
49+
require.NoError(t, err)
50+
expectedJSON, err := json.Marshal(expectedSchema)
51+
require.NoError(t, err)
52+
53+
assert.JSONEq(t, string(expectedJSON), string(generatedJSON))
4554
}
4655

4756
func TestParseTag(t *testing.T) {
@@ -138,14 +147,20 @@ func TestSetSchemaType(t *testing.T) {
138147
}
139148

140149
func TestSchemaValidation(t *testing.T) {
141-
// Load the JSON schema
142-
schemaPath := filepath.Join("testdata", "config_schema.json")
150+
// Load the YAML schema
151+
schemaPath := filepath.Join("testdata", "config_schema.yaml")
143152
schemaData, err := os.ReadFile(schemaPath)
144153
require.NoError(t, err, "failed to read schema file")
145154

146-
// Parse the schema JSON
155+
// Parse the schema YAML and convert to JSON-compatible format
147156
var schemaDoc any
148-
err = json.Unmarshal(schemaData, &schemaDoc)
157+
err = yaml.Unmarshal(schemaData, &schemaDoc)
158+
require.NoError(t, err, "failed to parse schema YAML")
159+
160+
// Convert to JSON and back to ensure JSON-compatible types
161+
jsonBytes, err := json.Marshal(schemaDoc)
162+
require.NoError(t, err, "failed to convert schema to JSON")
163+
err = json.Unmarshal(jsonBytes, &schemaDoc)
149164
require.NoError(t, err, "failed to parse schema JSON")
150165

151166
// Compile the schema

0 commit comments

Comments
 (0)