Skip to content

Commit

Permalink
[jsonrpc] add openapi converter
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Jan 25, 2024
1 parent 0dbe4d0 commit 9c97c04
Show file tree
Hide file tree
Showing 6 changed files with 682 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2022 - present - Yupiik SAS - https://www.yupiik.com
* 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.
*/
package io.yupiik.fusion.documentation;

import io.yupiik.fusion.json.JsonMapper;
import io.yupiik.fusion.json.internal.JsonMapperImpl;
import io.yupiik.fusion.json.pretty.PrettyJsonMapper;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import static java.util.Objects.requireNonNull;
import static java.util.Optional.empty;

public abstract class BaseOpenRPCConverter implements Runnable {
protected final Map<String, String> configuration;

public BaseOpenRPCConverter(final Map<String, String> configuration) {
this.configuration = configuration;
}

protected abstract String convert(Map<String, Object> openrpc, JsonMapper mapper);

@Override
public void run() {
final var input = Path.of(requireNonNull(configuration.get("input"), "No 'input'"));
if (Files.notExists(input)) {
throw new IllegalArgumentException("Input does not exist '" + input + "'");
}
final var output = Path.of(requireNonNull(configuration.get("output"), "No 'output'"));
try (final var mapper = new PrettyJsonMapper(new JsonMapperImpl(List.of(), c -> empty()))) {
final var openrpc = asObject(mapper.fromString(Object.class, preProcessInput(Files.readString(input))));
final var adoc = convert(openrpc, mapper);
if (output.getParent() != null) {
Files.createDirectories(output.getParent());
}
Files.writeString(output, adoc);
} catch (final IOException ioe) {
throw new IllegalStateException(ioe);
}
}

protected String preProcessInput(final String input) {
return input;
}

@SuppressWarnings("unchecked")
protected Map<String, Object> asObject(final Object o) {
return (Map<String, Object>) o;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,27 @@
*/
package io.yupiik.fusion.documentation;

import io.yupiik.fusion.json.internal.JsonMapperImpl;
import io.yupiik.fusion.json.JsonMapper;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import static java.util.Map.entry;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.empty;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.joining;

public class OpenRPC2Adoc implements Runnable {
private final Map<String, String> configuration;

/**
* Converts a partial fusion openrpc to an asciidoctor content.
*/
public class OpenRPC2Adoc extends BaseOpenRPCConverter {
public OpenRPC2Adoc(final Map<String, String> configuration) {
this.configuration = configuration;
super(configuration);
}

@Override
public void run() {
final var input = Path.of(requireNonNull(configuration.get("input"), "No 'input'"));
final var output = Path.of(requireNonNull(configuration.get("output"), "No 'output'"));
if (Files.notExists(input)) {
throw new IllegalArgumentException("Input does not exist '" + input + "'");
}
try (final var mapper = new JsonMapperImpl(List.of(), c -> empty())) {
final var openrpc = asObject(mapper.fromString(Object.class, Files.readString(input)));
final var adoc = toAdoc(openrpc);
if (output.getParent() != null) {
Files.createDirectories(output.getParent());
}
Files.writeString(output, adoc);
} catch (final IOException ioe) {
throw new IllegalStateException(ioe);
}
}

private String toAdoc(final Map<String, Object> openrpc) {
public String convert(final Map<String, Object> openrpc, final JsonMapper ignored) {
final var methods = openrpc.get("methods");
if (!(methods instanceof Map<?, ?> mtd)) {
return "";
Expand Down Expand Up @@ -139,9 +117,4 @@ private String type(final Map<String, Object> schemas, final Map<String, Object>
default -> type;
};
}

@SuppressWarnings("unchecked")
private Map<String, Object> asObject(final Object o) {
return (Map<String, Object>) o;
}
}
Loading

0 comments on commit 9c97c04

Please sign in to comment.