| 1 |
// =========================================================================== |
| 2 |
// |
| 3 |
// PUBLIC DOMAIN NOTICE |
| 4 |
// National Center for Biotechnology Information (NCBI) |
| 5 |
// |
| 6 |
// This software/database is a "United States Government Work" under the |
| 7 |
// terms of the United States Copyright Act. It was written as part of |
| 8 |
// the author's official duties as a United States Government employee and |
| 9 |
// thus cannot be copyrighted. This software/database is freely available |
| 10 |
// to the public for use. The National Library of Medicine and the U.S. |
| 11 |
// Government do not place any restriction on its use or reproduction. |
| 12 |
// We would, however, appreciate having the NCBI and the author cited in |
| 13 |
// any work or product based on this material. |
| 14 |
// |
| 15 |
// Although all reasonable efforts have been taken to ensure the accuracy |
| 16 |
// and reliability of the software and data, the NLM and the U.S. |
| 17 |
// Government do not and cannot warrant the performance or results that |
| 18 |
// may be obtained by using this software or data. The NLM and the U.S. |
| 19 |
// Government disclaim all warranties, express or implied, including |
| 20 |
// warranties of performance, merchantability or fitness for any particular |
| 21 |
// purpose. |
| 22 |
// |
| 23 |
// =========================================================================== |
| 24 |
// |
| 25 |
// File Name: yaml.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"bufio" |
| 35 |
"github.com/goccy/go-yaml" |
| 36 |
"io" |
| 37 |
"os" |
| 38 |
"runtime" |
| 39 |
"strings" |
| 40 |
) |
| 41 |
|
| 42 |
// YAMLConverter parses YAML files through JSON into XML object stream |
| 43 |
func YAMLConverter(inp io.Reader) <-chan string { |
| 44 |
|
| 45 |
if inp == nil { |
| 46 |
return nil |
| 47 |
} |
| 48 |
|
| 49 |
out := make(chan string, chanDepth) |
| 50 |
if out == nil { |
| 51 |
DisplayError("Unable to create YAML converter channel") |
| 52 |
os.Exit(1) |
| 53 |
} |
| 54 |
|
| 55 |
convertYAML := func(inp io.Reader, out chan<- string) { |
| 56 |
|
| 57 |
// close channel when all records have been sent |
| 58 |
defer close(out) |
| 59 |
|
| 60 |
var buffer strings.Builder |
| 61 |
|
| 62 |
scanr := bufio.NewScanner(inp) |
| 63 |
|
| 64 |
for scanr.Scan() { |
| 65 |
|
| 66 |
line := scanr.Text() |
| 67 |
buffer.WriteString(line + "\n") |
| 68 |
} |
| 69 |
|
| 70 |
txt := buffer.String() |
| 71 |
|
| 72 |
buffer.Reset() |
| 73 |
|
| 74 |
if txt == "" { |
| 75 |
return |
| 76 |
} |
| 77 |
|
| 78 |
inputBytes := []byte(txt) |
| 79 |
buff, err := yaml.YAMLToJSON(inputBytes) |
| 80 |
if err != nil { |
| 81 |
DisplayError("YAMLToJSON error '%v'", err) |
| 82 |
os.Exit(1) |
| 83 |
} |
| 84 |
|
| 85 |
jsn := string(buff) |
| 86 |
|
| 87 |
if jsn == "" { |
| 88 |
return |
| 89 |
} |
| 90 |
|
| 91 |
jcnv := JSONConverter(strings.NewReader(jsn), "", "ConfigFile", "element") |
| 92 |
|
| 93 |
if jcnv == nil { |
| 94 |
DisplayError("Unable to create YAML to JSON to XML converter") |
| 95 |
os.Exit(1) |
| 96 |
} |
| 97 |
|
| 98 |
for str := range jcnv { |
| 99 |
if str != "" { |
| 100 |
out <- str |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
runtime.Gosched() |
| 105 |
} |
| 106 |
|
| 107 |
go convertYAML(inp, out) |
| 108 |
|
| 109 |
return out |
| 110 |
} |
| 111 |
|
| 112 |
// YAMLtoXML converts YAML to an XML string |
| 113 |
func YAMLtoXML(yml string) string { |
| 114 |
|
| 115 |
return StringToXML(yml, YAMLConverter) |
| 116 |
} |