on main help sign in
fix: simplify stuff
3135b6fCarlos Alexandro Becker
.github/workflows/schema-update.yml+1−1
@@ -16,7 +16,7 @@ jobs:
16 - uses: actions/setup-go@v5
16 - uses: actions/setup-go@v5
17 with:
17 with:
18 go-version-file: go.mod
18 go-version-file: go.mod
19− - run: go run . schema > .github/crush-schema.json
19+ - run: go run . schema > ./schema.json
20 - uses: stefanzweifel/git-auto-commit-action@778341af668090896ca464160c2def5d1d1a3eb0 # v5
20 - uses: stefanzweifel/git-auto-commit-action@778341af668090896ca464160c2def5d1d1a3eb0 # v5
21 with:
21 with:
22 commit_message: "chore: auto-update generated files"
22 commit_message: "chore: auto-update generated files"
internal/cmd/schema.go+3−44
@@ -3,7 +3,6 @@ package cmd
3import (
3import (
4 "encoding/json"
4 "encoding/json"
5 "fmt"
5 "fmt"
6− "reflect"
7
6
8 "github.com/charmbracelet/crush/internal/config"
7 "github.com/charmbracelet/crush/internal/config"
9 "github.com/invopop/jsonschema"
8 "github.com/invopop/jsonschema"
@@ -16,52 +15,12 @@ var schemaCmd = &cobra.Command{
16 Long: "Generate JSON schema for the crush configuration file",
15 Long: "Generate JSON schema for the crush configuration file",
17 Hidden: true,
16 Hidden: true,
18 RunE: func(cmd *cobra.Command, args []string) error {
17 RunE: func(cmd *cobra.Command, args []string) error {
19− reflector := jsonschema.Reflector{
18+ reflector := new(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, "", " ")
19+ bts, err := json.MarshalIndent(reflector.Reflect(&config.Config{}), "", " ")
60 if err != nil {
20 if err != nil {
61 return fmt.Errorf("failed to marshal schema: %w", err)
21 return fmt.Errorf("failed to marshal schema: %w", err)
62 }
22 }
63−
64− fmt.Println(string(schemaJSON))
23+ fmt.Println(string(bts))
65 return nil
24 return nil
66 },
25 },
67}
26}
internal/csync/maps.go+5
@@ -96,6 +96,11 @@ var (
96 _ json.Marshaler = &Map[string, any]{}
96 _ json.Marshaler = &Map[string, any]{}
97)
97)
98
98
99+func (Map[K, V]) JSONSchemaAlias() any { //nolint
100+ m := map[K]V{}
101+ return m
102+}
103+
99// UnmarshalJSON implements json.Unmarshaler.
104// UnmarshalJSON implements json.Unmarshaler.
100func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
105func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
101 m.mu.Lock()
106 m.mu.Lock()
.github/crush-schema.json → schema.jsonrenamed

The content is the same.