Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the specifications of OpenResty coding

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what are the OpenResty coding specifications, the content is very detailed, interested friends can refer to, hope to be helpful to you.

This coding specification is written and maintained by APISIX.

Many development languages have their own coding specifications to tell developers the conventions in this field, to make the code look the same, and to avoid some common pitfalls, which are very friendly to beginners and make beginners get started quickly. Python's PEP 80, for example, is a model, and almost all Python developers have read the coding specification written by the Python author.

It is very important for developers to unify their thinking and write code according to the specification. OpenResty does not have its own coding specification, and some developers will be repeatedly review and require changes in the code style after submitting PR, which consumes a lot of time and effort that could have been avoided.

In fact, there are two tools in OpenResty that can help you automate code style detection: luacheck and lj-releng. The former is a universal testing tool for Lua and OpenResty, while the latter is a code testing tool written by OpenResty itself in perl. For myself, I install the luacheck plug-in in the vs code editor so that I have tools to prompt automatically when I write code, while in the project's CI, I run both tools, such as:

Luacheck-Q lua./utils/lj-releng lua/*.lua lua/apisix/*.lua copy code

The detection of one more tool is always not a bad thing.

But these two tools are more about detecting the most basic code styles, such as global variables, line length, etc., which are far from the level of detail of Python pep80, and there is no documentation for your reference.

Today, based on my experience in OpenResty-related open source projects, I will summarize the OpenResty coding style document, this specification is also consistent with some common API gateways: Kong, APISIX code style.

Indent

Use four spaces as indentation marks in OpenResty, although Lua does not require such syntax.

-- Noif a thenngx.say ("hello") end copy code-- yesif a thenngx.say ("hello") end copy code

You can change the tab to 4 spaces in the editor you use to simplify the operation.

Space

On both sides of the operator, you need a space to separate it:

-- Nolocal i=1local s = "apisix" copy code-- Yeslocal i=1local s = "apisix" copy code blank lines

Many developers will bring development habits from other languages to OpenResty, such as adding a semicolon at the end of the line.

-- Noif a then ngx.say ("hello"); end; copy code

Adding a semicolon makes the Lua code look ugly and unnecessary. In addition, do not save the number of lines of code, which turns multiple lines of code into one line in order to appear "concise." In this way, you will not know which piece of code has gone wrong when you locate the error:

-- Noif a then ngx.say ("hello") end copy code-- yesif a then ngx.say ("hello") end copy code

Functions need to be separated by two blank lines:

-- Nolocal function foo () end local function bar () end copy code-- Yeslocal function foo () end local function bar () end copy code

If there are multiple branches of if elseif, they need to be separated by a blank line:

-- Noif aversion = 1 then foo () elseif aversion = 2 then bar () elseif aversion = 3 then run () else error () end copy code-- Yesif axiom = 1 then foo () elseif aversion = 2 then bar () elseif aversion = 3 then run () else error () end copy code maximum length per line

Each line cannot exceed 80 characters. If it exceeds, it needs to be wrapped and aligned:

-- No return limit_conn_new ("plugin-limit-conn", conf.conn, conf.burst, conf.default_conn_delay) copy code-- Yesreturn limit_conn_new ("plugin-limit-conn", conf.conn, conf.burst, conf.default_conn_delay) copy code

When the line breaks are aligned, the correspondence between the upper and lower lines should be reflected. In the case of the above example, the argument to the second line of the function is to the right of the left parenthesis of the first line.

If it is string concatenation alignment, you need to put.. Put it on the next line:

-- No return limit_conn_new ("plugin-limit-conn".. "plugin-limit-conn".. "plugin-limit-conn") copy the code-- Yesreturn limit_conn_new ("plugin-limit-conn".. "plugin-limit-conn".. "plugin-limit-conn") copy code variables

You should always use local variables, not global variables:

-- Noi = 1s = "apisix" copy code-- Yeslocal I = 1local s = "apisix" copy code

Variable naming uses the snake_case style:

-- Nolocal IndexArr = 1local str_Name = "apisix" copy code-- Yeslocal index_arr = 1local str_name = "apisix" copy code

Use all uppercase for constants:

-- Nolocal max_int = 65535local server_name = "apisix" copy code-- Yeslocal MAX_INT = 65535local SERVER_NAME = "apisix" copy code array

Use table.new to pre-allocate arrays:

-- Nolocal t = {} for I = 1,100 do t [I] = I end copy code-- Yes local new_tab = require "table.new" local t = new_tab (100,0) for I = 1,100 do t [I] = I end copy code

Do not use nil in an array:

-- Nolocal t = {1,2, nil, 3} copy the code

If you must use a null value, use ngx.null to indicate:

-- Nolocal t = {1,2, ngx.null, 3} copy the code string

Do not concatenate strings on hot code paths:

-- Nolocal s = "" for I = 1, 100000 do s = s.. "a" end copy code-- Yeslocal t = {} for I = 1, 100000 do t [I] = "a" endlocal s = table.concat (t, "") copy code function

The naming of the function also follows snake_case:

-- Nolocal function testNginx () end copy code-- Yeslocal function test_nginx () end copy code

Functions should return as early as possible:

-- Nolocal function check (age, name) local ret = true if age < 20 then ret = false end if name = = "a" then ret = false end-- do something else return ret copy code-- Yeslocal function check (age Name) if age < 20 then return false end if name = "a" then return false end-- do something else return true copy code module

All require libraries should be local:

-- Nolocal function foo () local ok, err = ngx.timer.at (delay, handler) end copy code-- Yeslocal timer_at = ngx.timer.atlocal function foo () local ok, err = timer_at (delay, handler) end copy code

In order to unify the style, require and ngx also need local:

-- Nolocal core = require ("apisix.core") local timer_at = ngx.timer.atlocal function foo () local ok, err = timer_at (delay, handler) end copy code-- Yeslocal ngx = ngxlocal require = requirelocal core = require ("apisix.core") local timer_at = ngx.timer.atlocal function foo () local ok, err = timer_at (delay, handler) end copy code error handling

For functions with error messages returned, the error messages must be judged and handled:

-Nolocal sock = ngx.socket.tcp () local ok = sock:connect ("www.google.com", 80) ngx.say ("successfully connected to google!") Copy the code-Yeslocal sock = ngx.socket.tcp () local ok, err = sock:connect ("www.google.com", 80) if not ok then ngx.say ("failed to connect to google:", err) return end ngx.say ("successfully connected to google!") Copy the code

For the function written by yourself, the error message is returned in the format of a string as the second argument:

-- Nolocal function foo () local ok, err = func () if not ok then return false end return trueend copy code-- Nolocal function foo () local ok, err = func () if not ok then return false, {msg = err} end return trueend copy code-- Yeslocal function foo () local ok Err = func () if not ok then return false, "failed to call func ():".. Err end return trueend copy code on OpenResty coding which specifications are shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report