You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by bh...@apache.org on 2020/07/22 00:50:35 UTC

[beam] branch master updated: [BEAM-10274] Fix translation of json pipeline options. (#12333)

This is an automated email from the ASF dual-hosted git repository.

bhulette pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ab6c5c  [BEAM-10274] Fix translation of json pipeline options. (#12333)
0ab6c5c is described below

commit 0ab6c5c1ec85b75c9891245da12f7aaaf2647700
Author: Robert Bradshaw <ro...@google.com>
AuthorDate: Tue Jul 21 17:50:10 2020 -0700

    [BEAM-10274] Fix translation of json pipeline options. (#12333)
---
 .../python/apache_beam/options/pipeline_options.py |  2 ++
 .../apache_beam/options/pipeline_options_test.py   | 24 ++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/sdks/python/apache_beam/options/pipeline_options.py b/sdks/python/apache_beam/options/pipeline_options.py
index 32629be..e930207 100644
--- a/sdks/python/apache_beam/options/pipeline_options.py
+++ b/sdks/python/apache_beam/options/pipeline_options.py
@@ -245,6 +245,8 @@ class PipelineOptions(HasDisplayData):
       elif isinstance(v, list):
         for i in v:
           flags.append('--%s=%s' % (k, i))
+      elif isinstance(v, dict):
+        flags.append('--%s=%s' % (k, json.dumps(v)))
       else:
         flags.append('--%s=%s' % (k, v))
 
diff --git a/sdks/python/apache_beam/options/pipeline_options_test.py b/sdks/python/apache_beam/options/pipeline_options_test.py
index ee13cd6..25c42c0 100644
--- a/sdks/python/apache_beam/options/pipeline_options_test.py
+++ b/sdks/python/apache_beam/options/pipeline_options_test.py
@@ -21,6 +21,7 @@
 
 from __future__ import absolute_import
 
+import json
 import logging
 import unittest
 
@@ -178,6 +179,22 @@ class PipelineOptionsTest(unittest.TestCase):
               DisplayDataItemMatcher('mock_multi_option', ['op1', 'op2'])
           ]
       },
+      {
+          'flags': ['--mock_json_option={"11a": 0, "37a": 1}'],
+          'expected': {
+              'mock_flag': False,
+              'mock_option': None,
+              'mock_multi_option': None,
+              'mock_json_option': {
+                  '11a': 0, '37a': 1
+              },
+          },
+          'display_data': [
+              DisplayDataItemMatcher('mock_json_option', {
+                  '11a': 0, '37a': 1
+              })
+          ]
+      },
   ]
 
   # Used for testing newly added flags.
@@ -189,6 +206,7 @@ class PipelineOptionsTest(unittest.TestCase):
       parser.add_argument(
           '--mock_multi_option', action='append', help='mock multi option')
       parser.add_argument('--option with space', help='mock option with space')
+      parser.add_argument('--mock_json_option', type=json.loads, default={})
 
   # Use with MockOptions in test cases where multiple option classes are needed.
   class FakeOptions(PipelineOptions):
@@ -284,6 +302,12 @@ class PipelineOptionsTest(unittest.TestCase):
       self.assertEqual(
           options.view_as(PipelineOptionsTest.MockOptions).mock_option,
           case['expected']['mock_option'])
+      self.assertEqual(
+          options.view_as(PipelineOptionsTest.MockOptions).mock_multi_option,
+          case['expected']['mock_multi_option'])
+      self.assertEqual(
+          options.view_as(PipelineOptionsTest.MockOptions).mock_json_option,
+          case['expected'].get('mock_json_option', {}))
 
   def test_option_with_space(self):
     options = PipelineOptions(flags=['--option with space= value with space'])