Skip to content

Commit

Permalink
Merge pull request #710 from OpenAF/t8
Browse files Browse the repository at this point in the history
T8
  • Loading branch information
nmaguiar authored Sep 29, 2023
2 parents 468726f + 57fb51e commit dcddd82
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
36 changes: 24 additions & 12 deletions js/openaf.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,11 @@ const cprint = function(str, delim) { ansiStart(); print(colorify(str)); ansiSto

/**
* <odoc>
* <key>yprint(aObj, multidoc)</key>
* Prints aObj in YAML. If multiDoc = true and aJson is an array the output will be multi-document.
* <key>yprint(aObj, multidoc, sanitize)</key>
* Prints aObj in YAML. If multiDoc = true and aJson is an array the output will be multi-document. If sanitize = true all Java objects will be converted to avoid parsing errors.
* </odoc>
*/
const yprint = function(str, multidoc) { return print(af.toYAML(str, multidoc)); }
const yprint = function(str, multidoc, sanitize) { return print(af.toYAML(str, multidoc, sanitize)); }

/**
* <odoc>
Expand Down Expand Up @@ -7606,17 +7606,23 @@ __YAMLformat = {
};
/**
* <odoc>
* <key>AF.toYAML(aJson, multiDoc) : String</key>
* Tries to dump aJson into a YAML string. If multiDoc = true and aJson is an array the output will be multi-document.
* <key>AF.toYAML(aJson, multiDoc, sanitize) : String</key>
* Tries to dump aJson into a YAML string. If multiDoc = true and aJson is an array the output will be multi-document. If sanitize = true all Java objects will be converted to avoid parsing errors.
* </odoc>
*/
AF.prototype.toYAML = function(aJson, multiDoc) {
loadJSYAML();
var o = { indent: __YAMLformat.indent, noArrayIndent: !__YAMLformat.arrayIndent, lineWidth: __YAMLformat.lineWidth };
AF.prototype.toYAML = function(aJson, multiDoc, sanitize) {
loadJSYAML()
if (sanitize) {
aJson = clone(aJson)
traverse(aJson, (aK, aV, aP, aO) => {
if (isJavaObject(aV)) aO[aK] = String(aV)
})
}
var o = { indent: __YAMLformat.indent, noArrayIndent: !__YAMLformat.arrayIndent, lineWidth: __YAMLformat.lineWidth }
if (isArray(aJson) && multiDoc) {
return aJson.map(y => jsyaml.dump(y, o)).join("\n---\n\n");
return aJson.map(y => jsyaml.dump(y, o)).join("\n---\n\n")
} else {
return jsyaml.dump(aJson, o);
return jsyaml.dump(aJson, o)
}
}

Expand Down Expand Up @@ -7905,13 +7911,19 @@ AF.prototype.fromXML2Obj = function (xml, ignored, aPrefix) {

/**
* <odoc>
* <key>af.fromObj2XML(aMap) : String</key>
* <key>af.fromObj2XML(aMap, sanitize) : String</key>
* Tries to convert aMap into a similiar XML strucuture returned as string.
* Note that no validation of XML strucuture is performed.
* Tips: ensure each map is under a map key.
* </odoc>
*/
AF.prototype.fromObj2XML = function (obj) {
AF.prototype.fromObj2XML = function (obj, sanitize) {
if (sanitize) {
obj = clone(obj)
traverse(obj, (aK, aV, aP, aO) => {
if (isJavaObject(aV)) aO[aK] = String(aV)
})
}
var xml = '';
for (var prop in obj) {
if (obj[prop] instanceof Array) {
Expand Down
32 changes: 28 additions & 4 deletions js/openafsigil.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@
if (!(typeof isJavaObject == 'defined')) isJavaObject = () => false
const $$ = function(aObj) {
const _r = {
_ss: aString => {
let result = []
let word = ""
let inQuotes = false
for (let i = 0; i < aString.length; i++) {
let char = aString[i]
if (char === "." && !inQuotes) {
result.push(word)
word = ""
} else if (char === "'") {
inQuotes = !inQuotes
if (!inQuotes) {
result.push(word)
word = ""
}
} else {
word += char
}
}
if (word) {
result.push(word)
}
return result
},
/**
* <odoc>
* <key>$$.get(aPath) : Object</key>
Expand All @@ -23,7 +47,7 @@ const $$ = function(aObj) {
aPath = aPath.replace(/\[(\w+)\]/g, '.$1');
aPath = aPath.replace(/^\./, '');

var a = aPath.split('.');
var a = _r._ss(aPath).filter(r => r.length > 0)
for (var i = 0, n = a.length; i < n; ++i) {
var k = a[i];
if (k in aObj) {
Expand Down Expand Up @@ -53,7 +77,7 @@ const $$ = function(aObj) {
aPath = aPath.replace(/\[(\w+)\]/g, '.$1')
aPath = aPath.replace(/^\./, '')

var a = aPath.split('.')
var a = _r._ss(aPath).filter(r => r.length > 0)
for (var i = 0, n = a.length; i < n; ++i) {
var k = a[i]

Expand Down Expand Up @@ -94,7 +118,7 @@ const $$ = function(aObj) {
aPath = aPath.replace(/\[(\w+)\]/g, '.$1');
aPath = aPath.replace(/^\./, '');

var a = aPath.split('.');
var a = _r._ss(aPath).filter(r => r.length > 0)
var prev, prevK;
for (var i = 0, n = a.length; i < n; ++i) {
var k = a[i];
Expand Down Expand Up @@ -128,7 +152,7 @@ const $$ = function(aObj) {
aPath = aPath.replace(/\[(\w+)\]/g, '.$1')
aPath = aPath.replace(/^\./, '')

var a = aPath.split('.')
var a = _r._ss(aPath).filter(r => r.length > 0)
var prev, prevK
for (var i = 0, n = a.length; i < n; ++i) {
var k = a[i]
Expand Down
10 changes: 8 additions & 2 deletions js/owrap.oJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ OpenWrap.oJob = function(isNonLocal) {
pos: "\n_args, _err := json.Marshal(args); if _err != nil { return }; fmt.Println(string(_args))}",
withFile: ".go"
},
"node": {
lang : "node",
shell: "node",
pre : "var args = {{{args}}};",
pos : ";console.log(JSON.stringify(args, void 0, ''))"
},
"ruby": {
lang : "ruby",
shell: "ruby -",
Expand Down Expand Up @@ -3586,10 +3592,10 @@ OpenWrap.oJob.prototype.output = function(aObj, args, aFunc) {
if (isArray(res)) res.forEach(e => print(stringify(e, __, "")))
break
case "xml":
print(af.fromObj2XML(res))
print(af.fromObj2XML(res, true))
break
case "yaml":
yprint(res);
yprint(res, __, true);
break;
case "table":
if (isMap(res)) res = [ res ]
Expand Down

0 comments on commit dcddd82

Please sign in to comment.