Пример #1
0
func (self *Converter) ReadKey(input interface{}) types.Key {
	if input == nil {
		panic("Expecting key but value is missing")
	}
	switch input := input.(type) {
	case string:
		key, err := types.KeyFromString(input)
		if err != nil {
			panic(fmt.Sprintf("Expecting a string that can be parsed to key."+
				"Got '%v', unable to parse that: %v", input, err))
		}
		return key
	case []string:
		// Array of base32 strings
		result := make(types.Key, len(input))
		for index, stringElement := range input {
			binary := self.ReadKeyElement(stringElement)
			result[index] = binary
		}
		return result
	case []interface{}:
		// Array of base32 strings or array of bytes
		result := make(types.Key, len(input))
		for index, element := range input {
			binary := self.ReadKeyElement(element)
			result[index] = binary
		}
		return result
	case [][]byte:
		return types.KeyFromArrayOfBytes(input)
	default:
		panic(fmt.Sprintf("Expecting a string or an array of binary "+
			"but got a %T (value '%v')", input, input))
	}
}
Пример #2
0
func KeyFromString(str string) (key Key, err error) {
	// TODO: Remove dependency to ares
	aresKey, err := types.KeyFromString(str)
	if err != nil {
		return
	}
	key = make(Key, len(aresKey))
	for index, aresKey := range aresKey {
		key[index] = []byte(aresKey)
	}
	return
}