Fri Aug 19, 2022 5:26 pm
Login Register Lost Password? Contact Us

Please Note: The HPCC Systems forums are moving to Stack Overflow. We invite you to post your questions on Stack Overflow utilizing the tag hpcc-ecl (https://stackoverflow.com/search?tab=newest&q=hpcc-ecl). This legacy forum will be active and monitored during our transition to Stack Overflow but will become read only beginning September 1, 2022.



Default Value in RECORD

Comments and questions related to the Enterprise Control Language

Wed Jul 24, 2019 9:08 pm Change Time Zone

Hi,

Is it possible to provide a Default Value for a given field in a RECORD statement? I need to add a new field in some layouts, which are already being used and I don't wanna to change old code, that already use this layout. So, my idea would be add a new field to this layout and set a default value for it and change it only when I use it in my new code.

I tried this, but didn't worked:
Code: Select all
EXPORT myLayout := RECORD
   STRING oldvalue1;
   STRING oldvalue2;
   ....
   STRING newValue1 = '';
END;


But every time try this, I see an error telling that I should provide a value to newValue1.

Thanks,

Att.
Artur Baruchi
abaruchi
 
Posts: 19
Joined: Thu Apr 18, 2019 4:50 pm

Wed Jul 24, 2019 9:19 pm Change Time Zone

Artur,

The default value for a field in a RECORD structure requires use of the definition operator (:=) so your code example should be:
Code: Select all
EXPORT myLayout := RECORD
   STRING oldvalue1;
   STRING oldvalue2;
   ....
   STRING newValue1 := '';
END
You should also look at possibly using the {DEFAULT('')} field modifier, like this:
Code: Select all
EXPORT myLayout := RECORD
   STRING oldvalue1;
   STRING oldvalue2;
   ....
   STRING newValue1{DEFAULT('')};
END

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1619
Joined: Wed Oct 26, 2011 7:40 pm

Thu Jul 25, 2019 1:53 pm Change Time Zone

Hi Richard, thanks for your reply.

Code: Select all
// Deprecated Layout
EXPORT lMyLayout01 := RECORD
  STRING   message {XPATH('messagge')};
  UNSIGNED code    {XPATH('code')};
END;

// New Record Layout
EXPORT lMyLayout02 := RECORD
  STRING   message2 {XPATH('Message')};
  UNSIGNED code2    {XPATH('Code')};
END;

// Main record structure that uses both layouts.
EXPORT lMyFinalLayout := RECORD
  lMyLayout01 MyError;
  lMyLayout02 MyError2 := [];
END;

r01 := ROW({'abc', 123}, lMyLayout01);

t01 := ROW(TRANSFORM(lMyFinalLayout,
                     SELF.MyError  := r01;));


When I run this code, I got an error saying that I didn't provide values to MyError2.message2 and MyError2.code2. As far as I understand, since I defined MyError2 in lMyFinalLayout as an empty row, t01 transform shouldn't care about it.

Thanks,

Att.
Artur Baruchi
abaruchi
 
Posts: 19
Joined: Thu Apr 18, 2019 4:50 pm

Thu Jul 25, 2019 7:33 pm Change Time Zone

Artur,

The TRANSFORM must supply a value for every field in the result RECORD structure. That's why you get the error. The DEFAULT field modifier works like this:
Code: Select all
// Deprecated Layout
EXPORT lMyLayout01 := RECORD
  STRING   message {XPATH('messagge')};
  UNSIGNED code    {XPATH('code')};
END;

// New Record Layout
EXPORT lMyLayout02 := RECORD
  STRING   message2 {XPATH('Message'),DEFAULT('default')};
  UNSIGNED code2    {XPATH('Code'),DEFAULT(42)};
END;

// Main record structure that uses both layouts.
EXPORT lMyFinalLayout := RECORD
  lMyLayout01 MyError;
  lMyLayout02 MyError2;
END;

r01 := ROW({'abc', 123}, lMyLayout01);

t01 := ROW(TRANSFORM(lMyFinalLayout,
                     SELF.MyError  := r01;
                     SELF := []));
r01;
t01;
Notice that I added the DEFAULT modifier to the fields, then added the SELF := [] to the TRANSFORM to to handle the errors. The DEFAULT values are what show up as the result of the SELF := [].

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1619
Joined: Wed Oct 26, 2011 7:40 pm


Return to ECL

Who is online

Users browsing this forum: No registered users and 1 guest