Return-Path: X-Original-To: apmail-avro-dev-archive@www.apache.org Delivered-To: apmail-avro-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5835310DC4 for ; Wed, 17 Dec 2014 23:36:17 +0000 (UTC) Received: (qmail 36695 invoked by uid 500); 17 Dec 2014 23:36:17 -0000 Delivered-To: apmail-avro-dev-archive@avro.apache.org Received: (qmail 36591 invoked by uid 500); 17 Dec 2014 23:36:16 -0000 Mailing-List: contact dev-help@avro.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@avro.apache.org Delivered-To: mailing list dev@avro.apache.org Received: (qmail 36353 invoked by uid 99); 17 Dec 2014 23:36:16 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Dec 2014 23:36:16 +0000 Date: Wed, 17 Dec 2014 23:36:16 +0000 (UTC) From: "Hudson (JIRA)" To: dev@avro.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (AVRO-1614) Always getting a value... MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/AVRO-1614?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14250816#comment-14250816 ] Hudson commented on AVRO-1614: ------------------------------ SUCCESS: Integrated in AvroJava #506 (See [https://builds.apache.org/job/AvroJava/506/]) AVRO-1614. Java: In generated builder classes, add accessors to field sub-builders, permitting easier creation of nested, optional structures. Contributed by Niels Basjes. (cutting: rev 1646360) * /avro/trunk/CHANGES.txt * /avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java * /avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java * /avro/trunk/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm * /avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificBuilderTree.java * /avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java * /avro/trunk/lang/java/tools/src/test/compiler/output/Player.java * /avro/trunk/share/test/schemas/http.avdl * /avro/trunk/share/test/schemas/specialtypes.avdl > Always getting a value... > ------------------------- > > Key: AVRO-1614 > URL: https://issues.apache.org/jira/browse/AVRO-1614 > Project: Avro > Issue Type: New Feature > Components: java > Reporter: Niels Basjes > Assignee: Niels Basjes > Labels: java > Fix For: 1.8.0 > > Attachments: AVRO-1614-2014-12-16-v5.patch, AVRO-1614-20141027-v1.patch, AVRO-1614-20141201-v2.patch, AVRO-1614-20141202-v3.patch, AVRO-1614-20141204-v4.patch > > > Sometimes the Avro structure becomes deeply nested. > If in such a scenario you want to be able to set a specific value deep in this tree you want to do this: > {code} > public void setSomething(String value) { > myStruct > .getFoo() > .getBar() > .getOne() > .getOther() > .setSomething(value); > } > {code} > The 'problem' I ran into is that any of the 4 get methods can return a null value so the code I have to write is really huge. > For every step in this method I have to build null checks and create the underlying instance if it is null. > I already started writing helper methods to do this for parts of my tree. > To solve this in a way that makes this code readable I came up with the following which I want to propose to you guys (before I start working on a patch). > My idea is to generate a new 'get' method in addition to the existing normal get method for the regular instance of the class. > So in addition to the > {code} > public Foo getFoo() { > return foo; > } > {code} > I propose to generate something like this as well in the cases where this is a type of structure that you may want to traverse as shown in the example. > {code} > public Foo getAlwaysFoo() { > if (foo == null) { > setFoo(Foo.newBuilder().build()); > } > return foo; > } > {code} > This way the automatically created instance immediately has all the defaults I have defined. > Assuming this naming my code will be readable because it will look like this: > {code} > public void setSomething(String value) { > myStruct > .getAlwaysFoo() > .getAlwaysBar() > .getAlwaysOne() > .getAlwaysOther() > .setSomething(value); > } > {code} -- This message was sent by Atlassian JIRA (v6.3.4#6332)