Yurik has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/326540 )

Change subject: Add support for "null" as a valid tab value
......................................................................

Add support for "null" as a valid tab value

Bug: T152753
Change-Id: Iea04f707e03b018c6a5567faca32c3439d1a78f6
---
M includes/JCTabularContent.php
M includes/JCTabularContentView.php
M includes/JCValidators.php
M modules/JsonConfig.css
M tests/phpunit/tabular-good/06.json
5 files changed, 72 insertions(+), 26 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/JsonConfig 
refs/changes/40/326540/1

diff --git a/includes/JCTabularContent.php b/includes/JCTabularContent.php
index 78cdc2e..ffee284 100644
--- a/includes/JCTabularContent.php
+++ b/includes/JCTabularContent.php
@@ -148,7 +148,9 @@
                } else {
                        $result->data = array_map( function ( $row ) use ( 
$localize, $isLocalized ) {
                                foreach ( $isLocalized as $ind ) {
-                                       $row[$ind] = $localize( $row[$ind] );
+                                       if ( $row[$ind] !== null ) {
+                                               $row[$ind] = $localize( 
$row[$ind] );
+                                       }
                                }
                                return $row;
                        }, $data->data );
diff --git a/includes/JCTabularContentView.php 
b/includes/JCTabularContentView.php
index 084e1b6..490a38f 100644
--- a/includes/JCTabularContentView.php
+++ b/includes/JCTabularContentView.php
@@ -93,6 +93,10 @@
                                        } else {
                                                if ( is_bool( $column ) ) {
                                                        $column = $column ? '☑' 
: '☐';
+                                               } elseif ( $column === null ) {
+                                                       // TODO: Should we 
append the CSS class instead?
+                                                       $columnAttrs['class'] = 
'mw-jsonconfig-value-null';
+                                                       $column = '';
                                                }
                                                // TODO: We should probably 
introduce one CSS class per type
                                                $vals[] = Html::element( 'td', 
$columnAttrs, $column );
diff --git a/includes/JCValidators.php b/includes/JCValidators.php
index 1fcb4f3..5a7c2b9 100644
--- a/includes/JCValidators.php
+++ b/includes/JCValidators.php
@@ -29,15 +29,17 @@
        }
 
        /** Returns a validator function to check if the value is a valid 
boolean (true/false)
+        * @param bool $nullable if true, null becomes a valid value
         * @return callable
         */
-       public static function isBool() {
-               return function ( JCValue $v, array $path ) {
-                       if ( !is_bool( $v->getValue() ) ) {
-                               $v->error( 'jsonconfig-err-bool', $path );
-                               return false;
+       public static function isBool( $nullable = false ) {
+               return function ( JCValue $v, array $path ) use ( $nullable ) {
+                       $value = $v->getValue();
+                       if ( is_bool( $value ) || ( $nullable && $value === 
null ) ) {
+                               return true;
                        }
-                       return true;
+                       $v->error( 'jsonconfig-err-bool', $path );
+                       return false;
                };
        }
 
@@ -55,17 +57,20 @@
        }
 
        /** Returns a validator function to check if the value is a valid 
single line string
+        * @param bool $nullable if true, null becomes a valid value
         * @param int $maxlength maximum allowed string size
         * @return callable
         */
-       public static function isStringLine( $maxlength = 400 ) {
-               return function ( JCValue $v, array $path ) use ( $maxlength ) {
-                       $str = $v->getValue();
-                       if ( !JCUtils::isValidLineString( $str, $maxlength ) ) {
-                               $v->error( 'jsonconfig-err-stringline', $path, 
$maxlength );
-                               return false;
+       public static function isStringLine( $nullable = false, $maxlength = 
400 ) {
+               return function ( JCValue $v, array $path ) use ( $nullable, 
$maxlength ) {
+                       $value = $v->getValue();
+                       if ( JCUtils::isValidLineString( $value, $maxlength ) ||
+                                ( $nullable && $value === null )
+                       ) {
+                               return true;
                        }
-                       return true;
+                       $v->error( 'jsonconfig-err-stringline', $path, 
$maxlength );
+                       return false;
                };
        }
 
@@ -83,15 +88,17 @@
        }
 
        /** Returns a validator function to check if the value is a valid 
integer
+        * @param bool $nullable if true, null becomes a valid value
         * @return callable
         */
-       public static function isNumber() {
-               return function ( JCValue $v, array $path ) {
-                       if ( !is_double( $v->getValue() ) && !is_int( 
$v->getValue() ) ) {
-                               $v->error( 'jsonconfig-err-number', $path );
-                               return false;
+       public static function isNumber( $nullable = false ) {
+               return function ( JCValue $v, array $path ) use ( $nullable ) {
+                       $value = $v->getValue();
+                       if ( is_double( $value ) || is_int( $value ) || ( 
$nullable && $value === null ) ) {
+                               return true;
                        }
-                       return true;
+                       $v->error( 'jsonconfig-err-number', $path );
+                       return false;
                };
        }
 
@@ -191,13 +198,17 @@
 
        /** Returns a validator function that will ensure that the given value 
is a non-empty object,
         * with each key being an allowed language code, and each value being a 
single line string.
+        * @param bool $nullable if true, null becomes a valid value
         * @param int $maxlength
         * @return Closure
         */
-       public static function isLocalizedString( $maxlength = 400 ) {
-               return function ( JCValue $jcv, array $path ) use ( $maxlength 
) {
+       public static function isLocalizedString( $nullable = false, $maxlength 
= 400 ) {
+               return function ( JCValue $jcv, array $path ) use ( $nullable, 
$maxlength ) {
                        if ( !$jcv->isMissing() ) {
                                $v = $jcv->getValue();
+                               if ( $nullable && $v === null ) {
+                                       return true;
+                               }
                                if ( is_object( $v ) ) {
                                        $v = (array)$v;
                                }
@@ -279,16 +290,16 @@
                        if ( is_string( $value ) ) {
                                switch ( $value ) {
                                        case 'string':
-                                               $validator = 
JCValidators::isStringLine();
+                                               $validator = 
JCValidators::isStringLine( true );
                                                break;
                                        case 'boolean':
-                                               $validator = 
JCValidators::isBool();
+                                               $validator = 
JCValidators::isBool( true );
                                                break;
                                        case 'number':
-                                               $validator = 
JCValidators::isNumber();
+                                               $validator = 
JCValidators::isNumber( true );
                                                break;
                                        case 'localized':
-                                               $validator = 
JCValidators::isLocalizedString();
+                                               $validator = 
JCValidators::isLocalizedString( true );
                                                break;
                                }
                        }
diff --git a/modules/JsonConfig.css b/modules/JsonConfig.css
index c3e4325..59b5815 100644
--- a/modules/JsonConfig.css
+++ b/modules/JsonConfig.css
@@ -29,6 +29,10 @@
        background-color: #dcfae3;
 }
 
+.mw-jsonconfig .mw-jsonconfig-value-null {
+       background-color: #eaecf0;
+}
+
 .mw-jsonconfig-value-info {
        float: right;
        font-weight: bold;
diff --git a/tests/phpunit/tabular-good/06.json 
b/tests/phpunit/tabular-good/06.json
index 03fe893..fc2d150 100644
--- a/tests/phpunit/tabular-good/06.json
+++ b/tests/phpunit/tabular-good/06.json
@@ -51,6 +51,12 @@
                 {
                     "fr": "fr-baz"
                 }
+            ],
+            [
+                null,
+                null,
+                null,
+                null
             ]
         ]
     },
@@ -95,6 +101,12 @@
                 0,
                 false,
                 "fr-baz"
+            ],
+            [
+                null,
+                null,
+                null,
+                null
             ]
         ]
     },
@@ -139,7 +151,14 @@
                 0,
                 false,
                 "fr-baz"
+            ],
+            [
+                null,
+                null,
+                null,
+                null
             ]
+
         ]
     },
     "fr": {
@@ -183,6 +202,12 @@
                 0,
                 false,
                 "fr-baz"
+            ],
+            [
+                null,
+                null,
+                null,
+                null
             ]
         ]
     }

-- 
To view, visit https://gerrit.wikimedia.org/r/326540
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iea04f707e03b018c6a5567faca32c3439d1a78f6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/JsonConfig
Gerrit-Branch: wmf/1.29.0-wmf.5
Gerrit-Owner: Yurik <yu...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to