Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting a request/response back to its builder #91

Open
fredy-scs opened this issue Jan 4, 2022 · 6 comments
Open

Converting a request/response back to its builder #91

fredy-scs opened this issue Jan 4, 2022 · 6 comments
Assignees
Labels
Category: Enhancement New feature or request tracking

Comments

@fredy-scs
Copy link

I am trying to implement a generic deep pagination function that takes a SearchRequest, creates a point-in-time and then paginates through all the values using search_after.

This requires modifying the search request I am given to include the PIT and set the search_after field correctly. However, using the new client, I am unable to modify an existing request due to all data being immutable and all mutability being contained within the corresponding builder classes.

A solution to this problem could be adding a generated method that creates a builder from the values contained within the object. Alternatively, a new constructor could be added to the builder classes that take a constructed object and initialize their values from the object.

@swallez swallez self-assigned this Jan 4, 2022
@swallez swallez added the Category: Enhancement New feature or request label Jan 4, 2022
@swallez
Copy link
Member

swallez commented Jan 4, 2022

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.

@swallez swallez added the v7.16.2 label Jan 4, 2022
@swallez swallez added this to In progress in Test project Jan 6, 2022
@swallez swallez removed this from In progress in Test project Jan 6, 2022
@Link3750
Copy link

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

@visualage
Copy link

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;
  }

@jerryguowei
Copy link

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

@swallez swallez removed the v7.16.2 label Aug 17, 2022
@sothawo
Copy link
Contributor

sothawo commented Dec 10, 2022

any news on this one?

@mdgilene
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Enhancement New feature or request tracking
Projects
None yet
Development

No branches or pull requests

8 participants