You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by lenz <no...@googlemail.com> on 2010/01/08 02:43:33 UTC

small issue with a strange view

hi,

i do stuff the clumsy way and overlook something completely obvious.
my issue is that i try to map some very ugly API responses into a much
saner data structure in a view. i did that in the first run simply
this way:

function(doc) {
  emit(doc.domain,
    {domain: doc.domain,
     auth: doc.PROPERTY.AUTH[0],
     created: doc.PROPERTY.CREATEDDATE[0],
     expire: doc.PROPERTY.EXPIRATIONDATE[0],
     ns: doc.PROPERTY.NAMESERVER,
     status: doc.PROPERTY.STATUS,
     renew: doc.PROPERTY.RENEWALMODE[0],
     owner: {
       firstname: doc.PROPERTY.OWNERCONTACT.PROPERTY.FIRSTNAME[0],
       lastname: doc.PROPERTY.OWNERCONTACT.PROPERTY.LASTNAME[0],
       company: doc.PROPERTY.OWNERCONTACT.PROPERTY.ORGANIZATION[0],
       street: doc.PROPERTY.OWNERCONTACT.PROPERTY.STREET[0],
     .....

then reality caught me and i noticed that some of the values i try to
assign are actually not always there so i came up with this:

      .......

       firstname: doc.PROPERTY.OWNERCONTACT.PROPERTY.FIRSTNAME[0] || '',
       lastname: doc.PROPERTY.OWNERCONTACT.PROPERTY.LASTNAME[0] ||
doc.PROPERTY.OWNERCONTACT.PROPERTY.NAME[0],
       company: doc.PROPERTY.OWNERCONTACT.PROPERTY.ORGANIZATION[0] || '',
       street: doc.PROPERTY.OWNERCONTACT.PROPERTY.STREET[0] || '',
      .......

but obviously i get still errors as for example the
doc.PROPERTY.OWNERCONTACT.PROPERTY.FIRSTNAME key is not defined at all
and so the array i try to get the first element of is undefined and
throws an error.

any sane way i can do it nicely in the emit function or do i have to
write a proper parser :-)

cheers
lenz

-- 
iWantMyName.com
painless domain registration (finally)

Re: small issue with a strange view

Posted by Chris Anderson <jc...@apache.org>.
On Thu, Jan 7, 2010 at 5:43 PM, lenz <no...@googlemail.com> wrote:
> hi,
>
> i do stuff the clumsy way and overlook something completely obvious.
> my issue is that i try to map some very ugly API responses into a much
> saner data structure in a view. i did that in the first run simply
> this way:
>
> function(doc) {
>  emit(doc.domain,
>    {domain: doc.domain,
>     auth: doc.PROPERTY.AUTH[0],
>     created: doc.PROPERTY.CREATEDDATE[0],
>     expire: doc.PROPERTY.EXPIRATIONDATE[0],
>     ns: doc.PROPERTY.NAMESERVER,
>     status: doc.PROPERTY.STATUS,
>     renew: doc.PROPERTY.RENEWALMODE[0],
>     owner: {
>       firstname: doc.PROPERTY.OWNERCONTACT.PROPERTY.FIRSTNAME[0],
>       lastname: doc.PROPERTY.OWNERCONTACT.PROPERTY.LASTNAME[0],
>       company: doc.PROPERTY.OWNERCONTACT.PROPERTY.ORGANIZATION[0],
>       street: doc.PROPERTY.OWNERCONTACT.PROPERTY.STREET[0],
>     .....
>
> then reality caught me and i noticed that some of the values i try to
> assign are actually not always there so i came up with this:
>
>      .......
>
>       firstname: doc.PROPERTY.OWNERCONTACT.PROPERTY.FIRSTNAME[0] || '',
>       lastname: doc.PROPERTY.OWNERCONTACT.PROPERTY.LASTNAME[0] ||
> doc.PROPERTY.OWNERCONTACT.PROPERTY.NAME[0],
>       company: doc.PROPERTY.OWNERCONTACT.PROPERTY.ORGANIZATION[0] || '',
>       street: doc.PROPERTY.OWNERCONTACT.PROPERTY.STREET[0] || '',
>      .......
>
> but obviously i get still errors as for example the
> doc.PROPERTY.OWNERCONTACT.PROPERTY.FIRSTNAME key is not defined at all
> and so the array i try to get the first element of is undefined and
> throws an error.
>
> any sane way i can do it nicely in the emit function or do i have to
> write a proper parser :-)
>

Shouldn't be too hard to write a function you can call with a path to
the data, that will stop digging if anything's missing:

lazyValue(doc, "foo.bar.BAM[3]")



> cheers
> lenz
>
> --
> iWantMyName.com
> painless domain registration (finally)
>



-- 
Chris Anderson
http://jchrisa.net
http://couch.io