Git Product home page Git Product logo

Comments (6)

swallez avatar swallez commented on July 2, 2024

Thanks for the feedback @fredy-scs. This is indeed something we have considered but haven't implemented yet. I'll make sure it's listed explicitely on the roadmap.

from elasticsearch-java.

Link3750 avatar Link3750 commented on July 2, 2024

Aha, that's the problems I've faced too. Please notice me if it is solved.

from elasticsearch-java.

visualage avatar visualage commented on July 2, 2024

this is the workaround I have so far:

  public static <T extends JsonpSerializable, B extends ObjectBuilderBase> B toObjectBuilder(
      T obj,
      Supplier<B> createBuilder,
      Predicate<String> fieldFilter) {
    B builder = createBuilder.get();
    Class<?> objClass = obj.getClass();
    Class<?> builderClass = builder.getClass();

    for (Method method : objClass.getMethods()) {
      if (fieldFilter.test(method.getName()) && method.getParameterTypes().length == 0 &&
          !Void.class.equals(method.getReturnType())) {
        // look at public, no parameter, not void return type methods. likely field accessors
        // try look for a corresponding builder method
        Method builderMethod;

        try {
          builderMethod = builderClass.getMethod(method.getName(), method.getReturnType());
        } catch (NoSuchMethodException e) {
          // the corresponding method does not exist in builder, skip
          continue;
        }

        try {
          // see if this is part of an union field
          Method kindMethod = objClass.getMethod("is" + StringUtils.capitalize(method.getName()));
          if (kindMethod.getReturnType().equals(boolean.class)) {
            if (!(boolean) kindMethod.invoke(obj)) {
              // a union kind, and the current value is not this kind
              continue;
            }
          }
        } catch (NoSuchMethodException e) {
          // not a union field
        } catch (IllegalAccessException | InvocationTargetException e) {
          throw new IllegalStateException("Unable to convert to builder.", e);
        }

        try {
          builderMethod.invoke(builder, method.invoke(obj));
        } catch (IllegalAccessException | InvocationTargetException e) {
          throw new IllegalStateException("Unable to convert to builder.", e);
        }
      }
    }

    return builder;
  }

from elasticsearch-java.

jerryguowei avatar jerryguowei commented on July 2, 2024

We have the same problem, hope it can come soon!

from elasticsearch-java.

sothawo avatar sothawo commented on July 2, 2024

any news on this one?

from elasticsearch-java.

mdgilene avatar mdgilene commented on July 2, 2024

Was this abandoned? Would really like to see this functionality added.

I have a similar situation where I would like one part of my code to build the parts of the query that are context specific and have another part of my code "append" additional conditions to the query that are common to all requests. In my case it would be fields used for authorization.

from elasticsearch-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.