git.delta.rocks / jrsonnet / refs/commits / bc97a8002235

difftreelog

feat add std.manifestToml

Yaroslav Bolyukin2021-03-27parent: #82fef86.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
830 ];830 ];
831 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),831 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),
832832
833 manifestToml(value):: std.manifestTomlEx(value, ' '),
834
835 manifestTomlEx(value, indent)::
836 local
837 escapeStringToml = std.escapeStringJson,
838 escapeKeyToml(key) =
839 local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));
840 if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),
841 isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),
842 isSection(v) = std.isObject(v) || isTableArray(v),
843 renderValue(v, indexedPath, inline, cindent) =
844 if v == true then
845 'true'
846 else if v == false then
847 'false'
848 else if v == null then
849 error 'Tried to manifest "null" at ' + indexedPath
850 else if std.isNumber(v) then
851 '' + v
852 else if std.isString(v) then
853 escapeStringToml(v)
854 else if std.isFunction(v) then
855 error 'Tried to manifest function at ' + indexedPath
856 else if std.isArray(v) then
857 if std.length(v) == 0 then
858 '[]'
859 else
860 local range = std.range(0, std.length(v) - 1);
861 local new_indent = if inline then '' else cindent + indent;
862 local separator = if inline then ' ' else '\n';
863 local lines = ['[' + separator]
864 + std.join([',' + separator],
865 [
866 [new_indent + renderValue(v[i], indexedPath + [i], true, '')]
867 for i in range
868 ])
869 + [separator + (if inline then '' else cindent) + ']'];
870 std.join('', lines)
871 else if std.isObject(v) then
872 local lines = ['{ ']
873 + std.join([', '],
874 [
875 [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]
876 for k in std.objectFields(v)
877 ])
878 + [' }'];
879 std.join('', lines),
880 renderTableInternal(v, path, indexedPath, cindent) =
881 local kvp = std.flattenArrays([
882 [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]
883 for k in std.objectFields(v)
884 if !isSection(v[k])
885 ]);
886 local sections = [std.join('\n', kvp)] + [
887 (
888 if std.isObject(v[k]) then
889 renderTable(v[k], path + [k], indexedPath + [k], cindent)
890 else
891 renderTableArray(v[k], path + [k], indexedPath + [k], cindent)
892 )
893 for k in std.objectFields(v)
894 if isSection(v[k])
895 ];
896 std.join('\n\n', sections),
897 renderTable(v, path, indexedPath, cindent) =
898 cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'
899 + (if v == {} then '' else '\n')
900 + renderTableInternal(v, path, indexedPath, cindent + indent),
901 renderTableArray(v, path, indexedPath, cindent) =
902 local range = std.range(0, std.length(v) - 1);
903 local sections = [
904 (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'
905 + (if v[i] == {} then '' else '\n')
906 + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))
907 for i in range
908 ];
909 std.join('\n\n', sections);
910 if std.isObject(value) then
911 renderTableInternal(value, [], [], '')
912 else
913 error 'TOML body must be an object. Got ' + std.type(value),
914
833 escapeStringJson(str_)::915 escapeStringJson(str_)::
834 local str = std.toString(str_);916 local str = std.toString(str_);