You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Lewis John Mcgibbney <le...@gmail.com> on 2013/10/26 12:41:41 UTC

Setting field default value's programmatically

Hi user@,
Over in Apache Gora we've nearly made the upgrade of our Avro dependency
from legacy 1.3.3 to 1.7.5. For those that may be interested, the branch is
here [0].
The current issue I am working on concerns the following

In Gora we define our objects using Avro Schema's before using our
Velocity-driven [1] GoraCompiler [2] to compile the schema's down into
persistent Java beans.

Right now we wish for our compiler to recognize if a field default value
(within the schema is absent) and simply enforce a default value of null.

An example schema can be found here [3]. So instead of forcing the user to
define

{"name": "url", "type": "string", "default": ""},

we would instead prefer for them to be able to assume that a field can/may
be empty and they
can carry or as usual like so...

{"name": "url", "type": "string"},

Now, as an example to explain the problem I am having, if we assume that
the example schema [3] has been compiled and that I am constructing a new
persistent WebPage object like so

WebPage page1 = WebPage.newBuilder().build();

we come back to the velocity template [1] build() method where the default
value for each field is specified like so

(${this.javaType($field.schema())}) defaultValue(fields()[$field.pos()]);

We've therefore tried substituting the above with a function which does the
following

  private ${this.javaType($field.schema())} getNewValue() {
     ${this.javaType($field.schema())} newValue = null;
     if (defaultValue(fields()[$field.pos()]) != null) {
         newValue = (${this.javaType($field.schema())})
defaultValue(fields()[$field.pos()]);
     }
     return newValue;
  }

However I always get the following
AvroRuntimeException: Field url type:STRING pos:1 not set and has no
default value

Can someone please put me out of my short term misery and explain

1) Why my code is not working in this case? and
2) Why org.apache.avro.data.RecordBuilderBase#defaultValue(Field field) is
not returning null if no default is specified in the schema... which is the
case?

Apologies for the long post. I hope I've explained thoroughly enough. I
really thank you for your time here.

Lewis


[0] https://svn.apache.org/repos/asf/gora/branches/GORA_94/
[1] http://s.apache.org/CAX
[2] http://s.apache.org/lMZ
[3] http://s.apache.org/rF

-- 
*Lewis*

Re: Setting field default value's programmatically

Posted by Lewis John Mcgibbney <le...@gmail.com>.
Hi Doug,
Thanks for reading and for reply.

On Sun, Oct 27, 2013 at 5:25 PM, Doug Cutting <cu...@apache.org> wrote:

> That said,
> one could define an alternate method that returns a type-specific
> default value when none is specified.


This is more like what I had in mind. Until I discussed this with another
Gora committer and saw the rest of your reply I was not sure if this should
be addressed on the Gora-side or if it is something we can work on for
Avro.


> Alternately, one could traverse
> schemas and populate fields that have no specified default with a
> type-specific default.  I haven't looked at the Gora code in question,
> but perhaps there's an opportunity to pre-process the schemas there,
> prior to Avro code generation?
>
>
This sounds sensible and much more in fit with what we are after. This was
more or less what we were trying to achieve by setting the default value to
null (within the compiler) on a field-by-field basis if no default value
was explicitly specified within the Avro schema. I think we'll progress on
this basis.

Thanks for the thoughts.
Lewis

Re: Setting field default value's programmatically

Posted by Doug Cutting <cu...@apache.org>.
On Sat, Oct 26, 2013 at 3:41 AM, Lewis John Mcgibbney
<le...@gmail.com> wrote:
> 2) Why org.apache.avro.data.RecordBuilderBase#defaultValue(Field field) is
> not returning null if no default is specified in the schema... which is the
> case?

It returns null when null is the default value, but throws an error
when no default value is specified.  This is required for correct
behavior, since null values are invalid for many types.  That said,
one could define an alternate method that returns a type-specific
default value when none is specified.  Alternately, one could traverse
schemas and populate fields that have no specified default with a
type-specific default.  I haven't looked at the Gora code in question,
but perhaps there's an opportunity to pre-process the schemas there,
prior to Avro code generation?

Cheers,

Doug