Skip to content

Commit bff51e8

Browse files
committed
Extend the include syntax. v2fly#390
1 parent 0176ce5 commit bff51e8

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed

main.go

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,45 @@ func Load(path string) (*List, error) {
219219
return list, nil
220220
}
221221

222+
func isMatchAttr(Attrs []*router.Domain_Attribute, includeKey string) bool {
223+
isMatch := false
224+
mustMatch := true
225+
matchName := includeKey
226+
if strings.HasPrefix(includeKey, "!") {
227+
isMatch = true
228+
mustMatch = false
229+
matchName = strings.TrimLeft(includeKey, "!")
230+
}
231+
232+
for _, Attr := range Attrs {
233+
attrName := Attr.Key
234+
if mustMatch {
235+
if matchName == attrName {
236+
isMatch = true
237+
break
238+
}
239+
} else {
240+
if matchName == attrName {
241+
isMatch = false
242+
break
243+
}
244+
}
245+
}
246+
return isMatch
247+
}
248+
249+
func createIncludeAttrEntrys(list *List, matchAttr *router.Domain_Attribute) []Entry {
250+
newEntryList := make([]Entry, 0, len(list.Entry))
251+
matchName := matchAttr.Key
252+
for _, entry := range list.Entry {
253+
matched := isMatchAttr(entry.Attrs, matchName)
254+
if matched {
255+
newEntryList = append(newEntryList, entry)
256+
}
257+
}
258+
return newEntryList
259+
}
260+
222261
func ParseList(list *List, ref map[string]*List) (*ParsedList, error) {
223262
pl := &ParsedList{
224263
Name: list.Name,
@@ -231,15 +270,35 @@ func ParseList(list *List, ref map[string]*List) (*ParsedList, error) {
231270
for _, entry := range entryList {
232271
if entry.Type == "include" {
233272
refName := strings.ToUpper(entry.Value)
234-
if pl.Inclusion[refName] {
235-
continue
236-
}
237-
pl.Inclusion[refName] = true
238-
r := ref[refName]
239-
if r == nil {
240-
return nil, errors.New(entry.Value + " not found.")
273+
if entry.Attrs != nil {
274+
for _, attr := range entry.Attrs {
275+
InclusionName := strings.ToUpper(refName + "@" + attr.Key)
276+
if pl.Inclusion[InclusionName] {
277+
continue
278+
}
279+
pl.Inclusion[InclusionName] = true
280+
281+
refList := ref[refName]
282+
if refList == nil {
283+
return nil, errors.New(entry.Value + " not found.")
284+
}
285+
attrEntrys := createIncludeAttrEntrys(refList, attr)
286+
if len(attrEntrys) != 0 {
287+
newEntryList = append(newEntryList, attrEntrys...)
288+
}
289+
}
290+
} else {
291+
InclusionName := refName
292+
if pl.Inclusion[InclusionName] {
293+
continue
294+
}
295+
pl.Inclusion[InclusionName] = true
296+
refList := ref[refName]
297+
if refList == nil {
298+
return nil, errors.New(entry.Value + " not found.")
299+
}
300+
newEntryList = append(newEntryList, refList.Entry...)
241301
}
242-
newEntryList = append(newEntryList, r.Entry...)
243302
hasInclude = true
244303
} else {
245304
newEntryList = append(newEntryList, entry)

0 commit comments

Comments
 (0)