-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,351 additions
and
944 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/atombender/go-jsonschema/pkg/generator" | ||
"github.com/atombender/go-jsonschema/pkg/schemas" | ||
) | ||
|
||
const ( | ||
//TODO: Get rid of this? | ||
CONFIG_NAME = "config" | ||
) | ||
|
||
// GenerateConfig generates a "config.go", as well as any other Go files which "config.go" depends on. | ||
// The inputs are: | ||
// * "goPkgName" is the Go package at the top of the "config.go" file. For example, "batchprocessor". | ||
// * "dir" is the location where the "config.go" file will be written. For example, "./processor/batchprocessor". | ||
// * "conf" is the schema for "config.go". It is a "map[string]any". | ||
// | ||
// The output is a map, where: | ||
// * The key is the absolute path to the file which must be written. | ||
// * The value is the content of the file. | ||
func GenerateConfig(goPkgName string, dir string, conf any) (map[string]string, error) { | ||
// load config | ||
jsonBytes, err := json.Marshal(conf) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed loading config %w", err) | ||
} | ||
var schema schemas.Schema | ||
if err := json.Unmarshal(jsonBytes, &schema); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal JSON: %w", err) | ||
} | ||
|
||
// defaultOutputName := "config.go" | ||
defaultOutputDir, err := filepath.Abs(dir) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get absolute path for %s: %w", dir, err) | ||
} | ||
defaultOutputNameDest := filepath.Join(defaultOutputDir, "config.go") | ||
|
||
// TODO: Make this configurable? | ||
repoRootDir := "../../" | ||
|
||
// TODO: Make this configurable. Or find a way to get rid of this mapping? | ||
schemaMappings := []generator.SchemaMapping{ | ||
generator.SchemaMapping{ | ||
SchemaID: "opentelemetry.io/collector/exporter/exporterhelper/queue_sender", | ||
PackageName: "go.opentelemetry.io/collector/exporter/exporterhelper", | ||
OutputName: "./exporter/exporterhelper/queue_sender.go", | ||
}, | ||
generator.SchemaMapping{ | ||
SchemaID: "opentelemetry.io/collector/config/configretry/backoff/retry_on_failure", | ||
PackageName: "go.opentelemetry.io/collector/config/configretry", | ||
OutputName: "./config/configretry/backoff.go", | ||
}, | ||
generator.SchemaMapping{ | ||
SchemaID: "opentelemetry.io/collector/config/configtelemetry/configtelemetry", | ||
PackageName: "go.opentelemetry.io/collector/config/configtelemetry", | ||
OutputName: "./config/configtelemetry/configtelemetry.go", | ||
}, | ||
generator.SchemaMapping{ | ||
SchemaID: "opentelemetry.io/collector/config/confighttp/confighttp", | ||
PackageName: "go.opentelemetry.io/collector/config/confighttp", | ||
OutputName: "./config/confighttp/confighttp.go", | ||
}, | ||
generator.SchemaMapping{ | ||
SchemaID: "opentelemetry.io/collector/exporter/exporterhelper/timeout_sender", | ||
PackageName: "go.opentelemetry.io/collector/exporter/exporterhelper", | ||
OutputName: "./exporter/exporterhelper/timeout_sender.go", | ||
}, | ||
} | ||
for i := range schemaMappings { | ||
if schemaMappings[i].OutputName != "" { | ||
// The file paths in the schema mappings are relative to the repo root. | ||
// Make the paths absolute. | ||
relFilePath := filepath.Clean(filepath.Join(repoRootDir, schemaMappings[i].OutputName)) | ||
absFilePath, err := filepath.Abs(relFilePath) | ||
absFilePath = filepath.Clean(absFilePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get absolute path for %s: %w", schemaMappings[i].OutputName, err) | ||
} | ||
schemaMappings[i].OutputName = absFilePath | ||
} | ||
} | ||
|
||
// init generator | ||
cfg := generator.Config{ | ||
Warner: func(message string) { | ||
logf("Warning: %s", message) | ||
}, | ||
DefaultPackageName: goPkgName, | ||
DefaultOutputName: defaultOutputNameDest, | ||
StructNameFromTitle: true, | ||
Tags: []string{"mapstructure"}, | ||
SchemaMappings: schemaMappings, | ||
YAMLExtensions: []string{".yaml", ".yml"}, | ||
// YAMLPackage: "gopkg.in/yaml.v3", | ||
} | ||
|
||
generator, err := generator.New(cfg) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create generator: %w", err) | ||
} | ||
if err = generator.AddFile(CONFIG_NAME, &schema); err != nil { | ||
return nil, fmt.Errorf("failed to add config: %w", err) | ||
} | ||
|
||
// hasUnsupportedValidations := len(generator.NotSupportedValidations) > 0 | ||
|
||
// tplVars := struct { | ||
// ValidatorFuncName string | ||
// }{ | ||
// ValidatorFuncName: "Validate", | ||
// } | ||
// if hasUnsupportedValidations { | ||
// tplVars.ValidatorFuncName = "ValidateHelper" | ||
// } | ||
|
||
// tpl := ` | ||
// func (cfg *Config){{.ValidatorFuncName}}() error { | ||
// b, err := json.Marshal(cfg) | ||
// if err != nil { | ||
// return err | ||
// } | ||
// var config Config | ||
// if err := json.Unmarshal(b, &config); err != nil { | ||
// return err | ||
// } | ||
// return nil | ||
// }` | ||
// tmpl, err := template.New("validator").Parse(tpl) | ||
// if err != nil { | ||
// return fmt.Errorf("failed to parse template: %w", err) | ||
// } | ||
|
||
output := make(map[string]string) | ||
for sourceName, source := range generator.Sources() { | ||
fmt.Printf("Writing to %s\n", sourceName) | ||
// buf := bytes.NewBufferString("") | ||
// if err = tmpl.Execute(buf, tplVars); err != nil { | ||
// return fmt.Errorf("failed to execute template: %w", err) | ||
// } | ||
// only write custom validation if there are no unsupported validations | ||
// source = append(source, []byte(tpl)...) | ||
// source = append(source, buf.Bytes()...) | ||
// output[sourceName] = string(source) | ||
output[sourceName] = string(source) | ||
} | ||
fmt.Println("done") | ||
return output, nil | ||
} | ||
|
||
func logf(format string, args ...interface{}) { | ||
fmt.Fprint(os.Stderr, "go-jsonschema: ") | ||
fmt.Fprintf(os.Stderr, format, args...) | ||
fmt.Fprint(os.Stderr, "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TODO: This test doesn't compile due to recent refactoring. Fix it. | ||
func TestXxx(t *testing.T) { | ||
inputDir := `./testdata/config_gen/input_schema` | ||
outputDir := `./testdata/config_gen/expected_golang_output/` | ||
|
||
inputFiles, err := os.ReadDir(inputDir) | ||
require.NoError(t, err) | ||
|
||
for _, inputFile := range inputFiles { | ||
if inputFile.IsDir() { | ||
continue | ||
} | ||
|
||
md, err := loadMetadata(filepath.Join(inputDir, inputFile.Name())) | ||
require.NoError(t, err) | ||
|
||
err = GenerateConfig(md.Config, buf) | ||
require.NoError(t, err) | ||
|
||
actual := buf.String() | ||
|
||
expectedOutputFile := filepath.Join(outputDir, inputFile.Name()) | ||
var expectedOutput []byte | ||
expectedOutput, err = os.ReadFile(expectedOutputFile) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, string(expectedOutput), actual, "actual", actual) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.