on main help sign in
fix: simplify stuff
commit3135b6fa4db00e95f8c17dbec9037875d4b2a88f authorCarlos Alexandro Becker <caarlos0@users.noreply.github.com> dateMon 28 Jul 2025, 22:44 (1 year ago) parent9bf42ce
4 files changed+9−45
.github/workflows/schema-update.yml+1−1 internal/cmd/schema.go+3−44 internal/csync/maps.go+5 .github/crush-schema.json → schema.json
.github/workflows/schema-update.yml+1−1
@@ -16,7 +16,7 @@ jobs:
1616 - uses: actions/setup-go@v5
1717 with:
1818 go-version-file: go.mod
19− - run: go run . schema > .github/crush-schema.json
19+ - run: go run . schema > ./schema.json
2020 - uses: stefanzweifel/git-auto-commit-action@778341af668090896ca464160c2def5d1d1a3eb0 # v5
2121 with:
2222 commit_message: "chore: auto-update generated files"
internal/cmd/schema.go+3−44
@@ -3,7 +3,6 @@ package cmd
33import (
44 "encoding/json"
55 "fmt"
6− "reflect"
76
87 "github.com/charmbracelet/crush/internal/config"
98 "github.com/invopop/jsonschema"
@@ -16,52 +15,12 @@ var schemaCmd = &cobra.Command{
1615 Long: "Generate JSON schema for the crush configuration file",
1716 Hidden: true,
1817 RunE: func(cmd *cobra.Command, args []string) error {
19− reflector := jsonschema.Reflector{
20− // Custom type mapper to handle csync.Map
21− Mapper: func(t reflect.Type) *jsonschema.Schema {
22− // Handle csync.Map[string, ProviderConfig] specifically
23− if t.String() == "csync.Map[string,github.com/charmbracelet/crush/internal/config.ProviderConfig]" {
24− return &jsonschema.Schema{
25− Type: "object",
26− Description: "AI provider configurations",
27− AdditionalProperties: &jsonschema.Schema{
28− Ref: "#/$defs/ProviderConfig",
29− },
30− }
31− }
32− return nil
33− },
34− }
35−
36− // First reflect the config to get the main schema
37− schema := reflector.Reflect(&config.Config{})
38−
39− // Now manually add the ProviderConfig definition that might be missing
40− providerConfigSchema := reflector.ReflectFromType(reflect.TypeOf(config.ProviderConfig{}))
41− if schema.Definitions == nil {
42− schema.Definitions = make(map[string]*jsonschema.Schema)
43− }
44−
45− // Extract the actual definition from the nested schema
46− if providerConfigSchema.Definitions != nil && providerConfigSchema.Definitions["ProviderConfig"] != nil {
47− schema.Definitions["ProviderConfig"] = providerConfigSchema.Definitions["ProviderConfig"]
48− // Also add any other definitions from the provider config schema
49− for k, v := range providerConfigSchema.Definitions {
50− if k != "ProviderConfig" {
51− schema.Definitions[k] = v
52− }
53− }
54− } else {
55− // Fallback: use the schema itself if it's not nested
56− schema.Definitions["ProviderConfig"] = providerConfigSchema
57− }
58−
59− schemaJSON, err := json.MarshalIndent(schema, "", " ")
18+ reflector := new(jsonschema.Reflector)
19+ bts, err := json.MarshalIndent(reflector.Reflect(&config.Config{}), "", " ")
6020 if err != nil {
6121 return fmt.Errorf("failed to marshal schema: %w", err)
6222 }
63−
64− fmt.Println(string(schemaJSON))
23+ fmt.Println(string(bts))
6524 return nil
6625 },
6726}
internal/csync/maps.go+5
@@ -96,6 +96,11 @@ var (
9696 _ json.Marshaler = &Map[string, any]{}
9797)
9898
99+func (Map[K, V]) JSONSchemaAlias() any { //nolint
100+ m := map[K]V{}
101+ return m
102+}
103+
99104// UnmarshalJSON implements json.Unmarshaler.
100105func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
101106 m.mu.Lock()
.github/crush-schema.json → schema.jsonrenamed

The content is the same.