github.com/lei006/gmqtt-broker@v0.0.1/plugins/auth/authfile/spilt.go (about)

     1  package acl
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"strings"
     7  )
     8  
     9  func SubscribeTopicSpilt(topic string) ([]string, error) {
    10  	subject := []byte(topic)
    11  	if bytes.IndexByte(subject, '#') != -1 {
    12  		if bytes.IndexByte(subject, '#') != len(subject)-1 {
    13  			return nil, errors.New("Topic format error with index of #")
    14  		}
    15  	}
    16  	re := strings.Split(topic, "/")
    17  	for i, v := range re {
    18  		if i != 0 && i != (len(re)-1) {
    19  			if v == "" {
    20  				return nil, errors.New("Topic format error with index of //")
    21  			}
    22  			if strings.Contains(v, "+") && v != "+" {
    23  				return nil, errors.New("Topic format error with index of +")
    24  			}
    25  		} else {
    26  			if v == "" {
    27  				re[i] = "/"
    28  			}
    29  		}
    30  	}
    31  	return re, nil
    32  
    33  }
    34  
    35  func PublishTopicSpilt(topic string) ([]string, error) {
    36  	subject := []byte(topic)
    37  	if bytes.IndexByte(subject, '#') != -1 || bytes.IndexByte(subject, '+') != -1 {
    38  		return nil, errors.New("Publish Topic format error with + and #")
    39  	}
    40  	re := strings.Split(topic, "/")
    41  	for i, v := range re {
    42  		if v == "" {
    43  			if i != 0 && i != (len(re)-1) {
    44  				return nil, errors.New("Topic format error with index of //")
    45  			} else {
    46  				re[i] = "/"
    47  			}
    48  		}
    49  
    50  	}
    51  	return re, nil
    52  }