How to set default values in a SEQUENCE OF object - pyasn1

I'm starting to use the pyasn1 library and I have a question on how to set default values in a SEQUENCE OF object. My ASN1 structure is the following:
Asn1Def DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
CasinoPlayer ::= SEQUENCE
{
name UTF8String (SIZE(1..16)),
luckyNumbers SEQUENCE (SIZE(3)) OF INTEGER DEFAULT {7,7,7}
}
END
I understood how to create a DEFAULT field in the CasinoPlayer SEQUENCE using namedtype.DefaultedNamedType objects and using subtype to add SIZE constraint but how shall I initialize the default value {7,7,7}?
Thank you

I am thinking it should look like this:
class CasinoPlayer(Sequence):
componentType = NamedTypes(
NamedType(
'name',
UTF8String(
ConstraintsIntersection(
ValueSizeConstraint(1, 16)
)
)
),
DefaultedNamedType(
'luckyNumbers',
SequenceOf(
componentType=Integer(),
sizeSpec=ConstraintsIntersection(
ValueSizeConstraint(3, 3)
)
).setComponentByPosition(0, 7)
.setComponentByPosition(1, 7)
.setComponentByPosition(2, 7)
)
)
Plus you probably need to assign the tags to each ASN.1 type (as it's implied by the AUTOMATIC TAGS clause).
UPDATE:
Actually, this should work, but it does not! Luckily, there is the fix which should make defaulted SequenceOf propagating to the Sequence field for as long as it's a DefaultedNamedType.

Related

Fetch value from XML using dynamic tag in ESQL

I have an xml
<family>
<child_one>ROY</child_one>
<child_two>VIC</child_two>
</family>
I want to fetch the value from the XML based on the dynamic tag in ESQL. I have tried like this
SET dynamicTag = 'child_'||num;
SET value = InputRoot.XMLNSC.parent.(XML.Element)dynamicTag;
Here num is the value received from the input it can be one or two. The result should be value = ROY if num is one and value is VIC if num is two.
The chapter ESQL field reference overview describes this use case:
Because the names of the fields appear in the ESQL program, they must be known when the program is written. This limitation can be avoided by using the alternative syntax that uses braces ( { ... } ).
So can change your code like this:
SET value = InputRoot.XMLNSC.parent.(XMLNSC.Element){dynamicTag};
Notice the change of the element type as well, see comment of #kimbert.

0 xsd:anyURI using esql.IBM Integration Bus

<payload xsi:type="ns787:SomeRequest" xmlns:ns787="http://ws.abc.efg.com"/>
I'm working with IIB v10.0.0.7. I'd like to define one of the XML elements to be of type xsd:anyURI using esql. output should be like given above:
I assume that you are asking about the xsi:type attribute. The XMLNSC parser does not automatically populate the namespace prefix in the value of the xsi:type attribute - you must set the entire value (prefix:localName) as a literal text string.
DECLARE namespace787 NAMESPACE 'http://ws.abc.efg.com';
DECLARE namespaceXSI NAMESPACE 'http://www.w3.org/2001/XMLSchema-instance';
CREATE LASTCHILD OF OutputRoot.XMLNSC.payload TYPE XMLNSC.Attribute NAMESPACE namespaceXSI NAME 'type' VALUE 'ns787:SomeRequest';
CREATE LASTCHILD OF OutputRoot.XMLNSC.payload TYPE XMLNSC.NamespaceDecl NAME 'xmlns:ns787' VALUE namespace787;
or, if you prefer using SET instead of CREATE:
...
SET OutputRoot.XMLNSC.payload.(XMLNSC.Attribute)namespaceXSI:type = namespaceXSI;
SET OutputRoot.XMLNSC.payload.(XMLNSC.NamespaceDecl)xmlns:ns787 = namespace787;
If you are still stuck then you may find this technique useful: How to create a complex object in ESQL

Get components of DDIC structure with includes

I am using cl_abap_structdescr->get_components to get a list of fields in a structure. It works fine when I use it on locally declared structure types, but when I use it on DDIC structures, it doesn't give me the results I expect.
Reproducable example:
TYPES: BEGIN OF gty_outtab,
infty TYPE infty,
uname TYPE uname,
bdate TYPE datum,
btime TYPE uzeit,
pernr TYPE pernr_d,
opera TYPE hr_opera,
begda TYPE begda,
endda TYPE endda,
END OF gty_outtab.
DATA: lr_infty_structdescr TYPE REF TO cl_abap_structdescr,
lr_outtab_structdescr TYPE REF TO cl_abap_structdescr,
lt_outtab_components TYPE STANDARD TABLE OF abap_componentdescr,
lt_infty_components TYPE STANDARD TABLE OF abap_componentdescr.
" works as expected
lr_outtab_structdescr ?= cl_abap_structdescr=>describe_by_name( 'GTY_OUTTAB' ).
lt_outtab_components = lr_outtab_structdescr->get_components( ).
" doesn't work as expected
lr_infty_structdescr ?= cl_abap_structdescr=>describe_by_name( 'P0008' ).
lt_infty_components = lr_infty_structdescr->get_components( ).
BREAK-POINT.
Results:
That's okay for GTY_OUTTAB:
There are only two fields for P0008 although it contains many more fields (see below):
I already tried using cl_abap_typedescr instead and googled, but every code I find online looks just like mine?
Here is the definition of P0008 which contains many fields as you can see:
Of course after posting this, I found the reason why (german thread). Apparently, if the given structure contains included structures, then get_components won't break them up. Three solutions have been suggested in the thread and they all work great for me. Since I only need the structures' fieldnames, I will use option 1.
DATA: lt_infty_complist1 TYPE abap_compdescr_tab,
lt_infty_complist2 TYPE STANDARD TABLE OF fieldname,
lt_infty_complist3 TYPE abap_component_tab.
" 1 - get full fieldname list, but with barely any typedescription
lt_infty_complist1 = lr_infty_structdescr->components.
" 2 - get full fieldname list of DDIC structures, but without typedescription
SELECT fieldname
FROM dd03l
INTO TABLE lt_infty_complist2
WHERE tabname = 'P0008'.
DELETE lt_infty_complist2 WHERE table_line = '.INCLU--AP'
OR table_line = '.INCLUDE'.
" 3 - get full component list
" function code from: https://www.abapforum.com/forum/viewtopic.php?f=18&p=59840)
PERFORM return_components USING lr_infty_structdescr CHANGING lt_infty_complist3.
There is a method on cl_abap_structdescr called get_included_view( ) that will expand the included structures
Method GET_RTTS_FOR_LOCAL_TABLE of helper-class CL_CACS_RTTS_HELPER seems to do exactly what you want and what lacks your option 1
CALL METHOD cl_cacs_rtts_helper=>get_rtts_for_local_structure
EXPORTING
id_tabname = 'P0008'
receiving
ro_data = DATA(ro_struct)
.
It fetches all components of a structure into reference data object and also includes absolute types:
In the same class there is get field list method, which may be sufficient.
data: lo_incl_stru TYPE REF TO cl_abap_structdescr,
lt_field_list TYPE ddfields.
lt_field_list = lo_incl_stru->get_ddic_field_list( p_including_substructres = abap_true ).
If that isnt enough.... try
METHODS recursive_get_components
IMPORTING
io_structdescr TYPE REF TO cl_abap_structdescr
RETURNING
VALUE(rt_components) TYPE abap_component_tab.
METHOD recursive_get_components.
DATA:
lo_incl_stru TYPE REF TO cl_abap_structdescr,
lt_incl_comp TYPE abap_component_tab,
l_curr_index TYPE i.
FIELD-SYMBOLS: <comp> LIKE LINE OF rt_components,
<incl_comp> LIKE LINE OF lt_incl_comp.
rt_components = io_structdescr->get_components( ).
LOOP AT rt_components ASSIGNING <comp>.
IF <comp>-as_include = 'X'.
lo_incl_stru ?= <comp>-type. " not the include struc type
l_curr_index = sy-tabix. " and the index it is to be included
DELETE rt_components INDEX l_curr_index.
lt_incl_comp = recursive_get_components( io_structdescr = lo_incl_stru ).
LOOP AT lt_incl_comp ASSIGNING <incl_comp>.
INSERT <incl_comp> INTO rt_components INDEX l_curr_index.
l_curr_index = l_curr_index + 1.
ENDLOOP.
ENDIF.
ENDLOOP.
ENDMETHOD.

What's the difference between 'Array<_>' & 'Array'

I'm getting this error when trying to build the app:
<unknown>:0: error: cannot assign value of type 'Array<_>' to type 'Array'
but Xcode is not indicating a specific line or class for the failure.
If I could understand the difference between
Array<_>
&
Array
it may help me locate the issue.
When you app is crashing, than you can turn on All Exceptions for Debug breakpoint. This should stop on the line where the crash appears.
You find this in Xcode on the Left Panel-> BreakPoint Navigator.
Than press the + in the bottome left corner and Add Exception Breakpoint.
It looks like that you overwrite an array with an array that has a specific value definition. Good Luck :)
A generic argument clause is enclosed in angle brackets ( < > )
< generic argument list >
You can replace a type parameter with a type argument that is itself a specialized version of a generic type.For example, you can replace the type parameter T in Array < T > with a specialized version of an array, Array< Int > , Array< String >, to form an array whose elements are themselves array of integers / Strings
Regarding xour code Snippet the fix is:
u should define it with the right value ... userTweetsArray : [String] = String . or remove the : [String] because setting the value defines already the object and type :)
I don't like answering my own questions but for the sake of closure
I had this line of code
var userTweetsArray : Array = [String]()
I never actually used it. Once I removed that line the error had gone.
The error was caused by assigning an array of a type in this case String to an Array of no type.
So difference is Array<_> is an array of a type and Array is not.

How do I use parameterized types in ruby-pg (the new postgres library for ruby)?

The documentation ( http://deveiate.org/code/pg/PG/Connection.html#method-i-exec ) suggests that to insert types like booleans or dates, I should use code like this:
db_object.exec("INSERT INTO dan_test_1 (one) VALUES ($1)",
{ :value => "true",
:type => 16,
:format => 1
} );
(I looked up "16" in the pg_type table.)
But I keep on getting errors like "can't format (PGError)".
I can insert strings and numbers, just fine, but how do I handle these other common types?
The docs you link to actually recommend using common text formats ('t' for true, '2012-11-26' for dates, ...) and explicit casts in the SQL if needed:
Instead of specifying type oids, it’s recommended to simply add explicit casts in the query to ensure that the right type is used.
So if one is a boolean column, you would want this:
db_object.exec("INSERT INTO dan_test_1 (one) VALUES ($1)", [ 't' ])
Sequel may help you but you really should know what's going on behind your back anyway.

Resources