1010package manifest
1111
1212import (
13+ "bytes"
1314 _ "embed"
1415 "encoding/json"
1516 "fmt"
17+ "strings"
1618
17- "github.com/santhosh-tekuri/jsonschema/v5 "
19+ "github.com/santhosh-tekuri/jsonschema/v6 "
1820 "github.com/tidwall/gjson"
1921 "github.com/tidwall/jsonc"
2022)
@@ -28,6 +30,7 @@ const currentVersion = 3
2830
2931//go:embed modus_schema.json
3032var schemaContent string
33+ var schema * jsonschema.Schema
3134
3235type Manifest struct {
3336 Version int `json:"-"`
@@ -54,34 +57,43 @@ func (m *Manifest) GetVariables() map[string][]string {
5457 return results
5558}
5659
60+ func init () {
61+ doc , err := jsonschema .UnmarshalJSON (strings .NewReader (schemaContent ))
62+ if err != nil {
63+ panic (fmt .Errorf ("failed to parse manifest schema: %w" , err ))
64+ }
65+
66+ c := jsonschema .NewCompiler ()
67+ if err := c .AddResource ("modus.json" , doc ); err != nil {
68+ panic (fmt .Errorf ("failed to add manifest schema: %w" , err ))
69+ }
70+
71+ if sch , err := c .Compile ("modus.json" ); err != nil {
72+ panic (fmt .Errorf ("failed to compile manifest schema: %w" , err ))
73+ } else {
74+ schema = sch
75+ }
76+ }
77+
5778func IsCurrentVersion (version int ) bool {
5879 return version == currentVersion
5980}
6081
6182func ValidateManifest (content []byte ) error {
62-
63- sch , err := jsonschema .CompileString ( "modus.json" , schemaContent )
83+ r := bytes . NewReader ( jsonc . ToJSON ( content ))
84+ doc , err := jsonschema .UnmarshalJSON ( r )
6485 if err != nil {
65- return err
86+ return fmt . Errorf ( "failed to parse manifest: %w" , err )
6687 }
6788
68- content = jsonc .ToJSONInPlace (content )
69-
70- var v interface {}
71- if err := json .Unmarshal (content , & v ); err != nil {
72- return fmt .Errorf ("failed to deserialize manifest: %w" , err )
73- }
74- if err := sch .Validate (v ); err != nil {
89+ if err := schema .Validate (doc ); err != nil {
7590 return fmt .Errorf ("failed to validate manifest: %w" , err )
7691 }
7792
7893 return nil
7994}
8095
8196func ReadManifest (content []byte ) (* Manifest , error ) {
82-
83- content = jsonc .ToJSONInPlace (content )
84-
8597 var manifest Manifest
8698 if err := parseManifestJson (content , & manifest ); err != nil {
8799 return nil , fmt .Errorf ("failed to parse manifest: %w" , err )
@@ -96,7 +108,7 @@ func parseManifestJson(data []byte, manifest *Manifest) error {
96108 Connections map [string ]json.RawMessage `json:"connections"`
97109 Collections map [string ]CollectionInfo `json:"collections"`
98110 }
99- if err := json .Unmarshal (data , & m ); err != nil {
111+ if err := json .Unmarshal (jsonc . ToJSON ( data ) , & m ); err != nil {
100112 return fmt .Errorf ("failed to parse manifest: %w" , err )
101113 }
102114
0 commit comments