Skip to content

Commit af0bbda

Browse files
Merge branch 'develop_consolidation' into 'develop'
v2.0.0 See merge request libremfg/b2mml-batchml!5
2 parents 642e4bb + 27d1ef4 commit af0bbda

8 files changed

Lines changed: 153 additions & 88 deletions

File tree

Docs/content/posts/version-v1.0.0.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
+++
2-
title = 'Version v1.0.0'
2+
title = 'Version 1.0.0'
33
date = 2024-07-12T18:43:31Z
44
+++
55

Docs/content/posts/version-v2.0.0.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Based on the works of https://github.com/MESAInternational/B2MML-BatchML.
3131
Start out by importing the schema and using it in your JSON documents.
3232

3333
```
34-
"$schema": "{{< siteurl >}}schemas"
34+
"$schema": "{{< siteurl >}}schemas/"
3535
```
3636

3737
Here is an example using the JSON schema in a `NotifyWorkDefined` message.

scripts/baseToDt.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,38 @@ import (
88

99
// This is a very specific script that took all the definitions within v2.0.0.dataType.schema.json and checked if it existed within the base file
1010
// if it existed (which they all do) then it will copy the reference over, which saves a lot of tedious copy and pasting
11-
12-
func DTchangeReferences() {
13-
jsonFile := "/Users/mattprincev/Documents/Rhize/JSON Schema/b2mml-batchml/dataTypeTest.json"
14-
baseFile := "/Users/mattprincev/Documents/Rhize/JSON Schema/b2mml-batchml/oldSchema/v2.0.0.base.schema.json"
11+
func DTchangeReferences(newFile string, baseFile string) (int, error) {
1512

1613
// Extract keys from the JSON file
17-
keys, err := DTextractKeys(jsonFile)
14+
keys, err := DTextractKeys(newFile)
1815
if err != nil {
19-
fmt.Println("Error extracting keys:", err)
20-
return
16+
return fmt.Println("Error extracting keys:", err)
2117
}
2218

23-
// fmt.Println("Extracted keys:", keys)
24-
// fmt.Println("Length of extracted keys:", len(keys))
25-
2619
// Get the $ref values from the base file
2720
refs, err := DTgetRefs(keys, baseFile)
2821
if err != nil {
29-
fmt.Println("Error getting references:", err)
30-
return
22+
return fmt.Println("Error getting references:", err)
3123
}
3224

33-
// Print the map of refs
34-
// refsJSON, err := json.MarshalIndent(refs, "", " ")
35-
// if err != nil {
36-
// fmt.Println("Error marshalling refs:", err)
37-
// return
38-
// }
39-
// fmt.Println("Refs:", string(refsJSON))
40-
// fmt.Println("refs:", refs)
41-
4225
// Replace the $ref values in the output file
43-
if err := DTreplacesRefs(jsonFile, refs); err != nil {
44-
fmt.Println("Error replacing references:", err)
26+
if err := DTreplacesRefs(newFile, refs); err != nil {
27+
return fmt.Println("Error replacing references:", err)
4528
}
29+
30+
return 0, nil
4631
}
4732

33+
// Extracts the definitions keys from the dataType schema file
4834
func DTextractKeys(jsonFile string) ([]string, error) {
35+
36+
// read the json file
4937
data, err := os.ReadFile(jsonFile)
5038
if err != nil {
5139
return nil, fmt.Errorf("error reading JSON file: %v", err)
5240
}
5341

42+
// unmarshall the data from reading the json file and map it to the struct in main
5443
var jsonData map[string]interface{}
5544
if err := json.Unmarshal(data, &jsonData); err != nil {
5645
return nil, fmt.Errorf("error unmarshalling JSON data: %v", err)
@@ -62,6 +51,7 @@ func DTextractKeys(jsonFile string) ([]string, error) {
6251
return nil, fmt.Errorf("$defs section not found or is not an object")
6352
}
6453

54+
// create string array for mapping definitions from schema
6555
var keys []string
6656
for key := range defs {
6757
keys = append(keys, key)
@@ -70,7 +60,9 @@ func DTextractKeys(jsonFile string) ([]string, error) {
7060
return keys, nil
7161
}
7262

63+
// Finds the references within the base schema file and creates a map containing them
7364
func DTgetRefs(keys []string, baseFile string) (map[string]string, error) {
65+
7466
// Read the JSON file
7567
data, err := os.ReadFile(baseFile)
7668
if err != nil {
@@ -105,7 +97,9 @@ func DTgetRefs(keys []string, baseFile string) (map[string]string, error) {
10597
return refMap, nil
10698
}
10799

100+
// This will write the references found in the base file to the dataType schema file
108101
func DTreplacesRefs(outputFile string, refValues map[string]string) error {
102+
109103
// Read the JSON file
110104
data, err := os.ReadFile(outputFile)
111105
if err != nil {
@@ -143,6 +137,7 @@ func DTreplacesRefs(outputFile string, refValues map[string]string) error {
143137

144138
// updateRefs recursively updates the $ref values in the $defs section
145139
func updateRefs(data interface{}, refValues map[string]string) {
140+
146141
switch v := data.(type) {
147142
case map[string]interface{}:
148143
for key, value := range v {

scripts/findPropRefs.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3935
func 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
5960
func 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
8285
func 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
}

scripts/main.go

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package main
22

3+
import (
4+
"flag"
5+
"fmt"
6+
)
7+
38
// Struct to represent the JSON structure with generic map
49
type JSONStructure struct {
510
ID string `json:"$id"`
@@ -14,22 +19,70 @@ type JSONStructure struct {
1419
}
1520

1621
func main() {
17-
jsonFile := "/Users/mattprincev/Documents/Rhize/JSON Schema/b2mml-batchml/schemas/v2.0.0.generalRecipe.schema.json"
18-
// findPropRefs(jsonFile)
19-
changeReferences(jsonFile)
20-
21-
// DTchangeReferences()
22-
// parse()
23-
// schemaFile := "oldSchema/v2.0.0.base.schema.json"
24-
25-
// // Load the schema
26-
// schemaLoader := gojsonschema.NewReferenceLoader(schemaFile)
27-
28-
// // Try to compile the schema
29-
// _, err := gojsonschema.NewSchema(schemaLoader)
30-
// if err != nil {
31-
// log.Fatalf("Schema compilation error: %s", err)
32-
// } else {
33-
// log.Println("Schema compiled successfully.")
34-
// }
22+
// Define command-line flags
23+
24+
// Flag variables for baseToDt.go
25+
// example command: go run ./ --dtNewFile=myDataType.json --baseFile=myBaseFile.json
26+
dtNewFile := flag.String("dtNewFile", "", "Path to the dataType file")
27+
dTBaseFile := flag.String("baseFile", "", "Path to the dataType base file")
28+
29+
// Flag variables for findPropRefs.go
30+
// example command: go run ./ --propRefNewFile=myDataType.json --propRefBaseFile=myBaseFile.json
31+
propRefNewFile := flag.String("propRefNewFile", "", "Path to the file that the property references will be added to")
32+
propRefBaseFile := flag.String("propRefBaseFile", "", "Path to the base file that contains the property references")
33+
34+
// Flag variables for refFinder.go
35+
// example command: go run ./ --refFinderNewFile=myDataType.json --refFinderBaseFile=myBaseFile.json
36+
refFinderNewFile := flag.String("refFinderNewFile", "", "Path to the file that the conatins the reference names that will be searched for")
37+
refFinderBaseFile := flag.String("refFinderBaseFile", "", "Path to the directory containing the schema files to be referenced.")
38+
39+
// Flag variables for xsdToJson.go
40+
// example command: go run ./ --dtNewFile=myDataType.json --baseFile=myBaseFile.json
41+
xsdFile := flag.String("xsdFile", "", "Path to the xsd schema file")
42+
baseJsonFile := flag.String("baseJsonFile", "", "Path to the base json schema file")
43+
outputFile := flag.String("outputFile", "", "Name of the output file that will be created")
44+
45+
// Parse the command-line flags
46+
flag.Parse()
47+
48+
// Check if DTchangeReferences should be called
49+
if *dtNewFile != "" && *dTBaseFile != "" {
50+
fmt.Println("Running baseToDt.go")
51+
if _, err := DTchangeReferences(*dtNewFile, *dTBaseFile); err != nil {
52+
return
53+
}
54+
// fmt.Println(*dtNewFile, *dTBaseFile)
55+
} else {
56+
fmt.Println("Flags for dataType file are empty")
57+
}
58+
59+
if *propRefNewFile != "" && *propRefBaseFile != "" {
60+
fmt.Println("Running findPropRefs.go")
61+
if _, err := findPropRefs(*propRefNewFile, *propRefBaseFile); err != nil {
62+
return
63+
}
64+
// fmt.Println(*propRefNewFile, *propRefBaseFile)
65+
} else {
66+
fmt.Println("Flags for property reference finder are empty")
67+
}
68+
69+
if *refFinderNewFile != "" && *refFinderBaseFile != "" {
70+
fmt.Println("Running refFinder.go")
71+
if _, err := changeReferences(*refFinderNewFile, *refFinderBaseFile); err != nil {
72+
return
73+
}
74+
// fmt.Println(*refFinderNewFile, *refFinderBaseFile)
75+
} else {
76+
fmt.Println("Flags for reference finding are empty")
77+
}
78+
79+
if *xsdFile != "" && *baseJsonFile != "" && *outputFile != "" {
80+
fmt.Println("Running refFinder.go")
81+
if _, err := parse(*xsdFile, *baseJsonFile, *outputFile); err != nil {
82+
return
83+
}
84+
// fmt.Println(*xsdFile, *baseJsonFile, *outputFile)
85+
} else {
86+
fmt.Println("Flags for xsd to JSON conversion are empty")
87+
}
3588
}

0 commit comments

Comments
 (0)