Skip to content

Commit

Permalink
Now will handle an array of the "mockaroo HL7" objects or a single ob…
Browse files Browse the repository at this point in the history
…ject via specified file input or via stdin.
  • Loading branch information
austinmoody committed Mar 25, 2022
1 parent 91814c9 commit 13ae520
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/**/.idea/*
89 changes: 85 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
)
Expand All @@ -16,6 +19,9 @@ var (
templateFile = flag.String("template", "", "Output Template File")
)

const JsonArray = "["
const JsonObject = "{"

func main() {

flag.Parse()
Expand All @@ -31,19 +37,94 @@ func main() {

}

func ValidJson(rdr io.Reader) (bool, string) {
decoder := json.NewDecoder(rdr)

firstToken, err := decoder.Token()
if err != nil {
log.Fatal(err)
}

if delimiter, success := firstToken.(json.Delim); success {
switch delimiter.String() {
case JsonArray:
return true, JsonArray
case JsonObject:
return true, JsonObject
default:
return false, ""
}
}

return false, ""
}

func ProcessInputFile(fileName string, hl7encoding Hl7Encoding) {

inputFile, err := os.Open(fileName)
if err != nil {
panic(err)
}

jsonValid, jsonType := ValidJson(inputFile)

if jsonValid != true {
panic("Input JSON is invalid")
}

// reset so we can decode full input
_, err = inputFile.Seek(0, io.SeekStart)
if err != nil {
panic(err)
}

decoder := json.NewDecoder(inputFile)

ProcessJsonInput(decoder, hl7encoding)
if jsonType == JsonArray {
ProcessJsonInput(decoder, hl7encoding)
} else {
ProcessSingleJsonInput(decoder, hl7encoding)
}
}

func ProcessStdin(hl7encoding Hl7Encoding) {
decoder := json.NewDecoder(os.Stdin)
ProcessJsonInput(decoder, hl7encoding)

jsonType := ""
jsonValid := false

buf := bytes.Buffer{}
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
if jsonType == "" {
jsonValid, jsonType = ValidJson(bytes.NewReader(s.Bytes()))
}
buf.Write(s.Bytes())
}

if jsonValid != true {
panic("Input JSON is invalid")
}

decoder := json.NewDecoder(bufio.NewReader(&buf))

if jsonType == JsonArray {
ProcessJsonInput(decoder, hl7encoding)
} else {
ProcessSingleJsonInput(decoder, hl7encoding)
}

}

func ProcessSingleJsonInput(decoder *json.Decoder, hl7encoding Hl7Encoding) {
var encounter Encounter

err := decoder.Decode(&encounter)
if err != nil {
log.Fatal(err)
}

encounter.Hl7Encoding = hl7encoding
fmt.Printf("%s%s", encounter.AsHl7(), "\u0000")
}

func ProcessJsonInput(decoder *json.Decoder, hl7encoding Hl7Encoding) {
Expand All @@ -64,7 +145,7 @@ func ProcessJsonInput(decoder *json.Decoder, hl7encoding Hl7Encoding) {
}

encounter.Hl7Encoding = hl7encoding
fmt.Print(encounter.AsHl7())
fmt.Printf("%s%s", encounter.AsHl7(), "\u0000")
}

// JSON array closing
Expand Down

0 comments on commit 13ae520

Please sign in to comment.