-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from jxskiss/optimize_logid
refactor logid
- Loading branch information
Showing
17 changed files
with
413 additions
and
120 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,57 +1,119 @@ | ||
package logid | ||
|
||
import ( | ||
"encoding/base32" | ||
"net" | ||
"time" | ||
) | ||
|
||
var defaultGen *v1Gen | ||
var defaultGen Generator | ||
|
||
func init() { | ||
defaultGen = &v1Gen{} | ||
defaultGen = NewV1Gen() | ||
} | ||
|
||
type Generator interface { | ||
|
||
// Gen generates a new log ID string, it should always return | ||
// a valid log ID, and don't generate duplicate log IDs. | ||
Gen() string | ||
} | ||
|
||
// SetDefault changes the default generator. | ||
// | ||
// The default generator may be changed by the main program, | ||
// but generally library code shall not call this function. | ||
func SetDefault(gen Generator) { | ||
defaultGen = gen | ||
} | ||
|
||
// Gen generates a new log ID string using the default generator. | ||
func Gen() string { | ||
return defaultGen.Gen() | ||
} | ||
|
||
var b32Enc = base32. | ||
NewEncoding("0123456789abcdefghijklmnopqrstuv"). | ||
WithPadding(base32.NoPadding) | ||
|
||
// minLength is the minimum length of a log ID generated by this package. | ||
// Update this when adding new generators. | ||
const minLength = 47 | ||
const minLength = v1Length | ||
|
||
// strTimeMilli is the time format used in string form of a log ID info. | ||
const strTimeMilli = "20060102150405.000Z0700" | ||
|
||
// Decode decodes a log ID string and returns the parsed information. | ||
func Decode(s string) (info Info) { | ||
if len(s) >= minLength { | ||
switch s[0] { | ||
case v1Version: | ||
return Info{decodeV1(s)} | ||
case v2Version: | ||
return Info{decodeV2(s)} | ||
} | ||
} | ||
return // invalid | ||
} | ||
|
||
// Info holds parsed information of a log ID string. | ||
type Info struct { | ||
infoInterface | ||
} | ||
|
||
// Valid tells whether the info holds valid log ID information. | ||
func (i Info) Valid() bool { | ||
return i.infoInterface != nil && i.infoInterface.Valid() | ||
} | ||
|
||
// Version returns the log ID's version. | ||
func (i Info) Version() string { | ||
if i.Valid() { | ||
return i.infoInterface.Version() | ||
} | ||
return "0" | ||
} | ||
|
||
// Time returns the time information of the log ID if available, | ||
// else it returns a zero time.Time{}. | ||
func (i Info) Time() time.Time { | ||
if i.Valid() { | ||
return i.infoInterface.Time() | ||
} | ||
return time.Time{} | ||
} | ||
|
||
// IP returns the IP information of the log ID if available, | ||
// else it returns nil. | ||
func (i Info) IP() net.IP { | ||
if i.Valid() { | ||
return i.infoInterface.IP() | ||
} | ||
return nil | ||
} | ||
|
||
// Random returns the random part of the log ID if available, | ||
// else it returns an empty string. | ||
func (i Info) Random() string { | ||
if i.Valid() { | ||
return i.infoInterface.Random() | ||
} | ||
return "" | ||
} | ||
|
||
// String formats the log ID's information to string. | ||
func (i Info) String() string { | ||
if i.infoInterface == nil { | ||
return "0|invalid" | ||
if i.Valid() { | ||
return i.infoInterface.String() | ||
} | ||
return i.infoInterface.String() | ||
return "0|invalid" | ||
} | ||
|
||
type infoInterface interface { | ||
Valid() bool | ||
Version() string | ||
Time() time.Time | ||
IP() net.IP | ||
Random() int | ||
Random() string | ||
String() string | ||
} |
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
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
Oops, something went wrong.