Skip to content

Commit

Permalink
Merge pull request PelicanPlatform#266 from turetske/json_generation
Browse files Browse the repository at this point in the history
Json generation
  • Loading branch information
turetske authored Oct 23, 2023
2 parents d6c0991 + e9f1f04 commit 2d60e66
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/
README.dev.md
param/parameters.go
docs/parameter.json
36 changes: 35 additions & 1 deletion generate/param_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
// This should not be included in any release of pelican, instead only the generated "parameters.go" should packaged

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
Expand All @@ -22,10 +24,11 @@ var requiredKeys = [4]string{"name", "description", "default", "type"}
func GenParamEnum() {
/*
* This generated a file "config/parameters.go" that is based off of docs/parameters.yaml to be used
* instead of explicit calls to viper.Get*
* instead of explicit calls to viper.Get* It also generates a parameters.json file for website use
*/
filename, _ := filepath.Abs("../docs/parameters.yaml")
yamlFile, err := os.Open(filename)
fullJsonInt := []interface{}{}

if err != nil {
panic(err)
Expand Down Expand Up @@ -66,6 +69,17 @@ func GenParamEnum() {
}
}

// Each document must be converted to json on it's own and then the name
// must be used as a key
jsonBytes, _ := json.Marshal(entry)
var j map[string]interface{}
err = json.Unmarshal(jsonBytes, &j)
if err != nil {
panic(err)
}
j2 := map[string]interface{}{entry["name"].(string): j}
fullJsonInt = append(fullJsonInt, j2)

rawName := entry["name"].(string)
name := strings.ReplaceAll(rawName, ".", "_")
pType := entry["type"].(string)
Expand Down Expand Up @@ -106,6 +120,26 @@ func GenParamEnum() {
if err != nil {
panic(err)
}

// Write the json version of the yaml document to the file
fullJsonBytes, err := json.Marshal(fullJsonInt)
if err != nil {
panic(err)
}
var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, fullJsonBytes, "", "\t")
if err != nil {
panic(err)
}
// Create the json file to be generated (for the web ui)
fJSON, err := os.Create("../docs/parameters.json")
if err != nil {
panic(err)
}
_, err = fJSON.Write(prettyJSON.Bytes())
if err != nil {
panic(err)
}
}

// As more varied paramters get added to parameters.yaml with different paths and names, this may need to be
Expand Down

0 comments on commit 2d60e66

Please sign in to comment.