blob: 80b33cdaf90591b2fa0288704c8c84fd14354d31 [file] [log] [blame]
amit.ghosh258d14c2020-10-02 15:13:38 +02001/*
2 *
3 * Copyright 2020 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19// Package grpcutil provides a bunch of utility functions to be used across the
20// gRPC codebase.
21package grpcutil
22
23import (
24 "strings"
25
26 "google.golang.org/grpc/resolver"
27)
28
29// split2 returns the values from strings.SplitN(s, sep, 2).
30// If sep is not found, it returns ("", "", false) instead.
31func split2(s, sep string) (string, string, bool) {
32 spl := strings.SplitN(s, sep, 2)
33 if len(spl) < 2 {
34 return "", "", false
35 }
36 return spl[0], spl[1], true
37}
38
39// ParseTarget splits target into a resolver.Target struct containing scheme,
40// authority and endpoint.
41//
42// If target is not a valid scheme://authority/endpoint, it returns {Endpoint:
43// target}.
44func ParseTarget(target string) (ret resolver.Target) {
45 var ok bool
46 ret.Scheme, ret.Endpoint, ok = split2(target, "://")
47 if !ok {
48 return resolver.Target{Endpoint: target}
49 }
50 ret.Authority, ret.Endpoint, ok = split2(ret.Endpoint, "/")
51 if !ok {
52 return resolver.Target{Endpoint: target}
53 }
54 return ret
55}