这个Ruby参数约定来自哪里?

时间:2022-11-22 16:55:56

There's a piece of Ruby middleware used by Rails and other frameworks to the parse the parameters you've sent to the server into a nested Hash object.

Rails和其他框架使用了一块Ruby中间件来解析您发送到服务器的参数到嵌套的Hash对象中。

If you send these parameters to the server:

如果将这些参数发送到服务器:

person[id] = 1
person[name] = Joe Blow
person[email] = joe.blow@test.com
person[address][street_address] = 123 Somewhere St.
person[address][city] = Chicago
person[address][zip] = 12345
person[other_field][] = 1
person[other_field][] = 2
person[other_field][] = 3

They get parsed to this:

他们被解析为:

{
  :person => {
    :id => "1",
    :name => "Joe Blow",
    :email => "joe.blow@test.com",
    :address => {
      :street_address => "123 Somewhere St.",
      :city => "Chicago",
      :state => "IL",
      :zip => "12345"
    },
    :other_field => [ 1, 2, 3 ]
  }
}

I believe this is supported by PHP as well. Can anybody tell me what this convention is called, where it came from, and what other languages support it? (Perl, Python, etc.)

我相信PHP也支持这一点。任何人都可以告诉我这个约定叫什么,它来自哪里,以及其他语言支持它? (Perl,Python等)

3 个解决方案

#1


7  

Field research

I'm trying to find out if there's a name for this convention, but I can't find it yet.

我试图找出这个约定是否有名称,但我还没找到它。

Ruby

For what it's worth, the piece of middleware that does this in Ruby is Rack::Utils. See the source on Github.

对于它的价值,在Ruby中执行此操作的中间件是Rack :: Utils。请参阅Github上的源代码。

There is some more information on the subject in the Ruby on Rails Guides.

Ruby on Rails指南中有关于该主题的更多信息。

And here is an interesting ticket about the code being moved from Rails to the Rack middleware.

这是一个有趣的票据,关于代码从Rails移动到Rack中间件。

PHP

I've done some digging in the PHP source, and it seems that all the magic there happens in the main/php_variables.c source file. The SAPI code calls the php_std_post_handler method defined here. This eventually calls the php_register_variable_ex method, which is 185 lines of complex string-parsing in C (I must admit that C isn't my forte).

我已经在PHP源代码中进行了一些挖掘,似乎所有的魔法都发生在main / php_variables.c源文件中。 SAPI代码调用此处定义的php_std_post_handler方法。这最终调用了php_register_variable_ex方法,这是C行中185行复杂的字符串解析(我必须承认C不是我的强项)。

The only name I could find here was the php_std_post_handler, the PHP standard POST handler.

我在这里找到的唯一名字是php_std_post_handler,PHP标准的POST处理程序。

Python

In Python, this format isn't supported by default. See this question here on *.com on the subject.

在Python中,默认情况下不支持此格式。有关此主题,请参阅*.com上的此问题。

Perl

The CGI library in Perl doesn't support this format either. It does give easy access to single or multiple values, but not nested values as in your example. See the documentation on feching the value or values of a single named parameter and fetching the parameter list as a hash.

Perl中的CGI库也不支持这种格式。它确实可以轻松访问单个或多个值,但不能像示例中那样访问嵌套值。请参阅有关单个命名参数的值或值的文档,并将参数列表作为哈希值获取。

Java

Check out the heated debate on the subject of query parameter parsing in this question. Java doesn't parse this 'nested format' of POST parameters into a data structure by default.

查看关于此问题中查询参数解析主题的激烈争论。默认情况下,Java不会将POST参数的这种“嵌套格式”解析为数据结构。

Conclusion

I've looked into this, and haven't found a single name for this way of parameter parsing. Of the languages that I've looked into, only Ruby and PHP support this format natively.

我已经研究了这个,并没有找到这种参数解析方式的单一名称。在我研究过的语言中,只有Ruby和PHP本身支持这种格式。

#2


2  

It's not called anything, AFAIK, other than "sending parameters". If anything, it's called "parameter [type] conversion", where Rails just "converts" it into a hash.

除了“发送参数”之外,它不被称为任何东西,AFAIK。如果有的话,它被称为“参数[类型]转换”,其中Rails只是将其“转换”为哈希。

Other frameworks go further, using the parameter names as expressions used to create typed objects initialized with type-converted parameter values.

其他框架更进一步,使用参数名称作为表达式,用于创建使用类型转换参数值初始化的类型化对象。

All parameters are is a string value with a name. Any/all structure is imposed by the language/framework in use on the server side; what it gets transformed to is 100% dependent on that language/framework, and what that conversion consists of would determine what it would be (reasonable) called.

所有参数都是带有名称的字符串值。任何/所有结构都是由服务器端使用的语言/框架强加的;它转化为什么是100%依赖于该语言/框架,转换所包含的将决定它(合理)被调用的内容。

#3


0  

that would be a JSON object, which is quite standard and supported by most languages/libraries these days.

这将是一个JSON对象,这是非常标准的,现在大多数语言/库都支持它。

#1


7  

Field research

I'm trying to find out if there's a name for this convention, but I can't find it yet.

我试图找出这个约定是否有名称,但我还没找到它。

Ruby

For what it's worth, the piece of middleware that does this in Ruby is Rack::Utils. See the source on Github.

对于它的价值,在Ruby中执行此操作的中间件是Rack :: Utils。请参阅Github上的源代码。

There is some more information on the subject in the Ruby on Rails Guides.

Ruby on Rails指南中有关于该主题的更多信息。

And here is an interesting ticket about the code being moved from Rails to the Rack middleware.

这是一个有趣的票据,关于代码从Rails移动到Rack中间件。

PHP

I've done some digging in the PHP source, and it seems that all the magic there happens in the main/php_variables.c source file. The SAPI code calls the php_std_post_handler method defined here. This eventually calls the php_register_variable_ex method, which is 185 lines of complex string-parsing in C (I must admit that C isn't my forte).

我已经在PHP源代码中进行了一些挖掘,似乎所有的魔法都发生在main / php_variables.c源文件中。 SAPI代码调用此处定义的php_std_post_handler方法。这最终调用了php_register_variable_ex方法,这是C行中185行复杂的字符串解析(我必须承认C不是我的强项)。

The only name I could find here was the php_std_post_handler, the PHP standard POST handler.

我在这里找到的唯一名字是php_std_post_handler,PHP标准的POST处理程序。

Python

In Python, this format isn't supported by default. See this question here on *.com on the subject.

在Python中,默认情况下不支持此格式。有关此主题,请参阅*.com上的此问题。

Perl

The CGI library in Perl doesn't support this format either. It does give easy access to single or multiple values, but not nested values as in your example. See the documentation on feching the value or values of a single named parameter and fetching the parameter list as a hash.

Perl中的CGI库也不支持这种格式。它确实可以轻松访问单个或多个值,但不能像示例中那样访问嵌套值。请参阅有关单个命名参数的值或值的文档,并将参数列表作为哈希值获取。

Java

Check out the heated debate on the subject of query parameter parsing in this question. Java doesn't parse this 'nested format' of POST parameters into a data structure by default.

查看关于此问题中查询参数解析主题的激烈争论。默认情况下,Java不会将POST参数的这种“嵌套格式”解析为数据结构。

Conclusion

I've looked into this, and haven't found a single name for this way of parameter parsing. Of the languages that I've looked into, only Ruby and PHP support this format natively.

我已经研究了这个,并没有找到这种参数解析方式的单一名称。在我研究过的语言中,只有Ruby和PHP本身支持这种格式。

#2


2  

It's not called anything, AFAIK, other than "sending parameters". If anything, it's called "parameter [type] conversion", where Rails just "converts" it into a hash.

除了“发送参数”之外,它不被称为任何东西,AFAIK。如果有的话,它被称为“参数[类型]转换”,其中Rails只是将其“转换”为哈希。

Other frameworks go further, using the parameter names as expressions used to create typed objects initialized with type-converted parameter values.

其他框架更进一步,使用参数名称作为表达式,用于创建使用类型转换参数值初始化的类型化对象。

All parameters are is a string value with a name. Any/all structure is imposed by the language/framework in use on the server side; what it gets transformed to is 100% dependent on that language/framework, and what that conversion consists of would determine what it would be (reasonable) called.

所有参数都是带有名称的字符串值。任何/所有结构都是由服务器端使用的语言/框架强加的;它转化为什么是100%依赖于该语言/框架,转换所包含的将决定它(合理)被调用的内容。

#3


0  

that would be a JSON object, which is quite standard and supported by most languages/libraries these days.

这将是一个JSON对象,这是非常标准的,现在大多数语言/库都支持它。