github.com/signintech/pdft@v0.5.0/crawl_result.go (about) 1 package pdft 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "strings" 8 ) 9 10 type crawlResult struct { 11 items []crawlResultItem 12 } 13 14 func (c *crawlResult) String() string { 15 var buff bytes.Buffer 16 loopCrawlResultString(&buff, c) 17 return buff.String() 18 } 19 20 // ErrCrawlResultValOfNotFound CrawlResult Val Of Not Found 21 var ErrCrawlResultValOfNotFound = errors.New("CrawlResult Val Of Not Found") 22 23 func (c *crawlResult) valOf(key string) (string, error) { 24 return loopValOf(c, key) 25 } 26 27 func (c *crawlResult) setValOf(key string, val string) error { 28 return loopSetValOf(c, key, val) 29 } 30 31 func (c *crawlResult) add(key string, val string) error { 32 var item crawlResultItem 33 item.key = key 34 item.valStr = val 35 item.typeOfVal = typeOfValStr 36 //fmt.Printf("key===%s\n", key) 37 c.items = append(c.items, item) 38 return nil 39 } 40 41 func loopSetValOf(cr *crawlResult, key string, val string) error { 42 for i := range cr.items { 43 item := &cr.items[i] 44 if item.typeOfVal == typeOfValStr { 45 if item.key == key { 46 item.setValStr(val) 47 return nil 48 } 49 } else if item.typeOfVal == typeOfValCr { 50 v, _ := item.getValCr() 51 return loopSetValOf(v, key, val) 52 } 53 } 54 return ErrCrawlResultValOfNotFound 55 } 56 57 func loopValOf(cr *crawlResult, key string) (string, error) { 58 //fmt.Printf("\n#####%s\n", key) 59 for _, item := range cr.items { 60 if item.typeOfVal == typeOfValStr { 61 //fmt.Printf("key=%s\n", item.key) 62 if item.key == key { 63 v, err := item.getValStr() 64 if err != nil { 65 return "", err 66 } 67 return v, nil 68 } 69 } else if item.typeOfVal == typeOfValCr { 70 v, err := item.getValCr() 71 if err != nil { 72 return "", err 73 } 74 return loopValOf(v, key) 75 } 76 } 77 return "", ErrCrawlResultValOfNotFound 78 } 79 80 func loopCrawlResultString(buff *bytes.Buffer, cr *crawlResult) { 81 for _, item := range cr.items { 82 buff.WriteString(fmt.Sprintf("/%s ", item.key)) 83 if item.typeOfVal == typeOfValStr { 84 if strings.TrimSpace(item.valStr) != "" { 85 buff.WriteString(fmt.Sprintf("%s\n", item.valStr)) 86 } 87 } else if item.typeOfVal == typeOfValCr { 88 buff.WriteString("\n\t<<") 89 loopCrawlResultString(buff, item.valCr) 90 buff.WriteString(">>\n") 91 } 92 } 93 } 94 95 var typeOfValStr = "str" 96 var typeOfValCr = "cr" //crawlResult 97 98 type crawlResultItem struct { 99 key string 100 typeOfVal string 101 valCr *crawlResult 102 valStr string 103 } 104 105 // ErrWrongTypeOfVal wrong type of val 106 var ErrWrongTypeOfVal = errors.New("wrong type of val") 107 108 func (c *crawlResultItem) getValCr() (*crawlResult, error) { 109 if c.typeOfVal != typeOfValCr { 110 return nil, ErrWrongTypeOfVal 111 } 112 return c.valCr, nil 113 } 114 115 func (c *crawlResultItem) getValStr() (string, error) { 116 if c.typeOfVal != typeOfValStr { 117 return "", ErrWrongTypeOfVal 118 } 119 return c.valStr, nil 120 } 121 122 func (c *crawlResultItem) setValStr(v string) { 123 c.typeOfVal = typeOfValStr 124 c.valStr = v 125 } 126 127 func (c *crawlResultItem) setValCr(v *crawlResult) { 128 c.typeOfVal = typeOfValCr 129 c.valCr = v 130 }