initial
This commit is contained in:
commit
5dd1164165
24
oid64.go
Normal file
24
oid64.go
Normal file
@ -0,0 +1,24 @@
|
||||
package oid64
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func ObjectIDToBase64(oid string) (string, error) {
|
||||
b, e := hex.DecodeString(oid)
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
s := base64.URLEncoding.EncodeToString(b)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func Base64ToObjectID(bid string) (string, error) {
|
||||
b, e := base64.URLEncoding.DecodeString(bid)
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
s := hex.EncodeToString(b)
|
||||
return s, nil
|
||||
}
|
31
oid64_test.go
Normal file
31
oid64_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package oid64
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestObjectIDToBase64(t *testing.T) {
|
||||
in := "507f191e810c19729de860ea"
|
||||
exOut := "UH8ZHoEMGXKd6GDq"
|
||||
|
||||
out, e := ObjectIDToBase64(in)
|
||||
if e != nil {
|
||||
t.Fatal(e.Error())
|
||||
}
|
||||
if out != exOut {
|
||||
t.Logf("Results don't match. Expected %s got %s", exOut, out)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestBase64ToObjectID(t *testing.T) {
|
||||
in := "UH8ZHoEMGXKd6GDq"
|
||||
exOut := "507f191e810c19729de860ea"
|
||||
|
||||
out, e := Base64ToObjectID(in)
|
||||
if e != nil {
|
||||
t.Fatal(e.Error())
|
||||
}
|
||||
if out != exOut {
|
||||
t.Logf("Results don't match. Expected %s got %s", exOut, out)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user