@@ -3,26 +3,51 @@ package options
33import (
44 "fmt"
55 "strings"
6+
7+ "github.com/gluster/glusterd2/pkg/utils"
68)
79
810// InvalidKeyError is returned by SplitKey if key is not of the correct form.
911type InvalidKeyError string
1012
1113func (e InvalidKeyError ) Error () string {
12- return fmt .Sprintf ("option key not in <graph>.<xlator>.<name> form: %s" , string (e ))
14+ return fmt .Sprintf ("option key not in [<graph>.]<xlator>.<option-name> form: %s" , string (e ))
15+ }
16+
17+ var validGraphs = [... ]string {
18+ "brick" ,
19+ "fuse" ,
20+ "gfproxy" ,
21+ "nfs" ,
1322}
1423
1524// SplitKey returns three strings by breaking key of the form
16- // [<graph>].<xlator>.<name> into its constituents. <graph> is optional.
25+ // [<graph>].<xlator>.<option- name> into its constituents. <graph> is optional.
1726// Returns an InvalidKeyError if key is not of correcf form.
1827func SplitKey (k string ) (string , string , string , error ) {
28+ var graph , xlator , optName string
29+
1930 tmp := strings .Split (strings .TrimSpace (k ), "." )
20- switch len (tmp ) {
21- case 2 :
22- return "" , tmp [0 ], tmp [1 ], nil
23- case 3 :
24- return tmp [0 ], tmp [1 ], tmp [2 ], nil
25- default :
26- return "" , "" , "" , InvalidKeyError (k )
31+ if len (tmp ) < 2 {
32+ // must at least be of the form <xlator>.<name>
33+ return graph , xlator , optName , InvalidKeyError (k )
2734 }
35+
36+ if utils .StringInSlice (tmp [0 ], validGraphs [:]) {
37+ // valid graph present
38+ if len (tmp ) < 3 {
39+ // must be of the form <graph>.<xlator>.<name>
40+ return graph , xlator , optName , InvalidKeyError (k )
41+ }
42+ graph = tmp [0 ]
43+ xlator = tmp [1 ]
44+ optName = strings .Join (tmp [2 :], "." )
45+ } else {
46+ // key is of the format <xlator>.<name> where <name> itself
47+ // may contain dots. For example: transport.socket.ssl-enabled
48+ xlator = tmp [0 ]
49+ optName = k [len (xlator )+ 1 :]
50+ }
51+
52+ return graph , xlator , optName , nil
2853}
0 commit comments