Re: [protobuf] [Java] Proper way to add an item to a List?

2017-07-11 Thread Laird Nelson
On Tuesday, July 11, 2017 at 5:09:30 PM UTC-7, Feng Xiao wrote:
>
> Actually you can't add a sub-builder to another builder. If you look at 
> the implementation of addDependencies(Builder), it just calls 
> builder.build() right away and add the resulted Chart object:
>
> https://microbean.github.io/microbean-helm/apidocs/src-html/hapi/chart/ChartOuterClass.Chart.Builder.html#line.1680
>

You're quite right of course; not sure how I missed that.
 

> You can instead get a sub-builder out from an existing builder, by calling 
> the getFieldBuilder() method, e.g., getDependenciesBuilder(int). This 
> sub-builder you get is "owned" by the parent builder and they are 
> cross-referenced: if you call build() on the parent builder, it will 
> recursively call build() on all its sub-builders; if you change the 
> sub-builder, its parent builder will get notified for the change.
>

And I add a sub-builder by...calling...addDependenciesBuilder(), with no 
arguments.  I guess.  This all feels very, very weird.

I wish I could use the actual top-level Chart.Builder to keep track of his 
tree of dependent Chart.Builders, but it looks like without some serious 
gymnastics this will be harder than just maintaining the hierarchy by hand 
out-of-band and then adding the immutable objects in the right places. 
 This feels cumbersome, which was why I was looking for a simpler way to do 
it.  Looks like there is no simpler way to do it.  Many thanks for your 
reply.

Best,
Laird

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.


Re: [protobuf] [Java] Proper way to add an item to a List?

2017-07-11 Thread 'Feng Xiao' via Protocol Buffers
On Tue, Jul 11, 2017 at 4:53 PM, Laird Nelson  wrote:

> I hope this is the right group.
>
> I've generated a Java API for Helm's Tiller using gRPC and protocol
> buffers.  You can see the javadocs here: https://microbean.
> github.io/microbean-helm/apidocs/index.html  Look at the hapi.* packages.
>
> As I'm working with the generated API, I need to add one kind of thing to
> another thing's list of such things.
>
> Specifically (and arbitrarily) I need to add a (sub-) Chart.Builder to a
> (top-level) Chart.Builder's "dependencies", only if it isn't already
> present.
>
> I can do this via the following generated methods:
> https://microbean.github.io/microbean-helm/apidocs/hapi/
> chart/ChartOuterClass.Chart.Builder.html#addDependencies-
> hapi.chart.ChartOuterClass.Chart.Builder- (Adds a Chart.Builder)
> https://microbean.github.io/microbean-helm/apidocs/hapi/
> chart/ChartOuterClass.Chart.Builder.html#addDependenciesBuilder-- (I
> don't know what this does.)
>
> It seems from looking at the generated source code that if I call build()
> on the "top level" Chart.Builder, it will cascade to the Chart.Builders
> present in the return value of getDependenciesBuilderList() and call
> build() on them too.
>
Actually you can't add a sub-builder to another builder. If you look at the
implementation of addDependencies(Builder), it just calls builder.build()
right away and add the resulted Chart object:
https://microbean.github.io/microbean-helm/apidocs/src-html/hapi/chart/ChartOuterClass.Chart.Builder.html#line.1680

You can instead get a sub-builder out from an existing builder, by calling
the getFieldBuilder() method, e.g., getDependenciesBuilder(int). This
sub-builder you get is "owned" by the parent builder and they are
cross-referenced: if you call build() on the parent builder, it will
recursively call build() on all its sub-builders; if you change the
sub-builder, its parent builder will get notified for the change.


>
> I also need to add a Chart.Builder only if it isn't present already.
>
> When I tried to implement this logic, I found that no Chart.Builder added
> is equal to any other Chart.Builder.  That is, there doesn't seem to be the
> ability to check for a Chart.Builder "in" another Chart.Builder's list of
> dependencies.
>
I think you can have some kind of id field in your Chart message and use
that to check whether a Chart is already added in dependencies?


> Finally at the end of all this, I call build() on the top-level
> Chart.Builder and expect it to produce a Chart with some subcharts in its
> dependencies.  Should I in fact expect this?
>
> Now I feel like I have the semantics about these generated methods all
> wrong.  Is there a better walkthrough than https://developers.
> google.com/protocol-buffers/docs/javatutorial?  Is there a standard way
> for assembling an object graph out of connected builders?
>
> Thanks,
> Best,
> Laird
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at https://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.