|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.internal.openapi.asciidoc; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +import io.jooby.internal.openapi.OperationExt; |
| 11 | +import io.pebbletemplates.pebble.error.PebbleException; |
| 12 | +import io.pebbletemplates.pebble.extension.Filter; |
| 13 | +import io.pebbletemplates.pebble.template.EvaluationContext; |
| 14 | +import io.pebbletemplates.pebble.template.PebbleTemplate; |
| 15 | +import io.swagger.v3.oas.models.Operation; |
| 16 | + |
| 17 | +public enum Filters implements Filter { |
| 18 | + curl { |
| 19 | + @Override |
| 20 | + public Object apply( |
| 21 | + Object input, |
| 22 | + Map<String, Object> args, |
| 23 | + PebbleTemplate self, |
| 24 | + EvaluationContext context, |
| 25 | + int lineNumber) |
| 26 | + throws PebbleException { |
| 27 | + try { |
| 28 | + if (!(input instanceof OperationExt operation)) { |
| 29 | + throw new IllegalArgumentException( |
| 30 | + "Argument must be " + Operation.class.getName() + ". Got: " + input); |
| 31 | + } |
| 32 | + var snippetResolver = (SnippetResolver) context.getVariable("snippetResolver"); |
| 33 | + |
| 34 | + var options = new LinkedHashMap<String, Set<String>>(); |
| 35 | + options.put("-X", Set.of(null)); |
| 36 | + operation |
| 37 | + .getProduces() |
| 38 | + .forEach( |
| 39 | + produces -> { |
| 40 | + options |
| 41 | + .computeIfAbsent("-H", (key) -> new LinkedHashSet<>()) |
| 42 | + .add("'Accept: " + produces + "'"); |
| 43 | + }); |
| 44 | + |
| 45 | + // Convert to map so can override any generated option |
| 46 | + var optionList = new ArrayList<>(args.values()); |
| 47 | + for (int i = 0; i < optionList.size(); ) { |
| 48 | + var key = optionList.get(i).toString(); |
| 49 | + String value = null; |
| 50 | + if (i + 1 < optionList.size()) { |
| 51 | + var next = optionList.get(i + 1); |
| 52 | + if (next.toString().startsWith("-")) { |
| 53 | + i += 1; |
| 54 | + } else { |
| 55 | + value = next.toString(); |
| 56 | + i += 2; |
| 57 | + } |
| 58 | + } else { |
| 59 | + i += 1; |
| 60 | + } |
| 61 | + var values = options.computeIfAbsent(key, k -> new LinkedHashSet<>()); |
| 62 | + if (value != null) { |
| 63 | + values.add(value); |
| 64 | + } |
| 65 | + } |
| 66 | + return snippetResolver.apply( |
| 67 | + "curl", Map.of("url", operation.getPattern(), "options", toString(options))); |
| 68 | + } catch (PebbleException pebbleException) { |
| 69 | + throw pebbleException; |
| 70 | + } catch (Exception exception) { |
| 71 | + throw new PebbleException(exception, name() + " failed to generate output"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private String toString(Map<String, Set<String>> options) { |
| 76 | + if (options.isEmpty()) { |
| 77 | + return ""; |
| 78 | + } |
| 79 | + var sb = new StringBuilder(); |
| 80 | + var separator = " "; |
| 81 | + for (var e : options.entrySet()) { |
| 82 | + var values = e.getValue(); |
| 83 | + if (values.isEmpty()) { |
| 84 | + sb.append(e.getKey()).append(separator); |
| 85 | + } else { |
| 86 | + for (var value : e.getValue()) { |
| 87 | + sb.append(e.getKey()).append(separator); |
| 88 | + sb.append(value).append(separator); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + sb.deleteCharAt(sb.length() - separator.length()); |
| 93 | + return sb.toString(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public List<String> getArgumentNames() { |
| 98 | + return null; |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + public static Map<String, Filter> fn() { |
| 103 | + Map<String, Filter> functions = new HashMap<>(); |
| 104 | + for (var value : values()) { |
| 105 | + functions.put(value.name(), value); |
| 106 | + } |
| 107 | + return functions; |
| 108 | + } |
| 109 | +} |
0 commit comments