Skip to content

Commit

Permalink
Merge pull request #7692 from m-kuhn/arrayRep
Browse files Browse the repository at this point in the history
Adjust representation of arrays and maps in expressions
  • Loading branch information
m-kuhn committed Aug 24, 2018
2 parents a5a6f00 + ec4483a commit 31fadd1
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 57 deletions.
2 changes: 1 addition & 1 deletion resources/function_help/json/array
Expand Up @@ -7,6 +7,6 @@
{"arg":"value1", "syntaxOnly": true},
{"arg":"value2", "syntaxOnly": true},
{"arg":"value", "descOnly": true, "description":"a value"}],
"examples": [ { "expression":"array(2,10)", "returns":"array: 2, 10"}
"examples": [ { "expression":"array(2,10)", "returns":"[ 2, 10 ]"}
]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/array_append
Expand Up @@ -4,5 +4,5 @@
"description": "Returns an array with the given value added at the end.",
"arguments": [ {"arg":"array","description":"an array"},
{"arg":"value","description":"the value to add"}],
"examples": [ { "expression":"array_append(array(1,2,3),4)", "returns":"array: 1,2,3,4"}]
"examples": [ { "expression":"array_append(array(1,2,3),4)", "returns":"[ 1, 2, 3, 4 ]"}]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/array_cat
Expand Up @@ -7,6 +7,6 @@
{"arg":"array1", "syntaxOnly": true},
{"arg":"array2", "syntaxOnly": true},
{"arg":"array", "descOnly": true, "description":"an array"}],
"examples": [ { "expression":"array_cat(array(1,2),array(2,3))", "returns":"array: 1,2,2,3"}
"examples": [ { "expression":"array_cat(array(1,2),array(2,3))", "returns":"[ 1, 2, 2, 3 ]"}
]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/array_distinct
Expand Up @@ -4,6 +4,6 @@
"description": "Returns an array containing distinct values of the given array.",
"arguments": [
{"arg":"array","description":"an array"}],
"examples": [ { "expression":"array_distinct(array(1,2,3,2,1))", "returns":"array: 1,2,3"}
"examples": [ { "expression":"array_distinct(array(1,2,3,2,1))", "returns":"[ 1, 2, 3 ]"}
]
}
4 changes: 2 additions & 2 deletions resources/function_help/json/array_foreach
Expand Up @@ -7,7 +7,7 @@
{"arg":"expression","description":"an expression to evaluate on each item. The variable `@element` will be replaced by the current value."}
],
"examples": [
{ "expression": "array_foreach(array('a','b','c'),upper(@element))", "returns":"array: 'A', 'B', 'C'"},
{ "expression": "array_foreach(array(1,2,3),@element + 10)", "returns":"array: 11, 12, 13"}
{ "expression": "array_foreach(array('a','b','c'),upper(@element))", "returns":"[ 'A', 'B', 'C' ]"},
{ "expression": "array_foreach(array(1,2,3),@element + 10)", "returns":"[ 11, 12, 13 ]"}
]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/array_insert
Expand Up @@ -5,5 +5,5 @@
"arguments": [ {"arg":"array","description":"an array"},
{"arg":"pos","description":"the position where to add (0 based)"},
{"arg":"value","description":"the value to add"}],
"examples": [ { "expression":"array_insert(array(1,2,3),1,100)", "returns":"array: 1,100,2,3"}]
"examples": [ { "expression":"array_insert(array(1,2,3),1,100)", "returns":"[ 1, 100, 2, 3 ]"}]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/array_prepend
Expand Up @@ -4,5 +4,5 @@
"description": "Returns an array with the given value added at the beginning.",
"arguments": [ {"arg":"array","description":"an array"},
{"arg":"value","description":"the value to add"}],
"examples": [ { "expression":"array_prepend(array(1,2,3),0)", "returns":"array: 0,1,2,3"}]
"examples": [ { "expression":"array_prepend(array(1,2,3),0)", "returns":"[ 0, 1, 2, 3 ]"}]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/array_remove_all
Expand Up @@ -4,5 +4,5 @@
"description": "Returns an array with all the entries of the given value removed.",
"arguments": [ {"arg":"array","description":"an array"},
{"arg":"value","description":"the values to remove"}],
"examples": [ { "expression":"array_remove_all(array('a','b','c','b'),'b')", "returns":"array: 'a','c'"}]
"examples": [ { "expression":"array_remove_all(array('a','b','c','b'),'b')", "returns":"[ 'a', 'c' ]"}]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/array_remove_at
Expand Up @@ -4,5 +4,5 @@
"description": "Returns an array with the given index removed.",
"arguments": [ {"arg":"array","description":"an array"},
{"arg":"pos","description":"the position to remove (0 based)"}],
"examples": [ { "expression":"array_remove_at(array(1,2,3),1)", "returns":"array: 1,3"}]
"examples": [ { "expression":"array_remove_at(array(1,2,3),1)", "returns":"[ 1, 3 ]"}]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/array_reverse
Expand Up @@ -3,5 +3,5 @@
"type": "function",
"description": "Returns the given array with array values in reversed order.",
"arguments": [ {"arg":"array","description":"an array"} ],
"examples": [ { "expression":"array_reverse(array(2,4,0,10))", "returns":"array: 10,0,4,2"}]
"examples": [ { "expression":"array_reverse(array(2,4,0,10))", "returns":"[ 10, 0, 4, 2 ]"}]
}
16 changes: 8 additions & 8 deletions resources/function_help/json/array_slice
Expand Up @@ -17,35 +17,35 @@
],
"examples": [{
"expression": "array_slice(array(1,2,3,4,5),0,3)",
"returns": "array: 1,2,3,4"
"returns": "[ 1, 2, 3, 4 ]"
},
{
"expression": "array_slice(array(1,2,3,4,5),0,-1)",
"returns": "array: 1,2,3,4,5"
"returns": "[ 1, 2, 3, 4, 5 ]"
},
{
"expression": "array_slice(array(1,2,3,4,5),-5,-1)",
"returns": "array: 1,2,3,4,5"
"returns": "[ 1, 2, 3, 4, 5 ]"
},
{
"expression": "array_slice(array(1,2,3,4,5),0,0)",
"returns": "array: 1"
"returns": "[ 1 ]"
},
{
"expression": "array_slice(array(1,2,3,4,5),-2,-1)",
"returns": "array: 4,5"
"returns": "[ 4, 5 ]"
},
{
"expression": "array_slice(array(1,2,3,4,5),-1,-1)",
"returns": "array: 5"
"returns": "[ 5 ]"
},
{
"expression": "array_slice(array('Dufour','Valmiera','Chugiak','Brighton'),1,2)",
"returns": "array: 'Valmiera','Chugiak'"
"returns": "[ 'Valmiera', 'Chugiak' ]"
},
{
"expression": "array_slice(array_slice(array('Dufour','Valmiera','Chugiak','Brighton'),-2,-1)",
"returns": "array: 'Chugiak','Brighton'"
"returns": "[ 'Chugiak', 'Brighton' ]"
}
]
}
4 changes: 2 additions & 2 deletions resources/function_help/json/generate_series
Expand Up @@ -7,7 +7,7 @@
{"arg":"stop", "description":"value that ends the sequence once reached"},
{"arg":"step","optional":true,"default":"1","description":"value used as the increment between values"}
],
"examples": [ { "expression":"generate_series('1,5)", "returns":"array: '1', '2', '3', '4', '5'"},
{ "expression":"generate_series('5,1,-1)", "returns":"array: '5', '4', '3', '2', '1'"}
"examples": [ { "expression":"generate_series('1,5)", "returns":"[ 1, 2, 3, 4, 5 ]"},
{ "expression":"generate_series('5,1,-1)", "returns":"[ 5, 4, 3, 2, 1 ]"}
]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/map
Expand Up @@ -10,6 +10,6 @@
{"arg":"value2", "syntaxOnly": true},
{"arg":"key", "descOnly": true, "description":"a key (string)"},
{"arg":"value", "descOnly": true, "description":"a value"}],
"examples": [ { "expression":"map('1','one','2', 'two')", "returns":"map: 1: 'one', 2: 'two'"}
"examples": [ { "expression":"map('1','one','2', 'two')", "returns":"{ '1': 'one', '2': 'two' }"}
]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/map_akeys
Expand Up @@ -3,5 +3,5 @@
"type": "function",
"description": "Returns all the keys of a map as an array.",
"arguments": [ {"arg":"map","description":"a map"}],
"examples": [ { "expression":"map_akeys(map('1','one','2','two'))", "returns":"array: '1', '2'"}]
"examples": [ { "expression":"map_akeys(map('1','one','2','two'))", "returns":"[ '1', '2' ]"}]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/map_avals
Expand Up @@ -3,5 +3,5 @@
"type": "function",
"description": "Returns all the values of a map as an array.",
"arguments": [ {"arg":"map","description":"a map"}],
"examples": [ { "expression":"map_avals(map('1','one','2','two'))", "returns":"array: 'one', 'two'"}]
"examples": [ { "expression":"map_avals(map('1','one','2','two'))", "returns":"[ 'one', 'two' ]"}]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/map_concat
Expand Up @@ -7,6 +7,6 @@
{"arg":"map1", "syntaxOnly": true},
{"arg":"map2", "syntaxOnly": true},
{"arg":"map", "descOnly": true, "description":"a map"}],
"examples": [ { "expression":"map_concat(map('1','one', '2','overridden'),map('2','two', '3','three'))", "returns":"map: 1: 'one, 2: 'two', 3: 'three'"}
"examples": [ { "expression":"map_concat(map('1','one', '2','overridden'),map('2','two', '3','three'))", "returns":"{ '1': 'one, '2': 'two', '3': 'three' }"}
]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/map_delete
Expand Up @@ -4,5 +4,5 @@
"description": "Returns a map with the given key and its corresponding value deleted.",
"arguments": [ {"arg":"map","description":"a map"},
{"arg":"key","description":"the key to delete"}],
"examples": [ { "expression":"map_delete(map('1','one','2','two'),'2')", "returns":"map: 1: 'one'"}]
"examples": [ { "expression":"map_delete(map('1','one','2','two'),'2')", "returns":"{ '1': 'one' }"}]
}
2 changes: 1 addition & 1 deletion resources/function_help/json/map_insert
Expand Up @@ -5,5 +5,5 @@
"arguments": [ {"arg":"map","description":"a map"},
{"arg":"key","description":"the key to add"},
{"arg":"value","description":"the value to add"}],
"examples": [ { "expression":"map_insert(map('1','one'),'3','three')", "returns":"map: 1: 'one', 3: 'three'"}]
"examples": [ { "expression":"map_insert(map('1','one'),'3','three')", "returns":"{ '1': 'one', '3': 'three' }"}]
}
4 changes: 2 additions & 2 deletions resources/function_help/json/regexp_matches
Expand Up @@ -6,7 +6,7 @@
{"arg":"string", "description":"the string to capture groups from against the regular expression"},
{"arg":"regex","description":"the regular expression used to capture groups"},
{"arg":"empty_value","optional":true,"default":"''","description":"the optional string to use as replacement for empty (zero length) matches"}],
"examples": [ { "expression":"regexp_matches('QGIS=>rocks','(.*)=>(.*)')", "returns":"array: 'QGIS', 'rocks'"},
{ "expression":"regexp_matches('key=>','(.*)=>(.*)','empty value')", "returns":"array: 'key', 'empty value'"}
"examples": [ { "expression":"regexp_matches('QGIS=>rocks','(.*)=>(.*)')", "returns":"[ 'QGIS', 'rocks' ]"},
{ "expression":"regexp_matches('key=>','(.*)=>(.*)','empty value')", "returns":"[ 'key', 'empty value' ]"}
]
}
4 changes: 2 additions & 2 deletions resources/function_help/json/string_to_array
Expand Up @@ -6,7 +6,7 @@
{"arg":"string", "description":"the input string"},
{"arg":"delimiter","optional":true,"default":"','","description":"the string delimiter used to split the input string"},
{"arg":"empty_value","optional":true,"default":"''","description":"the optional string to use as replacement for empty (zero length) matches"}],
"examples": [ { "expression":"string_to_array('1,2,3',',')", "returns":"array: '1', '2', '3'"},
{ "expression":"string_to_array('1,,3',',','0')", "returns":"array: '1', '0', '3'"}
"examples": [ { "expression":"string_to_array('1,2,3',',')", "returns":"[ '1', '2', '3' ]"},
{ "expression":"string_to_array('1,,3',',','0')", "returns":"[ '1', '0', '3' ]"}
]
}
41 changes: 28 additions & 13 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -903,35 +903,50 @@ QString QgsExpression::formatPreviewString( const QVariant &value )
}
else if ( value.type() == QVariant::Map )
{
QString mapStr;
QString mapStr = QStringLiteral( "{" );
const QVariantMap map = value.toMap();
QString separator;
for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
{
if ( !mapStr.isEmpty() ) mapStr.append( ", " );
mapStr.append( it.key() ).append( ": " ).append( formatPreviewString( it.value() ) );
if ( mapStr.length() > MAX_PREVIEW + 3 )
mapStr.append( separator );
if ( separator.isEmpty() )
separator = QStringLiteral( "," );

mapStr.append( QStringLiteral( " '%1': %2" ).arg( it.key(), formatPreviewString( it.value() ) ) );
if ( mapStr.length() > MAX_PREVIEW - 3 )
{
mapStr = QString( tr( "%1…" ) ).arg( mapStr.left( MAX_PREVIEW ) );
mapStr = tr( "%1…" ).arg( mapStr.left( MAX_PREVIEW - 2 ) );
break;
}
}
return tr( "<i>&lt;map: %1&gt;</i>" ).arg( mapStr );
if ( !map.empty() )
mapStr += QStringLiteral( " " );
mapStr += QStringLiteral( "}" );
return mapStr;
}
else if ( value.type() == QVariant::List || value.type() == QVariant::StringList )
{
QString listStr;
QString listStr = QStringLiteral( "[" );
const QVariantList list = value.toList();
for ( QVariantList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it )
QString separator;
for ( const QVariant &arrayValue : list )
{
if ( !listStr.isEmpty() ) listStr.append( ", " );
listStr.append( formatPreviewString( *it ) );
if ( listStr.length() > MAX_PREVIEW + 3 )
listStr.append( separator );
if ( separator.isEmpty() )
separator = QStringLiteral( "," );

listStr.append( " " );
listStr.append( formatPreviewString( arrayValue ) );
if ( listStr.length() > MAX_PREVIEW - 3 )
{
listStr = QString( tr( "%1…" ) ).arg( listStr.left( MAX_PREVIEW ) );
listStr = QString( tr( "%1…" ) ).arg( listStr.left( MAX_PREVIEW - 2 ) );
break;
}
}
return tr( "<i>&lt;array: %1&gt;</i>" ).arg( listStr );
if ( !list.empty() )
listStr += QStringLiteral( " " );
listStr += QStringLiteral( "]" );
return listStr;
}
else
{
Expand Down
14 changes: 7 additions & 7 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -279,7 +279,7 @@ void QgsExpressionBuilderWidget::btnNewFile_pressed()
bool ok;
QString text = QInputDialog::getText( this, tr( "Enter new file name" ),
tr( "File name:" ), QLineEdit::Normal,
QLatin1String( "" ), &ok );
QString(), &ok );
if ( ok && !text.isEmpty() )
{
newFunctionFile( text );
Expand Down Expand Up @@ -348,7 +348,7 @@ void QgsExpressionBuilderWidget::loadFieldNames( const QgsFields &fields )
{
QString fieldName = fields.at( i ).name();
fieldNames << fieldName;
registerItem( QStringLiteral( "Fields and Values" ), fieldName, " \"" + fieldName + "\" ", QLatin1String( "" ), QgsExpressionItem::Field, false, i );
registerItem( QStringLiteral( "Fields and Values" ), fieldName, " \"" + fieldName + "\" ", QString(), QgsExpressionItem::Field, false, i );
}
// highlighter->addFields( fieldNames );
}
Expand Down Expand Up @@ -426,7 +426,7 @@ void QgsExpressionBuilderWidget::registerItem( const QString &group,
else
{
// If the group doesn't exist yet we make it first.
QgsExpressionItem *newgroupNode = new QgsExpressionItem( QgsExpression::group( group ), QLatin1String( "" ), QgsExpressionItem::Header );
QgsExpressionItem *newgroupNode = new QgsExpressionItem( QgsExpression::group( group ), QString(), QgsExpressionItem::Header );
newgroupNode->setData( group, Qt::UserRole );
//Recent group should always be last group
newgroupNode->setData( group.startsWith( QLatin1String( "Recent (" ) ) ? 2 : 1, QgsExpressionItem::CUSTOM_SORT_ROLE );
Expand Down Expand Up @@ -628,9 +628,9 @@ void QgsExpressionBuilderWidget::txtExpressionString_textChanged()
if ( text.isEmpty() )
{
lblPreview->clear();
lblPreview->setStyleSheet( QLatin1String( "" ) );
txtExpressionString->setToolTip( QLatin1String( "" ) );
lblPreview->setToolTip( QLatin1String( "" ) );
lblPreview->setStyleSheet( QString() );
txtExpressionString->setToolTip( QString() );
lblPreview->setToolTip( QString() );
emit expressionParsed( false );
setParserError( true );
setEvalError( true );
Expand Down Expand Up @@ -1064,7 +1064,7 @@ QString QgsExpressionBuilderWidget::helpStylesheet() const
QString QgsExpressionBuilderWidget::loadFunctionHelp( QgsExpressionItem *expressionItem )
{
if ( !expressionItem )
return QLatin1String( "" );
return QString();

QString helpContents = expressionItem->getHelpText();

Expand Down
12 changes: 6 additions & 6 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -3114,24 +3114,24 @@ class TestQgsExpression: public QObject

void test_formatPreviewString()
{
QCOMPARE( QgsExpression::formatPreviewString( QVariant( "hello" ) ), QString( "'hello'" ) );
QCOMPARE( QgsExpression::formatPreviewString( QVariant( QVariantMap() ) ), QString( "<i>&lt;map: &gt;</i>" ) );
QCOMPARE( QgsExpression::formatPreviewString( QVariant( "hello" ) ), QStringLiteral( "'hello'" ) );
QCOMPARE( QgsExpression::formatPreviewString( QVariant( QVariantMap() ) ), QStringLiteral( "{}" ) );

QVariantMap map;
map[QStringLiteral( "1" )] = "One";
map[QStringLiteral( "2" )] = "Two";
QCOMPARE( QgsExpression::formatPreviewString( QVariant( map ) ), QString( "<i>&lt;map: 1: 'One', 2: 'Two'&gt;</i>" ) );
QCOMPARE( QgsExpression::formatPreviewString( QVariant( map ) ), QStringLiteral( "{ '1': 'One', '2': 'Two' }" ) );
map[QStringLiteral( "3" )] = "A very long string that is going to be truncated";
QCOMPARE( QgsExpression::formatPreviewString( QVariant( map ) ), QString( "<i>&lt;map: 1: 'One', 2: 'Two', 3: 'A very long string that is going to …&gt;</i>" ) );
QCOMPARE( QgsExpression::formatPreviewString( QVariant( map ) ), QStringLiteral( "{ '1': 'One', '2': 'Two', '3': 'A very long string that is… }" ) );

QVariantList list;
list << 1 << 2 << 3;
QCOMPARE( QgsExpression::formatPreviewString( QVariant( list ) ), QString( "<i>&lt;array: 1, 2, 3&gt;</i>" ) );
QCOMPARE( QgsExpression::formatPreviewString( QVariant( list ) ), QStringLiteral( "[ 1, 2, 3 ]" ) );

QStringList stringList;
stringList << QStringLiteral( "One" ) << QStringLiteral( "Two" ) << QStringLiteral( "A very long string that is going to be truncated" );
QCOMPARE( QgsExpression::formatPreviewString( QVariant( stringList ) ),
QString( "<i>&lt;array: 'One', 'Two', 'A very long string that is going to be trunca…&gt;</i>" ) );
QStringLiteral( "[ 'One', 'Two', 'A very long string that is going to be tr… ]" ) );
}
};

Expand Down

0 comments on commit 31fadd1

Please sign in to comment.