Coder Social home page Coder Social logo

Comments (14)

rharter avatar rharter commented on June 28, 2024 3

The idea of a ContentValuesBuilder might be simplified by simply adding a toContentValues() method to the generated class, so you could use it like this:

ContentValues values = User.builder()
  .id(id)
  .name(name)
  .email(email)
  .build()
  .toContentValues();

The @ContentValuesBuilder seems inflexible in that the user would have to add both Builder and ContentValuesBuilder inner classes in order to use it. I wonder if the auto-value-cursor generated class could simply override the $AutoValue_User.Builder class to add a public ContentValues asContentValues() method so that, if the class has an @AutoValue.Builder inner class, the ContentValues addition can be added with no extra code.

My suspicion is the the extension support in AutoValue will have to be extended to the Builder in order to accommodate this type of functionality.

from auto-value-cursor.

gabrielittner avatar gabrielittner commented on June 28, 2024 2

I'll add support for that, but I'm currently a bit busy. Maybe this or next weekend. If you want to give it a try before that I'm happy about pull requests 😃

from auto-value-cursor.

laurencedawson avatar laurencedawson commented on June 28, 2024 1

Can confirm this is working great. Thanks again for your help.

from auto-value-cursor.

gabrielittner avatar gabrielittner commented on June 28, 2024

Yes it is possible.

It would even be possible to generate a ContentValues Builder class similar AutoValue Builders for ContentValues. Something like this:

@AutoValue public abstract class User {
  abstract String id();
  abstract String name();
  abstract String email();

  @ContentValuesBuilder
  abstract static class ContentValuesBuilder {
    abstract ContentValuesBuilder id(String id);
    abstract ContentValuesBuilder name(String name);
    abstract ContentValuesBuilder email(String email);
    abstract ContentValues build();
  }

  // returns ContentValuesBuilder with values from current object
  abstract ContentValuesBuilder toContentValuesBuilder();

  public static User create(Cursor cursor) {
    return AutoValue_User.createFromCursor(cursor);
  }
}

from auto-value-cursor.

rharter avatar rharter commented on June 28, 2024

Re: the toContentValues method, you could simply check the class for an abstract method returning ContentValues, and implement that to convert the class to ContentValues and return it.

from auto-value-cursor.

gabrielittner avatar gabrielittner commented on June 28, 2024

Yeah a regular toContentValues() should definitely be done and is super easy to implement.
I just thought that it might be nice to have the ContentValuesBuilder on top of that. One major advantage of having it would be that you can use it for partial updates like only updating name and email. That would only work with either a separate Builder or extending the default Builder, because build().toContentValues() would do null checks.

from auto-value-cursor.

gabrielittner avatar gabrielittner commented on June 28, 2024

@laurencedawson The latest 0.3.0-SNAPSHOT will now generate a toContentValues() if you add an abstract one to your class.

from auto-value-cursor.

laurencedawson avatar laurencedawson commented on June 28, 2024

@gabrielittner Fantastic thanks!

from auto-value-cursor.

laurencedawson avatar laurencedawson commented on June 28, 2024

@gabrielittner sorry but can you provide a quick example? Thanks

from auto-value-cursor.

gabrielittner avatar gabrielittner commented on June 28, 2024

You just need to change the dependency to the snapshot version and add public abstract ContentValues toContentValues() to your AutoValue class.

from auto-value-cursor.

laurencedawson avatar laurencedawson commented on June 28, 2024

Have you published the snapshot or do you mean built it and add locally?

from auto-value-cursor.

gabrielittner avatar gabrielittner commented on June 28, 2024

Snapshots are published to a different repository. You have to add

maven {
    url "https://oss.sonatype.org/content/repositories/snapshots"
}

to the repositories block in your build.gradle file.

from auto-value-cursor.

laurencedawson avatar laurencedawson commented on June 28, 2024

Aha thanks!

from auto-value-cursor.

laurencedawson avatar laurencedawson commented on June 28, 2024

I created another issue but I think it's probably best discussed in here.

If a custom class is defined and setup with the @CursorAdapter annotation, the toContentValues method will cause the build to fail throwing the following error message:

Error:Property "Y" has type "X" that can't be put into ContentValues

Would it be possible to add along side the CursorAdapterFactory (containing createFromCursor) a method to saveToCursorValues?

Thanks

from auto-value-cursor.

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.