forked from gocolly/colly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mod] split colly.go to smaller parts
- Loading branch information
Showing
5 changed files
with
279 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package colly | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// Context provides a tiny layer for passing data between callbacks | ||
type Context struct { | ||
contextMap map[string]interface{} | ||
lock *sync.RWMutex | ||
} | ||
|
||
// NewContext initializes a new Context instance | ||
func NewContext() *Context { | ||
return &Context{ | ||
contextMap: make(map[string]interface{}), | ||
lock: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
// UnmarshalBinary decodes Context value to nil | ||
// This function is used by request caching | ||
func (c *Context) UnmarshalBinary(_ []byte) error { | ||
return nil | ||
} | ||
|
||
// MarshalBinary encodes Context value | ||
// This function is used by request caching | ||
func (c *Context) MarshalBinary() (_ []byte, _ error) { | ||
return nil, nil | ||
} | ||
|
||
// Put stores a value of any type in Context | ||
func (c *Context) Put(key string, value interface{}) { | ||
c.lock.Lock() | ||
c.contextMap[key] = value | ||
c.lock.Unlock() | ||
} | ||
|
||
// Get retrieves a string value from Context. | ||
// Get returns an empty string if key not found | ||
func (c *Context) Get(key string) string { | ||
c.lock.RLock() | ||
defer c.lock.RUnlock() | ||
if v, ok := c.contextMap[key]; ok { | ||
return v.(string) | ||
} | ||
return "" | ||
} | ||
|
||
// GetAny retrieves a value from Context. | ||
// GetAny returns nil if key not found | ||
func (c *Context) GetAny(key string) interface{} { | ||
c.lock.RLock() | ||
defer c.lock.RUnlock() | ||
if v, ok := c.contextMap[key]; ok { | ||
return v | ||
} | ||
return nil | ||
} |
Oops, something went wrong.