Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Selector for JSON Payload #437

Open
wants to merge 6 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"doctrine/dbal": "^2.12 || ^3.5",
"dragonmantank/cron-expression": "^3.1",
"league/flysystem-sftp-v3": "^3.0",
"mtdowling/jmespath.php": "^2.8",
"nesbot/carbon": "^2.27",
"phpoffice/phpspreadsheet": "^1.24 || ^2.2",
"pimcore/compatibility-bridge-v10": "^1.0",
Expand Down
25 changes: 20 additions & 5 deletions src/DataSource/Interpreter/JsonFileInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

namespace Pimcore\Bundle\DataImporterBundle\DataSource\Interpreter;

use JmesPath\Env as JmesPath;
use Pimcore\Bundle\DataImporterBundle\PimcoreDataImporterBundle;
use Pimcore\Bundle\DataImporterBundle\Preview\Model\PreviewData;

class JsonFileInterpreter extends AbstractInterpreter
{
protected string $path;

/**
* @var array|null
*/
Expand All @@ -32,13 +35,17 @@ class JsonFileInterpreter extends AbstractInterpreter

protected function loadData(string $path): array
{
if ($this->cachedFilePath === $path && !empty($this->cachedContent)) {
if ($this->cachedFilePath !== $path || empty($this->cachedContent)) {
Copy link
Contributor Author

@cancan101 cancan101 Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe this logic was wrong before. See fix for Xml: #438

$content = file_get_contents($path);

return json_decode($this->prepareContent($content), true);
$data = json_decode($this->prepareContent($content), true);
} else {
return $this->cachedContent;
$data = $this->cachedContent;
}

if (!empty($this->path)) {
return $this->getValueFromPath($data);
}
return $data;
}

protected function doInterpretFileAndCallProcessRow(string $path): void
Expand All @@ -52,7 +59,7 @@ protected function doInterpretFileAndCallProcessRow(string $path): void

public function setSettings(array $settings): void
{
//nothing to do
$this->path = $settings['path'];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to do any validation here that the path passed in is a valid JmesPath?

}

/**
Expand Down Expand Up @@ -132,4 +139,12 @@ public function previewData(string $path, int $recordNumber = 0, array $mappedCo

return new PreviewData($columns, $previewData, $readRecordNumber, $mappedColumns);
}

/**
* Returns a value from the specified path in a nested array `$data`.
*/
private function getValueFromPath(array $data): mixed
{
return JmesPath::search($this->path, $data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pimcore.plugin.pimcoreDataImporterBundle.configuration.components.interpreter.js
},
border: false,
items: [
{
xtype: 'textfield',
fieldLabel: t('plugin_pimcore_datahub_data_importer_configpanel_json_path'),
name: this.dataNamePrefix + 'path',
value: this.data.path || '',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do I need anything else here, such as:

allowBlank: true,
msgTarget: 'under'

}
]
});
}
Expand Down
1 change: 1 addition & 0 deletions src/Resources/translations/admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ plugin_pimcore_datahub_data_importer_configpanel_csv_escape: Escape
plugin_pimcore_datahub_data_importer_configpanel_xlsx_sheet: Sheet
plugin_pimcore_datahub_data_importer_configpanel_xml_xpath: XPath
plugin_pimcore_datahub_data_importer_configpanel_xml_schema: Schema
plugin_pimcore_datahub_data_importer_configpanel_json_path: Path
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have seen the name "path" and "expression" used. do we have a preference?

plugin_pimcore_datahub_data_importer_configpanel_import_settings: Import Settings
plugin_pimcore_datahub_data_importer_configpanel_import_preview: Import Preview
plugin_pimcore_datahub_data_importer_configpanel_preview_data_clone: Copy preview data from data source for preview.
Expand Down
Loading