Skip to content
Permalink
Browse files

[memory] Always use QVariant::StringList for list fields with a strin…

…g subtype

This matches the established behavior from the postgres provider
  • Loading branch information
nyalldawson committed Mar 25, 2021
1 parent 2f8adf7 commit 785ad7cbd30d619216aac22bae82a9f5345136ca
@@ -109,7 +109,7 @@ QgsMemoryProvider::QgsMemoryProvider( const QString &uri, const ProviderOptions
<< QgsVectorDataProvider::NativeType( tr( "Binary object (BLOB)" ), QStringLiteral( "binary" ), QVariant::ByteArray )

// list types
<< QgsVectorDataProvider::NativeType( tr( "String list" ), QStringLiteral( "stringlist" ), QVariant::List, 0, 0, 0, 0, QVariant::String )
<< QgsVectorDataProvider::NativeType( tr( "String list" ), QStringLiteral( "stringlist" ), QVariant::StringList, 0, 0, 0, 0, QVariant::String )
<< QgsVectorDataProvider::NativeType( tr( "Integer list" ), QStringLiteral( "integerlist" ), QVariant::List, 0, 0, 0, 0, QVariant::Int )
<< QgsVectorDataProvider::NativeType( tr( "Decimal (real) list" ), QStringLiteral( "doublelist" ), QVariant::List, 0, 0, 0, 0, QVariant::Double )
<< QgsVectorDataProvider::NativeType( tr( "Integer (64bit) list" ), QStringLiteral( "doublelist" ), QVariant::List, 0, 0, 0, 0, QVariant::Double )
@@ -202,7 +202,7 @@ QgsMemoryProvider::QgsMemoryProvider( const QString &uri, const ProviderOptions
{
//array
subType = type;
type = QVariant::List;
type = type == QVariant::String ? QVariant::StringList : QVariant::List;
typeName += QStringLiteral( "list" );
}
}
@@ -75,7 +75,7 @@ QgsVectorLayer *QgsMemoryProviderUtils::createMemoryLayer( const QString &name,
{
const QString lengthPrecision = QStringLiteral( "(%1,%2)" ).arg( field.length() ).arg( field.precision() );
parts << QStringLiteral( "field=%1:%2%3%4" ).arg( QString( QUrl::toPercentEncoding( field.name() ) ),
memoryLayerFieldType( field.type() == QVariant::List ? field.subType() : field.type() ),
memoryLayerFieldType( field.type() == QVariant::List || field.type() == QVariant::StringList ? field.subType() : field.type() ),
lengthPrecision,
field.type() == QVariant::List || field.type() == QVariant::StringList ? QStringLiteral( "[]" ) : QString() );
}
@@ -355,7 +355,7 @@ def testListFromUri(self):
'test',
'memory')

self.assertEqual(myMemoryLayer.fields().field('a').type(), QVariant.List)
self.assertEqual(myMemoryLayer.fields().field('a').type(), QVariant.StringList)
self.assertEqual(myMemoryLayer.fields().field('a').subType(), QVariant.String)

myMemoryLayer = QgsVectorLayer(
@@ -408,7 +408,8 @@ def testSaveFields(self):
QgsField('TestTime', QVariant.Time, 'time'),
QgsField('TestDateTime', QVariant.DateTime, 'datetime'),
QgsField("vallist", QVariant.List, subType=QVariant.Int),
QgsField("stringlist", QVariant.List, subType=QVariant.String),
QgsField("stringlist", QVariant.StringList, subType=QVariant.String),
QgsField("stringlist2", QVariant.List, subType=QVariant.String),
QgsField("reallist", QVariant.List, subType=QVariant.Double),
QgsField("longlist", QVariant.List, subType=QVariant.LongLong)]
self.assertTrue(myMemoryLayer.startEditing())
@@ -434,7 +435,12 @@ def testSaveFields(self):
importedFields = myImportedLayer.fields()
for f in myFields:
self.assertEqual(f.name(), importedFields.field(f.name()).name())
self.assertEqual(f.type(), importedFields.field(f.name()).type())
if f.name() != 'stringlist2':
self.assertEqual(f.type(), importedFields.field(f.name()).type())
else:
# we automatically convert List with String subtype to StringList, to match other data providers
self.assertEqual(importedFields.field(f.name()).type(), QVariant.StringList)

self.assertEqual(f.subType(), importedFields.field(f.name()).subType())
self.assertEqual(f.precision(), importedFields.field(f.name()).precision())
self.assertEqual(f.length(), importedFields.field(f.name()).length())
@@ -545,7 +551,8 @@ def testCreateMemoryLayer(self):
fields.append(QgsField("binaryfield", QVariant.ByteArray))
fields.append(QgsField("boolfield", QVariant.Bool))
fields.append(QgsField("vallist", QVariant.List, subType=QVariant.Int))
fields.append(QgsField("stringlist", QVariant.List, subType=QVariant.String))
fields.append(QgsField("stringlist", QVariant.StringList, subType=QVariant.String))
fields.append(QgsField("stringlist2", QVariant.List, subType=QVariant.String))
fields.append(QgsField("reallist", QVariant.List, subType=QVariant.Double))
fields.append(QgsField("longlist", QVariant.List, subType=QVariant.LongLong))

@@ -555,7 +562,11 @@ def testCreateMemoryLayer(self):
self.assertEqual(len(layer.fields()), len(fields))
for i in range(len(fields)):
self.assertEqual(layer.fields()[i].name(), fields[i].name())
self.assertEqual(layer.fields()[i].type(), fields[i].type())
if layer.fields()[i].name() != 'stringlist2':
self.assertEqual(layer.fields()[i].type(), fields[i].type())
else:
# we automatically convert List with String subtype to StringList, to match other data providers
self.assertEqual(layer.fields()[i].type(), QVariant.StringList)
self.assertEqual(layer.fields()[i].length(), fields[i].length())
self.assertEqual(layer.fields()[i].precision(), fields[i].precision(), fields[i].name())

0 comments on commit 785ad7c

Please sign in to comment.