about summary refs log tree commit diff
path: root/src/libexpr/value-to-xml.cc
blob: c8a067aacf77e2df5adc5756f09b541650c82614 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "value-to-xml.hh"
#include "xml-writer.hh"
#include "util.hh"

#include <cstdlib>


namespace nix {

    
static XMLAttrs singletonAttrs(const string & name, const string & value)
{
    XMLAttrs attrs;
    attrs[name] = value;
    return attrs;
}


static void printValueAsXML(EvalState & state, bool strict, Value & v,
    XMLWriter & doc, PathSet & context, PathSet & drvsSeen);


static void showAttrs(EvalState & state, bool strict, Bindings & attrs,
    XMLWriter & doc, PathSet & context, PathSet & drvsSeen)
{
    StringSet names;
    foreach (Bindings::iterator, i, attrs)
        names.insert(i->first);
    foreach (StringSet::iterator, i, names) {
        XMLOpenElement _(doc, "attr", singletonAttrs("name", *i));
        printValueAsXML(state, strict, attrs[*i], doc, context, drvsSeen);
    }
}


static void printValueAsXML(EvalState & state, bool strict, Value & v,
    XMLWriter & doc, PathSet & context, PathSet & drvsSeen)
{
    checkInterrupt();

    if (strict) state.forceValue(v);
        
    switch (v.type) {

        case tInt:
            doc.writeEmptyElement("int", singletonAttrs("value", (format("%1%") % v.integer).str()));
            break;

        case tBool:
            doc.writeEmptyElement("bool", singletonAttrs("value", v.boolean ? "true" : "false"));
            break;

        case tString:
            /* !!! show the context? */
            doc.writeEmptyElement("string", singletonAttrs("value", v.string.s));
            break;

        case tPath:
            doc.writeEmptyElement("path", singletonAttrs("value", v.path));
            break;

        case tNull:
            doc.writeEmptyElement("null");
            break;

        case tAttrs:
            if (state.isDerivation(v)) {
                XMLAttrs xmlAttrs;
            
                Bindings::iterator a = v.attrs->find("derivation");

                Path drvPath;
                a = v.attrs->find("drvPath");
                if (a != v.attrs->end() && a->second.type == tString)
                    xmlAttrs["drvPath"] = drvPath = a->second.string.s;
        
                a = v.attrs->find("outPath");
                if (a != v.attrs->end() && a->second.type == tString)
                    xmlAttrs["outPath"] = a->second.string.s;

                XMLOpenElement _(doc, "derivation", xmlAttrs);

                if (drvPath != "" && drvsSeen.find(drvPath) == drvsSeen.end()) {
                    drvsSeen.insert(drvPath);
                    showAttrs(state, strict, *v.attrs, doc, context, drvsSeen);
                } else
                    doc.writeEmptyElement("repeated");
            }

            else {
                XMLOpenElement _(doc, "attrs");
                showAttrs(state, strict, *v.attrs, doc, context, drvsSeen);
            }
            
            break;

        case tList: {
            XMLOpenElement _(doc, "list");
            for (unsigned int n = 0; n < v.list.length; ++n)
                printValueAsXML(state, strict, v.list.elems[n], doc, context, drvsSeen);
            break;
        }

        case tLambda: {
            XMLOpenElement _(doc, "function");
            if (v.lambda.fun->matchAttrs) {
                XMLAttrs attrs;
                if (!v.lambda.fun->arg.empty()) attrs["name"] = v.lambda.fun->arg;
                if (v.lambda.fun->formals->ellipsis) attrs["ellipsis"] = "1";
                XMLOpenElement _(doc, "attrspat", attrs);
                foreach (Formals::Formals_::iterator, i, v.lambda.fun->formals->formals)
                    doc.writeEmptyElement("attr", singletonAttrs("name", i->name));
            } else
                doc.writeEmptyElement("varpat", singletonAttrs("name", v.lambda.fun->arg));
            break;
        }

        default:
            doc.writeEmptyElement("unevaluated");
    }
}


void printValueAsXML(EvalState & state, bool strict,
    Value & v, std::ostream & out, PathSet & context)
{
    XMLWriter doc(true, out);
    XMLOpenElement root(doc, "expr");
    PathSet drvsSeen;    
    printValueAsXML(state, strict, v, doc, context, drvsSeen);
}

 
}