コード例 #1
0
ファイル: erg.go プロジェクト: nishanthvasudevan/erg
// Compress takes a slice of strings as argument
// and returns a compressed form.
func (*Erg) Compress(nodes []string) (result string) {
	grangeResult := grange.NewResult()
	for _, node := range nodes {
		grangeResult.Add(node)
	}
	return grange.Compress(&grangeResult)
}
コード例 #2
0
ファイル: erg.go プロジェクト: nishanthvasudevan/erg
// Expand takes a range expression as argument
// and returns an slice of strings as result
// err is set to nil on success
func (e *Erg) Expand(query string) (result []string, err error) {
	protocol := "http"

	if e.ssl {
		protocol = "https"
	}
	// TODO: Remove this with go 1.4
	// http://stackoverflow.com/questions/25008571/golang-issue-x509-cannot-verify-signature-algorithm-unimplemented-on-net-http
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{
			MaxVersion:               tls.VersionTLS11,
			PreferServerCipherSuites: true,
		},
	}
	client := &http.Client{Transport: tr}

	resp, err := client.Get(fmt.Sprintf("%s://%s:%d/range/list?%s",
		protocol,
		e.host,
		e.port,
		url.QueryEscape(query),
	))

	if err != nil {
		return nil, err
	}

	if resp.StatusCode != 200 {
		body, readErr := ioutil.ReadAll(resp.Body)
		if readErr != nil {
			return nil, readErr
		}
		return nil, errors.New(string(body))
	}

	defer resp.Body.Close()
	scanner := bufio.NewScanner(resp.Body)

	grangeResult := grange.NewResult()
	for scanner.Scan() {
		grangeResult.Add(scanner.Text())
	}

	if grangeResult.Cardinality() > 0 {
		for node := range grangeResult.Iter() {
			result = append(result, node.(string))
		}
		if e.Sort {
			sort.Sort(sortorder.Natural(result))
		}
	}

	return result, nil
}