意外收到 Google PR,Jsonnet-PHP v1.3.1 发布

栏目: 软件资讯 · 发布时间: 5年前

内容简介:由于意外地收到了Google一名工程师的PR(https://github.com/Neeke/Jsonnet-PHP/pull/6),Jsonnet-PHP发布了1.3.1版本。 此次PR内容为: Fixed capitalization: JsonNet -> Jsonnet Added language directive fo...

由于意外地收到了Google一名工程师的PR(https://github.com/Neeke/Jsonnet-PHP/pull/6),Jsonnet-PHP发布了1.3.1版本。

此次PR内容为:

  • Fixed capitalization: JsonNet -> Jsonnet

  • Added language directive for syntax highlighting code snippets

  • Removed extra indent whitespace around code samples for readability

  • Minor wording changes for readability

  • Added link to Jsonnet homepage for more info

可以看到 1.3.1 主要是将 JsonNet 修改为 Jsonnet,并对代码的格式进行了修改以提升可读性。

JsonNet-PHP 是 Google Jsonnet 对 PHP的支持扩展.

Change Log: 

1.3.1

  • Merged formatting cleanups for README from google.

  • Rename class from JsonNet to Jsonnet.

1.3.0

  • Update Lib JsonNet use v0.10.0.

  • Support PHP 7.

1.2.0

  • Fixed issue #4, pecl install failed.

  • Update Lib JsonNet use v0.9.5.

  • Add function JsonNet::fmtFile.

  • Add function JsonNet::fmtSnippet.

Google Jsonnet Tutorial

jsonnet语言,为我们最常使用的json对象赋予了新的生命力。使用jsonnet来描述json对象,可以在json对象中方便地使用变量\引用\循环等语法,甚至可以书写业务逻辑。

Install Jsonnet-PHP扩展

The pecl package is :  http://pecl.php.net/package/jsonnet

pecl install jsonnet

Input (Jsonnet)

{
    cocktails: {
        // Ingredient quantities are in fluid ounces.         "Tom Collins": {
            ingredients: [
                { kind: "Farmers Gin", qty: 1.5 },
                { kind: "Lemon", qty: 1 },
                { kind: "Simple Syrup", qty: 0.5 },
                { kind: "Soda", qty: 2 },
                { kind: "Angostura", qty: "dash" },
            ],
            garnish: "Maraschino Cherry",
            served: "Tall",
        },
        Manhattan: {
            ingredients: [
                { kind: "Rye", qty: 2.5 },
                { kind: "Sweet Red Vermouth", qty: 1 },
                { kind: "Angostura", qty: "dash" },
            ],
            garnish: "Maraschino Cherry",
            served: "Straight Up",
        },
    }
}

Output (JSON)

{
    "cocktails": {

        "Tom Collins": {
            "ingredients": [
                { "kind": "Farmers Gin", "qty": 1.5 },
                { "kind": "Lemon", "qty": 1 },
                { "kind": "Simple Syrup", "qty": 0.5 },
                { "kind": "Soda", "qty": 2 },
                { "kind": "Angostura", "qty": "dash" }
            ],
            "garnish": "Maraschino Cherry",
            "served": "Tall"         },
        "Manhattan": {
            "ingredients": [
                { "kind": "Rye", "qty": 2.5 },
                { "kind": "Sweet Red Vermouth", "qty": 1 },
                { "kind": "Angostura", "qty": "dash" }
            ],
            "garnish": "Maraschino Cherry",
            "served": "Straight Up"         }
    }
}

Demo of PHP

Jsonnet::evaluateFile('bar_menu.1.jsonnet');

$Snippet = '
{
    cocktails: {
        // Ingredient quantities are in fluid ounces.
        "Tom Collins": {
            ingredients: [
                { kind: "Farmers Gin", qty: 1.5 },
                { kind: "Lemon", qty: 1 },
                { kind: "Simple Syrup", qty: 0.5 },
                { kind: "Soda", qty: 2 },
                { kind: "Angostura", qty: "dash" },
            ],
            garnish: "Maraschino Cherry",
            served: "Tall",
        },
        Manhattan: {
            ingredients: [
                { kind: "Rye", qty: 2.5 },
                { kind: "Sweet Red Vermouth", qty: 1 },
                { kind: "Angostura", qty: "dash" },
            ],
            garnish: "Maraschino Cherry",
            served: "Straight Up",
        },
    }
}
';

var_dump(Jsonnet::evaluateSnippet($Snippet));

PHP Re Result

/usr/local/php/php-7.0.6-zts-debug/bin/php --re jsonnet

Extension [ <persistent> extension #40 Jsonnet version v1.3.1 ] {

  - Constants [2] {
    Constant [ string JSONNET_PHP_VERSION ] { v1.3.1 }
    Constant [ string JSONNET_PHP_AUTHOR ] { Chitao.Gao  [ neeke@php.net ] }
  }

  - Functions {
    Function [ <internal:Jsonnet> function jsonnet_get_version ] {
    }
    Function [ <internal:Jsonnet> function jsonnet_get_author ] {
    }
  }

  - Classes [1] {
    Class [ <internal:Jsonnet> class Jsonnet ] {

      - Constants [0] {
      }

      - Static properties [0] {
      }

      - Static methods [4] {
        Method [ <internal:Jsonnet> static public method evaluateFile ] {

          - Parameters [1] {
            Parameter #0 [ <required> $file_path ]
          }
        }

        Method [ <internal:Jsonnet> static public method evaluateSnippet ] {

          - Parameters [1] {
            Parameter #0 [ <required> $snippet_string ]
          }
        }

        Method [ <internal:Jsonnet> static public method fmtFile ] {

          - Parameters [1] {
            Parameter #0 [ <required> $file_path ]
          }
        }

        Method [ <internal:Jsonnet> static public method fmtSnippet ] {

          - Parameters [1] {
            Parameter #0 [ <required> $snippet_string ]
          }
        }
      }

      - Properties [0] {
      }

      - Methods [2] {
        Method [ <internal:Jsonnet, ctor> public method __construct ] {
        }

        Method [ <internal:Jsonnet, dtor> public method __destruct ] {
        }
      }
    }
  }
}

CodeTips

<?php
/**
 * @author neeke@php.net
 * Date: 18/3/29 下午7:51
 */

const JSONNET_PHP_VERSION = 'v1.3.1';
const JSONNET_PHP_AUTHOR  = 'neeke@php.net';

const CODE_SUCCESS = 1000;
const CODE_ERROR   = 900;

/**
 * @return string
 */
function jsonnet_get_version()
{
    return JSONNET_PHP_VERSION;
}

function jsonnet_get_author()
{
    return JSONNET_PHP_AUTHOR;
}

class Jsonnet
{
    public function __construct()
    {
        #Jsonnet init
    }

    public function __destruct()
    {
        #Jsonnet destroy
    }

    /**
     * @param $file_path
     *
     * @return array
     * @throws Exception
     */
    static public function evaluateFile($file_path)
    {
        throw new Exception('Jsonnet::evaluateFile #error', CODE_ERROR);

        return array();
    }

    /**
     * @param $snippet_string
     *
     * @return array
     * @throws Exception
     */
    static public function evaluateSnippet($snippet_string)
    {
        throw new Exception('Jsonnet::evaluateSnippet #error', CODE_ERROR);

        return array();
    }

    /**
     * @param $file_path
     *
     * @return array
     * @throws Exception
     */
    static public function fmtFile($file_path)
    {
        throw new Exception('Jsonnet::fmtFile #error', CODE_ERROR);

        return array();
    }

    /**
     * @param $snippet_string
     *
     * @return array
     * @throws Exception
     */
    static public function fmtSnippet($snippet_string)
    {
        throw new Exception('Jsonnet::fmtSnippet #error', CODE_ERROR);

        return array();
    }

}

【声明】文章转载自:开源中国社区 [http://www.oschina.net]


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Nginx Essentials

Nginx Essentials

Valery Kholodkov / Packt Publishing / 2015-7-1 / USD 34.99

About This Book Learn how to set up, configure, and operate an Nginx installation for day-to-day useExplore the vast features of Nginx to manage it like a pro, and use them successfully to run your......一起来看看 《Nginx Essentials》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具