Skip to content

Commit 4c0dcaf

Browse files
committed
feat(resolv): refactored the Context JSON format serialization structure to simplify serialization/deserialization operations, making the serialization structure more universal and unified.
BREAKING CHANGE: Changed the serialization structure of the `Context` `JSON` format, as well as the new functions for the `Directive` and `Comment` context objects. The `Context` `JSON` format serialization structure changes as follows: Before: ```json { "main-config": "C:\\test\\test.conf", "configs": { "C:\\test\\test.conf": [ { "http": { "params": [ { "inline": true, "comments": "test comment" }, { "server": { "params": [ { "directive": "server_name", "params": "testserver" }, { "location": {"value": "~ /test"} }, { "include": { "value": "conf.d\\include*conf", "params": ["conf.d\\include.location1.conf", "conf.d\\include.location2.conf"] } } ] } } ] } } ], "conf.d\\include.location1.conf": [ { "location": {"value": "~ /test1"} } ], "conf.d\\include.location2.conf": [ { "location": {"value": "^~ /test2"} } ] } } ``` After: ```json { "main-config": "C:\\test\\test.conf", "configs": { "C:\\test\\test.conf": { "context-type": "config", "value": "C:\\test\\test.conf", "params": [ { "context-type": "http", "params": [ { "context-type": "inline_comment", "value": "test comment" }, { "context-type": "server", "params": [ { "context-type": "directive", "value": "server_name testserver" }, { "context-type": "location", "value": "~ /test" }, { "context-type": "include", "value": "conf.d\\include*conf", "params": ["conf.d\\include.location1.conf", "conf.d\\include.location2.conf"] } ] } ] } ] }, "conf.d\\include.location1.conf": { "context-type": "config", "value": "conf.d\\include.location1.conf", "params": [ { "context-type": "location", "value": "~ /test1" } ] }, "conf.d\\include.location2.conf": { "context-type": "config", "value": "conf.d\\include.location2.conf", "params": [ { "context-type": "location", "value": "^~ /test2" } ] } } } ``` The code migration example for creating function calls for `Directive` and `Comment` context objects is as follows: Before: ```go import ( "github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context/local" ) // new directive context newDirective := local.NewDirective("some_directive", "some params") // new comment context newComment := local.NewComment("some comments", false) newComment := local.NewComment("some inline comments", true) ``` After: ```go import ( "github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context/local" "github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context_type" ) // new directive context newDirective := local.NewContext(context_type.TypeDirective, "some_directive some params") // new comment context newComment := local.NewContext(context_type.TypeComment, "some comments") newInlineComment := local.NewContext(context_type.TypeInlineComment, "some inline comments") ```
1 parent 669a509 commit 4c0dcaf

20 files changed

+592
-1735
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ package main
263263

264264
import (
265265
"fmt"
266+
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context_type"
266267

267268
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration"
268269
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context"
@@ -286,7 +287,7 @@ func main() {
286287
QueryByKeyWords(context.NewKeyWords(context_type.TypeIf).SetRegexpMatchingValue(`^\(\$http_api_name != ''\)$`)).Target().
287288
QueryByKeyWords(context.NewKeyWords(context_type.TypeDirective).SetStringMatchingValue("proxy_pass")).Position()
288289
// insert an inline comment after the "proxy_pass" `directive` context
289-
err = ctx.Insert(local.NewComment(fmt.Sprintf("[%s]test comments", time.Now().String()), true), idx+1).Error()
290+
err = ctx.Insert(local.NewContext(context_type.TypeInlineComment, fmt.Sprintf("[%s]test comments", time.Now().String())), idx+1).Error()
290291
if err != nil {
291292
panic(err)
292293
}
@@ -308,9 +309,10 @@ func main() {
308309
// new main context
309310
newMainContext, err := local.NewMain("/usr/local/nginx/conf/nginx.conf")
310311
// new directive context
311-
newDirective := local.NewDirective("some_directive", "some params")
312+
newDirective := local.NewContext(context_type.TypeDirective, "some_directive some params")
312313
// new comment context
313-
newComment := local.NewComment("some comments", false)
314+
newComment := local.NewContext(context_type.TypeComment, "some comments")
315+
newInlineComment := local.NewContext(context_type.TypeInlineComment, "some inline comments")
314316
// new other context
315317
newConfig := local.NewContext(context_type.TypeConfig, "conf.d/location.conf")
316318
newInclude := local.NewContext(context_type.TypeInclude, "conf.d/*.conf")

pkg/resolv/V3/nginx/configuration/context/local/basic_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
const INDENT = " "
1111

1212
type BasicContext struct {
13-
ContextType context_type.ContextType `json:"-"`
13+
ContextType context_type.ContextType `json:"context-type"`
1414
ContextValue string `json:"value,omitempty"`
1515
Children []context.Context `json:"params,omitempty"`
1616

pkg/resolv/V3/nginx/configuration/context/local/basic_context_test.go

Lines changed: 73 additions & 73 deletions
Large diffs are not rendered by default.

pkg/resolv/V3/nginx/configuration/context/local/comment.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package local
22

33
import (
4+
"encoding/json"
45
"github.com/ClessLi/bifrost/internal/pkg/code"
56
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context"
67
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context_type"
@@ -9,12 +10,22 @@ import (
910
)
1011

1112
type Comment struct {
12-
Comments string `json:"comments"`
13-
Inline bool `json:"inline,omitempty"`
13+
Comments string
14+
Inline bool
1415

1516
fatherContext context.Context
1617
}
1718

19+
func (c *Comment) MarshalJSON() ([]byte, error) {
20+
return json.Marshal(struct {
21+
ContextType context_type.ContextType `json:"context-type"`
22+
Value string `json:"value"`
23+
}{
24+
ContextType: c.Type(),
25+
Value: c.Value(),
26+
})
27+
}
28+
1829
func (c *Comment) Insert(ctx context.Context, idx int) context.Context {
1930
return context.ErrContext(errors.WithCode(code.ErrV3InvalidOperation, "comment cannot insert context"))
2031
}
@@ -91,22 +102,33 @@ func (c *Comment) ConfigLines(isDumping bool) ([]string, error) {
91102
return []string{"# " + c.Value()}, nil
92103
}
93104

94-
func NewComment(comments string, isInline bool) *Comment {
95-
return &Comment{
96-
Comments: comments,
97-
Inline: isInline,
98-
fatherContext: context.NullContext(),
105+
func registerCommentBuilder() error {
106+
builderMap[context_type.TypeComment] = func(value string) context.Context {
107+
return &Comment{
108+
Comments: value,
109+
Inline: false,
110+
fatherContext: context.NullContext(),
111+
}
99112
}
113+
builderMap[context_type.TypeInlineComment] = func(value string) context.Context {
114+
return &Comment{
115+
Comments: value,
116+
Inline: true,
117+
fatherContext: context.NullContext(),
118+
}
119+
}
120+
return nil
100121
}
101122

102123
func registerCommentParseFunc() error {
103124
inStackParseFuncMap[context_type.TypeComment] = func(data []byte, idx *int) context.Context {
104125
if subMatch := RegCommentHead.FindSubmatch(data[*idx:]); len(subMatch) == 3 { //nolint:nestif
105126
matchIndexes := RegCommentHead.FindIndex(data[*idx:])
106-
cmt := NewComment(
107-
string(subMatch[2]),
108-
!RegLineBreak.Match(subMatch[1]) && *idx != 0,
109-
)
127+
ct := context_type.TypeComment
128+
if !RegLineBreak.Match(subMatch[1]) && *idx != 0 {
129+
ct = context_type.TypeInlineComment
130+
}
131+
cmt := NewContext(ct, string(subMatch[2]))
110132
*idx += matchIndexes[len(matchIndexes)-1] - 1
111133

112134
return cmt

pkg/resolv/V3/nginx/configuration/context/local/comment_test.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -575,62 +575,6 @@ func TestComment_Value(t *testing.T) {
575575
}
576576
}
577577

578-
func TestNewComment(t *testing.T) {
579-
type args struct {
580-
comments string
581-
isInline bool
582-
}
583-
tests := []struct {
584-
name string
585-
args args
586-
want *Comment
587-
}{
588-
{
589-
name: "null value comment",
590-
args: args{
591-
comments: "",
592-
isInline: false,
593-
},
594-
want: &Comment{
595-
Comments: "",
596-
Inline: false,
597-
fatherContext: context.NullContext(),
598-
},
599-
},
600-
{
601-
name: "some only space comment",
602-
args: args{
603-
comments: " \t ",
604-
isInline: true,
605-
},
606-
want: &Comment{
607-
Comments: " \t ",
608-
Inline: true,
609-
fatherContext: context.NullContext(),
610-
},
611-
},
612-
{
613-
name: "normal comment",
614-
args: args{
615-
comments: " test comment",
616-
isInline: true,
617-
},
618-
want: &Comment{
619-
Comments: " test comment",
620-
Inline: true,
621-
fatherContext: context.NullContext(),
622-
},
623-
},
624-
}
625-
for _, tt := range tests {
626-
t.Run(tt.name, func(t *testing.T) {
627-
if got := NewComment(tt.args.comments, tt.args.isInline); !reflect.DeepEqual(got, tt.want) {
628-
t.Errorf("NewComment() = %v, want %v", got, tt.want)
629-
}
630-
})
631-
}
632-
}
633-
634578
func Test_registerCommentParseFunc(t *testing.T) {
635579
tests := []struct {
636580
name string

pkg/resolv/V3/nginx/configuration/context/local/config.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package local
22

33
import (
4-
"encoding/json"
54
"github.com/ClessLi/bifrost/internal/pkg/code"
65
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context"
76
"github.com/ClessLi/bifrost/pkg/resolv/V3/nginx/configuration/context_type"
@@ -13,11 +12,7 @@ import (
1312

1413
type Config struct {
1514
BasicContext
16-
context.ConfigPath
17-
}
18-
19-
func (c *Config) MarshalJSON() ([]byte, error) {
20-
return json.Marshal(c.BasicContext.Children)
15+
context.ConfigPath `json:"-"`
2116
}
2217

2318
func (c *Config) isInGraph() bool {

0 commit comments

Comments
 (0)