[PR #62] [MERGED] Fix/get more than2 level parent directive err #63

Closed
opened 2026-02-28 01:20:45 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/tufanbarisyildirim/gonginx/pull/62
Author: @mofantor
Created: 12/22/2024
Status: Merged
Merged: 12/25/2024
Merged by: @tufanbarisyildirim

Base: masterHead: fix/GetMoreThan2LevelParentDirectiveErr


📝 Commits (2)

  • d6505f3 Fix GetParent and change Parent type to IDirective
  • aa73e3c update user guide

📊 Changes

13 files changed (+145 additions, -46 deletions)

View changed files

📝 GUIDE.md (+6 -6)
📝 config/block.go (+3 -3)
📝 config/directive.go (+3 -3)
📝 config/http.go (+3 -3)
📝 config/include.go (+3 -3)
📝 config/location.go (+19 -3)
📝 config/lua_block.go (+3 -3)
📝 config/server.go (+32 -3)
📝 config/statement.go (+4 -4)
📝 config/upstream.go (+3 -3)
📝 config/upstream_server.go (+3 -3)
📝 parser/parser.go (+10 -1)
📝 parser/parser_test.go (+53 -8)

📄 Description

Fix Issue with GetParent Method

Problem Description:
I discovered that when obtaining a directive and attempting to retrieve its parent node more than twice, an error would occur. After investigation, I found that this issue arises because the SetParent method sets the parent as the block of the parent directive rather than the parent directive itself.

Solution:
To address this, I modified the SetParent and GetParent methods so that for each directive, its parent should be another directive rather than a block. This ensures that the parent-child relationship between directives is correctly maintained.

Test Case:
The following test case previously failed due to the issue but now runs successfully after the fix:

p := NewStringParser(`user www www;
http {
	include mime.types;
	server {
		listen 80;
		location / {
			proxy_pass http://backend/;
		}
	}
}
`)
conf, err := p.Parse()
assert.NilError(t, err, "no error expected here")

listens := conf.FindDirectives("listen")
assert.Equal(t, len(listens), 1, "num of listen error")

serverIBlock := listens[0].GetParent()
server, ok := serverIBlock.(*config.Server)

assert.Equal(t, ok, true, "cannot convert listen parent to server")

_, ok = server.GetParent().(*config.HTTP)
assert.Equal(t, ok, true, "cannot convert server parent to http")

---

<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
## 📋 Pull Request Information **Original PR:** https://github.com/tufanbarisyildirim/gonginx/pull/62 **Author:** [@mofantor](https://github.com/mofantor) **Created:** 12/22/2024 **Status:** ✅ Merged **Merged:** 12/25/2024 **Merged by:** [@tufanbarisyildirim](https://github.com/tufanbarisyildirim) **Base:** `master` ← **Head:** `fix/GetMoreThan2LevelParentDirectiveErr` --- ### 📝 Commits (2) - [`d6505f3`](https://github.com/tufanbarisyildirim/gonginx/commit/d6505f3e5ecd61e0c7698ac40e4021530a40e3d7) Fix GetParent and change Parent type to IDirective - [`aa73e3c`](https://github.com/tufanbarisyildirim/gonginx/commit/aa73e3cd7b6f4604a7e168dd0749126820c22385) update user guide ### 📊 Changes **13 files changed** (+145 additions, -46 deletions) <details> <summary>View changed files</summary> 📝 `GUIDE.md` (+6 -6) 📝 `config/block.go` (+3 -3) 📝 `config/directive.go` (+3 -3) 📝 `config/http.go` (+3 -3) 📝 `config/include.go` (+3 -3) 📝 `config/location.go` (+19 -3) 📝 `config/lua_block.go` (+3 -3) 📝 `config/server.go` (+32 -3) 📝 `config/statement.go` (+4 -4) 📝 `config/upstream.go` (+3 -3) 📝 `config/upstream_server.go` (+3 -3) 📝 `parser/parser.go` (+10 -1) 📝 `parser/parser_test.go` (+53 -8) </details> ### 📄 Description ### Fix Issue with `GetParent` Method **Problem Description:** I discovered that when obtaining a directive and attempting to retrieve its parent node more than twice, an error would occur. After investigation, I found that this issue arises because the `SetParent` method sets the parent as the block of the parent directive rather than the parent directive itself. **Solution:** To address this, I modified the `SetParent` and `GetParent` methods so that for each directive, its parent should be another directive rather than a block. This ensures that the parent-child relationship between directives is correctly maintained. **Test Case:** The following test case previously failed due to the issue but now runs successfully after the fix: ```go p := NewStringParser(`user www www; http { include mime.types; server { listen 80; location / { proxy_pass http://backend/; } } } `) conf, err := p.Parse() assert.NilError(t, err, "no error expected here") listens := conf.FindDirectives("listen") assert.Equal(t, len(listens), 1, "num of listen error") serverIBlock := listens[0].GetParent() server, ok := serverIBlock.(*config.Server) assert.Equal(t, ok, true, "cannot convert listen parent to server") _, ok = server.GetParent().(*config.HTTP) assert.Equal(t, ok, true, "cannot convert server parent to http") --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-28 01:20:45 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/gonginx#63
No description provided.