fix: simplify stuff

This commit is contained in:
Carlos Alexandro Becker
2025-07-28 22:44:20 -03:00
parent 9bf42ced99
commit 3135b6fa4d
4 changed files with 9 additions and 45 deletions

View File

@@ -16,7 +16,7 @@ jobs:
- uses: actions/setup-go@v5 - uses: actions/setup-go@v5
with: with:
go-version-file: go.mod go-version-file: go.mod
- run: go run . schema > .github/crush-schema.json - run: go run . schema > ./schema.json
- uses: stefanzweifel/git-auto-commit-action@778341af668090896ca464160c2def5d1d1a3eb0 # v5 - uses: stefanzweifel/git-auto-commit-action@778341af668090896ca464160c2def5d1d1a3eb0 # v5
with: with:
commit_message: "chore: auto-update generated files" commit_message: "chore: auto-update generated files"

View File

@@ -3,7 +3,6 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"reflect"
"github.com/charmbracelet/crush/internal/config" "github.com/charmbracelet/crush/internal/config"
"github.com/invopop/jsonschema" "github.com/invopop/jsonschema"
@@ -16,52 +15,12 @@ var schemaCmd = &cobra.Command{
Long: "Generate JSON schema for the crush configuration file", Long: "Generate JSON schema for the crush configuration file",
Hidden: true, Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
reflector := jsonschema.Reflector{ reflector := new(jsonschema.Reflector)
// Custom type mapper to handle csync.Map bts, err := json.MarshalIndent(reflector.Reflect(&config.Config{}), "", " ")
Mapper: func(t reflect.Type) *jsonschema.Schema {
// Handle csync.Map[string, ProviderConfig] specifically
if t.String() == "csync.Map[string,github.com/charmbracelet/crush/internal/config.ProviderConfig]" {
return &jsonschema.Schema{
Type: "object",
Description: "AI provider configurations",
AdditionalProperties: &jsonschema.Schema{
Ref: "#/$defs/ProviderConfig",
},
}
}
return nil
},
}
// First reflect the config to get the main schema
schema := reflector.Reflect(&config.Config{})
// Now manually add the ProviderConfig definition that might be missing
providerConfigSchema := reflector.ReflectFromType(reflect.TypeOf(config.ProviderConfig{}))
if schema.Definitions == nil {
schema.Definitions = make(map[string]*jsonschema.Schema)
}
// Extract the actual definition from the nested schema
if providerConfigSchema.Definitions != nil && providerConfigSchema.Definitions["ProviderConfig"] != nil {
schema.Definitions["ProviderConfig"] = providerConfigSchema.Definitions["ProviderConfig"]
// Also add any other definitions from the provider config schema
for k, v := range providerConfigSchema.Definitions {
if k != "ProviderConfig" {
schema.Definitions[k] = v
}
}
} else {
// Fallback: use the schema itself if it's not nested
schema.Definitions["ProviderConfig"] = providerConfigSchema
}
schemaJSON, err := json.MarshalIndent(schema, "", " ")
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal schema: %w", err) return fmt.Errorf("failed to marshal schema: %w", err)
} }
fmt.Println(string(bts))
fmt.Println(string(schemaJSON))
return nil return nil
}, },
} }

View File

@@ -96,6 +96,11 @@ var (
_ json.Marshaler = &Map[string, any]{} _ json.Marshaler = &Map[string, any]{}
) )
func (Map[K, V]) JSONSchemaAlias() any { //nolint
m := map[K]V{}
return m
}
// UnmarshalJSON implements json.Unmarshaler. // UnmarshalJSON implements json.Unmarshaler.
func (m *Map[K, V]) UnmarshalJSON(data []byte) error { func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
m.mu.Lock() m.mu.Lock()