All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.morimekta.providence.graphql.gql.GQLUtil Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
package net.morimekta.providence.graphql.gql;

import net.morimekta.providence.PMessage;
import net.morimekta.providence.descriptor.PField;
import net.morimekta.util.Binary;
import net.morimekta.util.Strings;
import net.morimekta.util.json.JsonWriter;

import java.io.PrintWriter;
import java.io.StringWriter;

public class GQLUtil {
    public static String toArgumentString(Object o) {
        if (o instanceof PMessage) {
            PMessage      m       = (PMessage) o;
            StringBuilder builder = new StringBuilder("{");
            boolean       first   = true;
            for (PField pf : m.descriptor().getFields()) {
                if (m.has(pf.getId())) {
                    if (!first) {
                        builder.append(", ");
                    } else {
                        first = false;
                    }
                    builder.append(pf.getName())
                           .append(": ")
                           .append(toArgumentString(m.get(pf.getId())));
                }
            }
            return builder.append("}").toString();
        } else if (o instanceof Binary) {
            return "\"" + ((Binary) o).toBase64() + "\"";
        } else if (o instanceof CharSequence) {
            StringWriter writer = new StringWriter();
            new JsonWriter(new PrintWriter(writer))
                    .value((CharSequence) o)
                    .flush();
            return writer.toString();
        } else {
            return Strings.asString(o);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy