aboutsummaryrefslogtreecommitdiff
path: root/src/mustach/mustach-jansson.c
blob: 2aed582919d6b547354997897abe45ce2ef07abf (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 Copyright (C) 2020 Taler Systems SA

 Original license:
 Author: José Bollo <jobol@nonadev.net>
 Author: José Bollo <jose.bollo@iot.bzh>

 https://gitlab.com/jobol/mustach

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

#include "platform.h"
#include "mustach-jansson.h"

struct Context
{
  /**
   * Context object.
   */
  json_t *cont;

  /**
   * Current object.
   */
  json_t *obj;

  /**
   * Opaque object iterator.
   */
  void *iter;

  /**
   * Current index when iterating over an array.
   */
  unsigned int index;

  /**
   * Count when iterating over an array.
   */
  unsigned int count;

  bool is_objiter;
};

enum Bang
{
  BANG_NONE,
  BANG_I18N,
  BANG_STRINGIFY,
  BANG_AMOUNT_CURRENCY,
  BANG_AMOUNT_DECIMAL,
};

struct JanssonClosure
{
  json_t *root;
  mustach_jansson_write_cb writecb;
  int depth;

  /**
   * Did the last find(..) call result in an iterable?
   */
  struct Context stack[MUSTACH_MAX_DEPTH];

  /**
   * The last object we found should be iterated over.
   */
  bool found_iter;

  /**
   * Last bang we found.
   */
  enum Bang found_bang;
  
  /**
   * Language for i18n lookups.
   */
  const char *lang;
};


static json_t *
walk (json_t *obj, const char *path)
{
  char *saveptr = NULL;
  char *sp = GNUNET_strdup (path);
  char *p = sp;
  while (true)
  {
    char *tok = strtok_r (p, ".", &saveptr);
    if (tok == NULL)
      break;
    obj = json_object_get (obj, tok);
    if (obj == NULL)
      break;
    p = NULL;
  }
  GNUNET_free (sp);
  return obj;
}


static json_t *
find (struct JanssonClosure *e, const char *name)
{
  json_t *obj = NULL;
  char *path = GNUNET_strdup (name);
  char *bang;

  bang = strchr (path, '!');

  e->found_bang = BANG_NONE;

  if (NULL != bang)
  {
    *bang = 0;
    bang++;

    if (0 == strcmp (bang, "i18n"))
      e->found_bang = BANG_I18N;
    else if (0 == strcmp(bang, "stringify"))
      e->found_bang = BANG_STRINGIFY;
    else if (0 == strcmp(bang, "amount_decimal"))
      e->found_bang = BANG_AMOUNT_CURRENCY;
    else if (0 == strcmp(bang, "amount_currency"))
      e->found_bang = BANG_AMOUNT_DECIMAL;
  }

  if (BANG_I18N == e->found_bang && NULL != e->lang)
  {
    char *aug_path;
    GNUNET_asprintf (&aug_path, "%s_i18n.%s", path, e->lang);
    obj = walk (e->stack[e->depth].obj, aug_path);
    GNUNET_free (aug_path);
  }

  if (NULL == obj)
  {
    obj = walk (e->stack[e->depth].obj, path);
  }

  GNUNET_free (path);

  return obj;
}


static int
start(void *closure)
{
  struct JanssonClosure *e = closure;
  e->depth = 0;
  e->stack[0].cont = NULL;
  e->stack[0].obj = e->root;
  e->stack[0].index = 0;
  e->stack[0].count = 1;
  e->lang = json_string_value (json_object_get (e->root, "$language"));
  return MUSTACH_OK;
}


static int
emituw (void *closure, const char *buffer, size_t size, int escape, FILE *file)
{
  struct JanssonClosure *e = closure;
  if (!escape)
    e->writecb (file, buffer, size);
  else
    do
    {
      switch (*buffer)
      {
        case '<':
          e->writecb (file, "&lt;", 4);
          break;
        case '>':
          e->writecb (file, "&gt;", 4);
          break;
        case '&':
          e->writecb (file, "&amp;", 5);
          break;
        default:
          e->writecb (file, buffer, 1);
          break;
      }
      buffer++;
    }
    while(--size);
  return MUSTACH_OK;
}


static int
enter(void *closure, const char *name)
{
  struct JanssonClosure *e = closure;
  json_t *o = find(e, name);
  if (++e->depth >= MUSTACH_MAX_DEPTH)
    return MUSTACH_ERROR_TOO_DEEP;

  if (json_is_object (o))
  {
    if (e->found_iter)
    {
      void *iter = json_object_iter (o);
      if (NULL == iter)
      {
        e->depth--;
        return 0;
      }
      e->stack[e->depth].is_objiter = 1;
      e->stack[e->depth].iter = iter;
      e->stack[e->depth].obj = json_object_iter_value (iter);
      e->stack[e->depth].cont = o;
    }
    else
    {
      e->stack[e->depth].is_objiter = 0;
      e->stack[e->depth].obj = o;
      e->stack[e->depth].cont = o;
    }
    return 1;
  }

  if (json_is_array (o))
  {
    unsigned int size = json_array_size (o);
    if (size == 0)
    {
      e->depth--;
      return 0;
    }
    e->stack[e->depth].count = size;
    e->stack[e->depth].cont = o;
    e->stack[e->depth].obj = json_array_get (o, 0);
    e->stack[e->depth].index = 0;
    e->stack[e->depth].is_objiter = 0;
    return 1;
  }

  e->depth--;
  return 0;
}


static int
next (void *closure)
{
  struct JanssonClosure *e = closure;
  struct Context *ctx;
  if (e->depth <= 0)
    return MUSTACH_ERROR_CLOSING;
  ctx = &e->stack[e->depth];
  if (ctx->is_objiter)
  {
    ctx->iter = json_object_iter_next (ctx->obj, ctx->iter);
    if (NULL == ctx->iter)
      return 0;
    ctx->obj = json_object_iter_value (ctx->iter);
    return 1;
  }
  ctx->index++;
  if (ctx->index >= ctx->count)
    return 0;
  ctx->obj = json_array_get (ctx->cont, ctx->index);
  return 1;
}

static int
leave (void *closure)
{
  struct JanssonClosure *e = closure;
  if (e->depth <= 0)
    return MUSTACH_ERROR_CLOSING;
  e->depth--;
  return 0;
}

static void
freecb (void *v)
{
  free (v);
}

static int
get (void *closure, const char *name, struct mustach_sbuf *sbuf)
{
  struct JanssonClosure *e = closure;
  json_t *obj;

  if ( (0 == strcmp (name, "*") ) &&
       (e->stack[e->depth].is_objiter ) )
  {
    sbuf->value = json_object_iter_key (e->stack[e->depth].iter);
    return MUSTACH_OK;
  }
  obj = find (e, name);
  if (NULL != obj)
  {
    switch (e->found_bang)
    {
      case BANG_I18N:
      case BANG_NONE:
        {
          const char *s = json_string_value (obj);
          if (NULL != s)
          {
            sbuf->value = s;
            return MUSTACH_OK;
          }
        }
        break;
      case BANG_STRINGIFY:
        sbuf->value = json_dumps (obj, JSON_INDENT (2));
        sbuf->freecb = freecb;
        return MUSTACH_OK;
      case BANG_AMOUNT_DECIMAL:
        {
          char *s;
          char *c;
          if (!json_is_string (obj))
            break;
          s = strdup (json_string_value (obj));
          c = strchr (s, ':');
          if (NULL != c)
            *c = 0;
          sbuf->value = s;
          sbuf->freecb = freecb;
          return MUSTACH_OK;
        }
        break;
      case BANG_AMOUNT_CURRENCY:
        {
          const char *s;
          if (!json_is_string (obj))
            break;
          s = json_string_value (obj);
          s = strchr (s, ':');
          if (NULL == s)
            break;
          sbuf->value = s + 1;
          return MUSTACH_OK;
        }
        break;
      default:
        break;
    }
  }
  sbuf->value = "";
  return MUSTACH_OK;
}

static struct mustach_itf itf = {
  .start = start,
  .put = NULL,
  .enter = enter,
  .next = next,
  .leave = leave,
  .partial =NULL,
  .get = get,
  .emit = NULL,
  .stop = NULL
};

static struct mustach_itf itfuw = {
  .start = start,
  .put = NULL,
  .enter = enter,
  .next = next,
  .leave = leave,
  .partial = NULL,
  .get = get,
  .emit = emituw,
  .stop = NULL
};

int fmustach_jansson (const char *template, json_t *root, FILE *file)
{
  struct JanssonClosure e = { 0 };
  e.root = root;
  return fmustach(template, &itf, &e, file);
}

int fdmustach_jansson (const char *template, json_t *root, int fd)
{
  struct JanssonClosure e = { 0 };
  e.root = root;
  return fdmustach(template, &itf, &e, fd);
}

int mustach_jansson (const char *template, json_t *root, char **result, size_t *size)
{
  struct JanssonClosure e = { 0 };
  e.root = root;
  e.writecb = NULL;
  return mustach(template, &itf, &e, result, size);
}

int umustach_jansson (const char *template, json_t *root, mustach_jansson_write_cb writecb, void *closure)
{
  struct JanssonClosure e = { 0 };
  e.root = root;
  e.writecb = writecb;
  return fmustach(template, &itfuw, &e, closure);
}