I'm using the PDI Spoon and wrote this script in Script Values to get values, check and transform:
var cte;
cte = gera_cte (CTRC.getString(), Numero_CTe.getString());
function gera_cte (arg1, arg2)
{
if (arg2 == 0) {
return arg1.substring(3,9);
}
else
{
return arg2.substring(4,10);
}
}
But when the script read the last record, this happen:
2016/02/05 17:28:40 - Gera Num Cte.0 - ERROR (version 5.0.1-stable, build 1 from 2013-11-15_16-08-58 by buildguy) : Erro inesperado
2016/02/05 17:28:40 - Gera Num Cte.0 - ERROR (version 5.0.1-stable, build 1 from 2013-11-15_16-08-58 by buildguy) : org.pentaho.di.core.exception.KettleValueException:
2016/02/05 17:28:40 - Gera Num Cte.0 - Javascript error:
2016/02/05 17:28:40 - Gera Num Cte.0 - TypeError: Cannot call method "substring" of null (script#12)
2016/02/05 17:28:40 - Gera Num Cte.0 -
2016/02/05 17:28:40 - Gera Num Cte.0 - at org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesMod.addValues(ScriptValuesMod.java:457)
2016/02/05 17:28:40 - Gera Num Cte.0 - at org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesMod.processRow(ScriptValuesMod.java:692)
2016/02/05 17:28:40 - Gera Num Cte.0 - at org.pentaho.di.trans.step.RunThread.run(RunThread.java:60)
2016/02/05 17:28:40 - Gera Num Cte.0 - at java.lang.Thread.run(Unknown Source)
2016/02/05 17:28:40 - Gera Num Cte.0 - Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot call method "substring" of null (script#12)
How can I solve this issue, because is in the end of file?
You probably have an extra line in your file, which makes it run with null values.
You should probably filter out the illegal rows before they reach your Java Script step, or check the input values in your function with something like:
function gera_cte (arg1, arg2)
{
if (arg1 == null) {
return arg1;
}
if (arg2 == 0) {
return arg1.substring(3,9);
}
else
{
return arg2.substring(4,10);
}
}
or setting trans_Status = SKIP_TRANSFORMATION; when the arguments are null.
If you are executing this script using the "Modified Javascript Value" step, you have this error because the javascript engine that PDI uses (Mozilla's Rhino engine) doesn't have a "substring" method. Try using "substr" instead:
// Perform the substring function
//
// Usage:
// substr(var, from);
// substr(var, from, to);
//
// 2007-01-25
//
var str1= "Hello Pentaho!";
var str2= substr(str1, 6);
var str3= substr(str1, 6, 7);
Alert("Input : " + str1);
Alert("From position 6: " + str2);
Alert("From position 6 for 7 long : " + str3);
Related
yesterday I updated my Vue project to the latest Bootstrap version (5.2) and now I have a SASS Error while starting.
ERROR Failed to compile with 1 error 10:45:42
error in ./src/assets/scss/config/modern/app.scss
Syntax Error: SassError: argument `$color` of `red($color)` must be a color
on line 185 of node_modules/bootstrap/scss/_functions.scss, in function `red`
from line 185 of node_modules/bootstrap/scss/_functions.scss, in function `luminance`
from line 174 of node_modules/bootstrap/scss/_functions.scss, in function `contrast-ratio`
from line 159 of node_modules/bootstrap/scss/_functions.scss, in function `color-contrast`
from line 7 of node_modules/bootstrap/scss/_accordion.scss
from line 29 of node_modules/bootstrap/scss/bootstrap.scss
from line 15 of src/assets/scss/config/modern/bootstrap.scss
from line 19 of src/assets/scss/config/modern/app.scss
>> "r": red($color),
It's about this code snippet:
// File: _functions.scss
// Return WCAG2.1 relative luminance
// See https://www.w3.org/TR/WCAG/#dfn-relative-luminance
// See https://www.w3.org/TR/WCAG/#dfn-contrast-ratio
#function luminance($color) {
$rgb: (
"r": red($color),
"g": green($color),
"b": blue($color)
);
#each $name, $value in $rgb {
$value: if(divide($value, 255) < .03928, divide(divide($value, 255), 12.92), nth($_luminance-list, $value + 1));
$rgb: map-merge($rgb, ($name: $value));
}
#return (map-get($rgb, "r") * .2126) + (map-get($rgb, "g") * .7152) + (map-get($rgb, "b") * .0722);
}
And that's the trigger:
// File: _accordion.scss
// scss-docs-start accordion-css-vars
--#{$prefix}accordion-color: #{color-contrast($accordion-bg)};
I know, the error says, the function gets a wrong parameter, which is not of type color, but I never user it my self.
How can I fix this error and why does it occur?
I'm writing my first odb code and can't get this basic working, although only the db connection code works:
/*! #file overview_record.h
*/
#ifndef OVERVIEW_RECORD_H
#define OVERVIEW_RECORD_H
#include <string>
#include <odb/core.hxx>
#include <odb/nullable.hxx>
#pragma db object table("mddb_overview") no_id
class overview_record
{
public:
#pragma db column("product_name") type("nvarchar(64)")
std::wstring product_name;
#pragma db column("order_number") type("int")
long order_number;
};
#endif
driver code:
// odb_playground.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <memory>
#include <thread>
#include <odb/core.hxx>
#include <odb/database.hxx>
#include <odb/mssql/database.hxx>
#include <odb/mssql/connection-factory.hxx>
#include <odb/mssql/exceptions.hxx>
#include "overview_record-odb.hxx"
int main(int argc, char* argv[])
{
try{
std::auto_ptr<odb::mssql::connection_pool_factory> connection_factory(
new odb::mssql::connection_pool_factory(0, std::thread::hardware_concurrency()));
std::unique_ptr<odb::database> db(
new odb::mssql::database("dsn=mddb_local_32", odb::mssql::isolation_read_committed, static_cast<SQLHENV>(0), connection_factory)
);
odb::transaction t(db->begin());
db->query<overview_record>();
//odb::result<overview_record> result();
//auto it = result.begin();
//while(true)
//{
// static int i = 0;
// if (i++ > 10)
// break;
// std::cout << "Order_number " << it->order_number << " product_name " << it->product_name << std::endl;
// ++i;
//}
t.commit();
}
catch (const odb::database_exception &e) {
std::cout << "ODB database error: " << e.what() << std::endl;
}
return 0;
}
Of course I odb-compiled the overview_record.h with odb.exe --database mssql overview_record.h (otherwise there won't be an .hxx). But I get the following compiler errors by the line db->query<overview_record>();, although instantiating a default constructed result works:
Error 3 error C2504: 'odb::result_base' : base class undefined c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 76 1 odb_playground
Error 4 error C2027: use of undefined type 'odb::result_base' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground
Error 5 error C2146: syntax error : missing ';' before identifier 'value_type' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground
Error 7 error C2602: 'odb::result::value_type' is not a member of a base class of 'odb::result' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground
Error 8 error C2868: 'odb::result::value_type' : illegal syntax for using-declaration; expected qualified-name c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 82 1 odb_playground
Error 9 error C2027: use of undefined type 'odb::result_base' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground
Error 10 error C2146: syntax error : missing ';' before identifier 'result_impl_type' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground
Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground
Error 12 error C2602: 'odb::result::result_impl_type' is not a member of a base class of 'odb::result' c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground
Error 13 error C2868: 'odb::result::result_impl_type' : illegal syntax for using-declaration; expected qualified-name c:\users\klm\downloads\libodb-2.4.0\odb\result.hxx 93 1 odb_playground
Problem was a missing flag of the odb compiler, in this case the -q e.g. --generate-query flag. Otherwise odb won't add the needed code to the generated files.
So the correct invocation would be odb.exe -q --database mssql overview_record.h
Can anyone suggest me what is the difference between
- (void)tabtwoAction:(id)sender;
and
- ()tabtwoAction:(id)sender;
with no return type
Method return types default to id, so all of
- (id)foo;
- ()foo;
- foo;
are equivalent.
One "difference" is.. if you have the method...
- staySexy { [thanks gorgeous]; }
you will get a compiler error...
error: returning void from a function with incompatible result type id
If the (id) is omitted (as has been discussed, it is actually just being implied)... the compiler will want (need? desire?) you to return something - aka an Object, or at least nil - not just void, or return!
- uglyMethod { return [loves it], nil; } // clang love you long time
Getting this error:
which seems to be connected to this, but have no idea why:
// Inline
inline float onsetsds_phase_rewrap(float phase);
inline float onsetsds_phase_rewrap(float phase){
return (phase>MINUSPI && phase<PI) ? phase : phase + TWOPI * (1.f + floorf((MINUSPI - phase) * INV_TWOPI));
}
Any ideas?
I'm trying to implement something like a Code Contracts feature for JavaScript as an assignment for one of my courses.
The problem I'm having is that I can't seem to find a way to output the source file directly to the console without modifying the entire grammar.
Does anybody knows a way to achieve this?
Thanks in advance.
Here's an example of what I'm trying to do:
function DoClear(num, arr, text){
Contract.Requires<RangeError>(num > 0);
Contract.Requires(num < 1000);
Contract.Requires<TypeError>(arr instanceOf Array);
Contract.Requires<RangeError>(arr.length > 0 && arr.length <= 9);
Contract.Requires<ReferenceError>(text != null);
Contract.Ensures<RangeError>(text.length === 0);
// method body
[...]
return text;
}
function DoClear(num, arr, text){
if (!(num > 0))
throw RangeError;
if (!(num < 1000))
throw Error;
if (!(arr instanceOf Array))
throw TypeError;
if (!(arr.length > 0 && arr.length <= 9))
throw RangeError;
if (!(text != null))
throw ReferenceError
// method body
[...]
if (!(text.length === 0))
throw RangeError
else
return text;
}
There are a few (minor) things you'll want to consider:
ignore string literals that might contain your special contract-syntax;
ignore multi- and single line comments that might contain your special Contract syntax;
ignore code like this: var Requires = "Contract.Requires<RangeError>"; (i.e. regular JavaScript code that "looks like" your contract-syntax);
It's pretty straight forward to take the points above into account and also simply create single tokens for an entire contract-line. You'll be making your life hard when tokenizing the following into 4 different tokens Contract.Requires<RangeError>(num > 0):
Contract
Requires
<RangeError>
(num > 0)
So it's easiest to create a single token from it, and at the parsing phase, split the token on ".", "<" or ">" with a maximum of 4 tokens (leaving expressions containing ".", "<" or ">" as they are).
A quick demo of what I described above might look like this:
grammar CCJS;
parse
: atom+ EOF
;
atom
: code_contract
| (Comment | String | Any) {System.out.print($text);}
;
code_contract
: Contract
{
String[] tokens = $text.split("[.<>]", 4);
System.out.print("if (!" + tokens[3] + ") throw " + tokens[2]);
}
;
Contract
#init{
boolean hasType = false;
}
#after{
if(!hasType) {
// inject a generic Error if this contract has no type
setText(getText().replaceFirst("\\(", "<Error>("));
}
}
: 'Contract.' ('Requires' | 'Ensures') ('<' ('a'..'z' | 'A'..'Z')+ '>' {hasType=true;})? '(' ~';'+
;
Comment
: '//' ~('\r' | '\n')*
| '/*' .* '*/'
;
String
: '"' (~('\\' | '"' | '\r' | '\n') | '\\' . )* '"'
;
Any
: .
;
which you can test with the following class:
import org.antlr.runtime.*;
public class Main {
public static void main(String[] args) throws Exception {
String src =
"/* \n" +
" Contract.Requires to be ignored \n" +
"*/ \n" +
"function DoClear(num, arr, text){ \n" +
" Contract.Requires<RangeError>(num > 0); \n" +
" Contract.Requires(num < 1000); \n" +
" Contract.Requires<TypeError>(arr instanceOf Array); \n" +
" Contract.Requires<RangeError>(arr.length > 0 && arr.length <= 9); \n" +
" Contract.Requires<ReferenceError>(text != null); \n" +
" Contract.Ensures<RangeError>(text.length === 0); \n" +
" \n" +
" // method body \n" +
" // and ignore single line comments, Contract.Ensures \n" +
" var s = \"Contract.Requires\"; // also ignore strings \n" +
" \n" +
" return text; \n" +
"} \n";
CCJSLexer lexer = new CCJSLexer(new ANTLRStringStream(src));
CCJSParser parser = new CCJSParser(new CommonTokenStream(lexer));
parser.parse();
}
}
If you run the Main class above, the following will be printed to the console:
/*
Contract.Requires to be ignored
*/
function DoClear(num, arr, text){
if (!(num > 0)) throw RangeError;
if (!(num < 1000)) throw Error;
if (!(arr instanceOf Array)) throw TypeError;
if (!(arr.length > 0 && arr.length <= 9)) throw RangeError;
if (!(text != null)) throw ReferenceError;
if (!(text.length === 0)) throw RangeError;
// method body
// and ignore single line comments, Contract.Ensures
var s = "Contract.Requires"; // also ignore strings
return text;
}
BUT ...
... I realize that it isn't what you're exactly looking for: the RangeError is not placed at the end of your function. And that's going to be tough one: a function might have multiple returns, and is likely to have multiple code blocks { ... } making it difficult to know where the } is that ends the function. So you don't know where exactly to inject this RangeError-check. At least, not with a naive approach as I demonstrated.
The only reliable way to implement such a thing is to get a decent JavaScript grammar, add your own contract-rules to it, rewrite the AST the parser produces, and finally emit the new AST in a friendly-formatted way: not a trivial task, to say the least!
There are various ECMA/JS grammars on the ANTLR Wiki, but tread with care: they are user-committed grammars and may contain errors (probably will in this case[1]!).
If you choose to place the RangeError there where it should be rewritte, like so:
function DoClear(num, arr, text){
Contract.Requires<RangeError>(num > 0);
...
// method body
...
Contract.Ensures<RangeError>(text.length === 0);
return text;
}
which would result in:
function DoClear(num, arr, text){
if (!(num > 0)) throw RangeError;
...
// method body
...
if (!(text.length === 0))
throw RangeError
return text;
}
then you need not parse the entire method body, and you might get away with a hack as I proposed.
Best of luck!
[1] the last time I checked these ECMA/JS script grammars, none of them handled regex literals, /pattern/, properly, making them in my opinion suspect.