2022-10-24 23:06:49 +08:00
|
|
|
package components
|
|
|
|
|
|
|
|
type Block struct {
|
2022-11-30 17:33:30 +08:00
|
|
|
Line int
|
2022-10-24 23:06:49 +08:00
|
|
|
Comment string
|
|
|
|
Directives []IDirective
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) GetDirectives() []IDirective {
|
|
|
|
return b.Directives
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) GetComment() string {
|
|
|
|
return b.Comment
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:33:30 +08:00
|
|
|
func (b *Block) GetLine() int {
|
|
|
|
return b.Line
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:06:49 +08:00
|
|
|
func (b *Block) FindDirectives(directiveName string) []IDirective {
|
|
|
|
directives := make([]IDirective, 0)
|
|
|
|
for _, directive := range b.GetDirectives() {
|
|
|
|
if directive.GetName() == directiveName {
|
|
|
|
directives = append(directives, directive)
|
|
|
|
}
|
|
|
|
if directive.GetBlock() != nil {
|
|
|
|
directives = append(directives, directive.GetBlock().FindDirectives(directiveName)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return directives
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:33:30 +08:00
|
|
|
//func (b *Block) UpdateDirectives(directiveName string, directive Directive) {
|
|
|
|
// directives := b.GetDirectives()
|
|
|
|
// index := -1
|
|
|
|
// for i, dir := range directives {
|
|
|
|
// if dir.GetName() == directiveName {
|
|
|
|
// index = i
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if index > -1 {
|
|
|
|
// directives[index] = &directive
|
|
|
|
// } else {
|
|
|
|
// directives = append(directives, &directive)
|
|
|
|
// }
|
|
|
|
// b.Directives = directives
|
|
|
|
//}
|
2022-11-08 15:42:31 +08:00
|
|
|
|
2022-11-30 17:33:30 +08:00
|
|
|
func (b *Block) UpdateDirective(key string, params []string) {
|
|
|
|
if key == "" || len(params) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-11-08 15:42:31 +08:00
|
|
|
directives := b.GetDirectives()
|
|
|
|
index := -1
|
|
|
|
for i, dir := range directives {
|
2022-11-30 17:33:30 +08:00
|
|
|
if dir.GetName() == key {
|
|
|
|
if IsRepeatKey(key) {
|
|
|
|
oldParams := dir.GetParameters()
|
|
|
|
if !(len(oldParams) > 0 && oldParams[0] == params[0]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 15:42:31 +08:00
|
|
|
index = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 17:33:30 +08:00
|
|
|
newDirective := &Directive{
|
|
|
|
Name: key,
|
|
|
|
Parameters: params,
|
|
|
|
}
|
2022-11-08 15:42:31 +08:00
|
|
|
if index > -1 {
|
2022-11-30 17:33:30 +08:00
|
|
|
directives[index] = newDirective
|
2022-11-08 15:42:31 +08:00
|
|
|
} else {
|
2022-11-30 17:33:30 +08:00
|
|
|
directives = append(directives, newDirective)
|
2022-11-08 15:42:31 +08:00
|
|
|
}
|
|
|
|
b.Directives = directives
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:33:30 +08:00
|
|
|
//func (b *Block) UpdateDirectiveBySecondKey(name string, key string, directive Directive) {
|
|
|
|
//
|
|
|
|
// directives := b.GetDirectives()
|
|
|
|
//
|
|
|
|
// index := -1
|
|
|
|
// for i, dir := range directives {
|
|
|
|
// if dir.GetName() == name && dir.GetParameters()[0] == key {
|
|
|
|
// index = i
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if index > -1 {
|
|
|
|
// directives[index] = &directive
|
|
|
|
// } else {
|
|
|
|
// directives = append(directives, &directive)
|
|
|
|
// }
|
|
|
|
// b.Directives = directives
|
|
|
|
//}
|
2022-10-28 17:04:57 +08:00
|
|
|
|
2022-11-30 17:33:30 +08:00
|
|
|
//func (b *Block) RemoveDirectives(names []string) {
|
|
|
|
// nameMaps := make(map[string]struct{}, len(names))
|
|
|
|
// for _, name := range names {
|
|
|
|
// nameMaps[name] = struct{}{}
|
|
|
|
// }
|
|
|
|
// directives := b.GetDirectives()
|
|
|
|
// var newDirectives []IDirective
|
|
|
|
// for _, dir := range directives {
|
|
|
|
// if _, ok := nameMaps[dir.GetName()]; ok {
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
// newDirectives = append(newDirectives, dir)
|
|
|
|
// }
|
|
|
|
// b.Directives = newDirectives
|
|
|
|
//}
|
|
|
|
|
|
|
|
func (b *Block) RemoveDirective(key string, params []string) {
|
2022-10-28 17:04:57 +08:00
|
|
|
directives := b.GetDirectives()
|
|
|
|
var newDirectives []IDirective
|
|
|
|
for _, dir := range directives {
|
2022-11-30 17:33:30 +08:00
|
|
|
if dir.GetName() == key {
|
|
|
|
if IsRepeatKey(key) {
|
|
|
|
oldParams := dir.GetParameters()
|
|
|
|
if len(oldParams) > 0 && oldParams[0] == params[0] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-28 17:04:57 +08:00
|
|
|
}
|
|
|
|
newDirectives = append(newDirectives, dir)
|
|
|
|
}
|
|
|
|
b.Directives = newDirectives
|
|
|
|
}
|