Merge pull request #6149 from bhandras/graph_import_rpc
lnd: add `devrpc` sub server and `devrpc.ImportGraph` to import graph dumps
This commit is contained in:
28 files changed
+1403
-70
No files matched your search
@@ -0,0 +1,34 @@
|
||||
package lncfg
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"image/color"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var (
|
||||
// validColorRegexp is a regexp that lets you check if a particular
|
||||
// color string matches the standard hex color format #RRGGBB.
|
||||
validColorRegexp = regexp.MustCompile("^#[A-Fa-f0-9]{6}$")
|
||||
)
|
||||
|
||||
// ParseHexColor takes a hex string representation of a color in the
|
||||
// form "#RRGGBB", parses the hex color values, and returns a color.RGBA
|
||||
// struct of the same color.
|
||||
func ParseHexColor(colorStr string) (color.RGBA, error) {
|
||||
// Check if the hex color string is a valid color representation.
|
||||
if !validColorRegexp.MatchString(colorStr) {
|
||||
return color.RGBA{}, errors.New("color must be specified " +
|
||||
"using a hexadecimal value in the form #RRGGBB")
|
||||
}
|
||||
|
||||
// Decode the hex color string to bytes.
|
||||
// The resulting byte array is in the form [R, G, B].
|
||||
colorBytes, err := hex.DecodeString(colorStr[1:])
|
||||
if err != nil {
|
||||
return color.RGBA{}, err
|
||||
}
|
||||
|
||||
return color.RGBA{R: colorBytes[0], G: colorBytes[1], B: colorBytes[2]}, nil
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package lncfg
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseHexColor(t *testing.T) {
|
||||
var colorTestCases = []struct {
|
||||
test string
|
||||
valid bool // If valid format
|
||||
R byte
|
||||
G byte
|
||||
B byte
|
||||
}{
|
||||
{"#123", false, 0, 0, 0},
|
||||
{"#1234567", false, 0, 0, 0},
|
||||
{"$123456", false, 0, 0, 0},
|
||||
{"#12345+", false, 0, 0, 0},
|
||||
{"#fFGG00", false, 0, 0, 0},
|
||||
{"", false, 0, 0, 0},
|
||||
{"#123456", true, 0x12, 0x34, 0x56},
|
||||
{"#C0FfeE", true, 0xc0, 0xff, 0xee},
|
||||
}
|
||||
|
||||
// Perform the table driven tests.
|
||||
for _, ct := range colorTestCases {
|
||||
|
||||
color, err := ParseHexColor(ct.test)
|
||||
if !ct.valid && err == nil {
|
||||
t.Fatalf("Invalid color string: %s, should return "+
|
||||
"error, but did not", ct.test)
|
||||
}
|
||||
|
||||
if ct.valid && err != nil {
|
||||
t.Fatalf("Color %s valid to parse: %s", ct.test, err)
|
||||
}
|
||||
|
||||
// Ensure that the string to hex decoding is working properly.
|
||||
if color.R != ct.R || color.G != ct.G || color.B != ct.B {
|
||||
t.Fatalf("Color %s incorrectly parsed as %v", ct.test, color)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user