@@ -6,48 +6,48 @@ import (
66 "os"
77)
88
9- func findPropRefs (schemaFile string ) {
10- // Command-line argument to implement
11- // xsdFile := flag.String("xsd", "b2mml-batchml/B2MML-ConfirmBOD .xsd", "XSD file to parse")
12- // jsonFile := flag.String("json", "b2mml-batchml/Schema/v2.0.0.base.schema.json", "JSON file to parse")
13- // outputFile := flag.String("output", "testFile.json", "Output file name")
14- // flag.Parse()
9+ // After converting an XSD file to a JSON file, this script adds all references found
10+ // within the specified base file to the designated schema file.
11+ func findPropRefs (schemaFile string , baseFile string ) (int , error ) {
1512
16- // schemaFile := "/Users/mattprincev/Documents/Rhize/JSON Schema/b2mml-batchml/errorTest.json"
17- baseFile := "/Users/mattprincev/Documents/Rhize/JSON Schema/b2mml-batchml/schemas/v2.0.0.batchInformation.schema.json"
18-
19- // Get the list of names from the XSD file
13+ // Gets the list of names from the XSD file
2014 propNames , err := getPropNames (schemaFile )
2115 if err != nil {
22- fmt .Println ("Error parsing schema file:" , err )
23- return
16+ return fmt .Println ("Error parsing schema file:" , err )
2417 }
2518
19+ // Gets the name of the references needed to search for in the basefile
2620 baseRefs , err := getBaseRefs (propNames , baseFile )
2721 if err != nil {
28- fmt .Println ("Error getting base references:" , err )
29- return
22+ return fmt .Println ("Error getting base references:" , err )
3023 }
3124
25+ // Adds each instance of a reference to the definition
3226 err = addDefs (baseRefs , schemaFile )
3327 if err != nil {
34- fmt .Println ("Error adding definitions:" , err )
35- return
28+ return fmt .Println ("Error adding definitions:" , err )
3629 }
30+
31+ return 0 , nil
3732}
3833
34+ // Reads the property names that need to be searched for in the base file
3935func getPropNames (jsonFile string ) (map [string ]interface {}, error ) {
36+
37+ // Read the inputted schema file
4038 data , err := os .ReadFile (jsonFile )
4139 if err != nil {
4240 return nil , fmt .Errorf ("error reading JSON file: %v" , err )
4341 }
4442
43+ // unmarshal the read data into the JSON struct
4544 var jsonData JSONStructure
4645 if err := json .Unmarshal (data , & jsonData ); err != nil {
4746 return nil , fmt .Errorf ("error unmarshalling JSON data: %v" , err )
4847 }
4948
50- var refProps = make (map [string ]interface {}) // Initialize the map
49+ // Initialize the map
50+ var refProps = make (map [string ]interface {})
5151
5252 for key , value := range jsonData .Properties {
5353 refProps [key ] = value
@@ -56,7 +56,9 @@ func getPropNames(jsonFile string) (map[string]interface{}, error) {
5656 return refProps , nil
5757}
5858
59+ // Gets the property names from the base schema and adds them to the json structure
5960func getBaseRefs (propNames map [string ]interface {}, baseSchema string ) (map [string ]interface {}, error ) {
61+
6062 data , err := os .ReadFile (baseSchema )
6163 if err != nil {
6264 return nil , fmt .Errorf ("error reading JSON file: %v" , err )
@@ -79,7 +81,9 @@ func getBaseRefs(propNames map[string]interface{}, baseSchema string) (map[strin
7981 return propNames , nil
8082}
8183
84+ // Adds the definitions found from the base file into the inputted schema file
8285func addDefs (definitions map [string ]interface {}, schemaFile string ) error {
86+
8387 data , err := os .ReadFile (schemaFile )
8488 if err != nil {
8589 return fmt .Errorf ("error reading JSON file: %v" , err )
@@ -90,7 +94,6 @@ func addDefs(definitions map[string]interface{}, schemaFile string) error {
9094 return fmt .Errorf ("error unmarshalling JSON data: %v" , err )
9195 }
9296
93- // var refProps = make(map[string]interface{})
9497 for key , value := range definitions {
9598 jsonData .Defs [key ] = value
9699 }
@@ -100,7 +103,7 @@ func addDefs(definitions map[string]interface{}, schemaFile string) error {
100103 return fmt .Errorf ("error marshalling JSON data: %v" , err )
101104 }
102105
103- // Step 5: Write the updated JSON content back to the file
106+ // Write the updated JSON content back to the file
104107 if err := os .WriteFile (schemaFile , updatedContent , 0644 ); err != nil {
105108 return fmt .Errorf ("error writing to file: %v" , err )
106109 }
0 commit comments