aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/server_hello.go
blob: dbe81063031adf4956f4a4eb47e8acfcb64aa0ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package proto

import (
	"fmt"
	"strings"

	"github.com/go-faster/errors"
)

// ServerHello is answer to ClientHello and represents ServerCodeHello message.
type ServerHello struct {
	Name        string
	Major       int
	Minor       int
	Revision    int
	Timezone    string
	DisplayName string
	Patch       int
}

// Features implemented by server.
func (s ServerHello) Features() []Feature {
	var features []Feature
	for _, f := range FeatureValues() {
		if s.Has(f) {
			features = append(features, f)
		}
	}
	return features
}

// Has reports whether Feature is implemented.
func (s ServerHello) Has(f Feature) bool {
	return f.In(s.Revision)
}

func (s ServerHello) String() string {
	var b strings.Builder
	b.WriteString(s.Name)
	if s.DisplayName != "" {
		_, _ = fmt.Fprintf(&b, " (%s", s.DisplayName)
		if s.Timezone != "" {
			b.WriteString(", ")
			b.WriteString(s.Timezone)
		}
		b.WriteRune(')')
	}

	_, _ = fmt.Fprintf(&b, " %d.%d", s.Major, s.Minor)
	if s.Has(FeatureVersionPatch) {
		_, _ = fmt.Fprintf(&b, ".%d", s.Patch)
	}
	_, _ = fmt.Fprintf(&b, " (%d)", s.Revision)
	return b.String()
}

// DecodeAware decodes ServerHello message from Reader.
func (s *ServerHello) DecodeAware(r *Reader, v int) error {
	name, err := r.Str()
	if err != nil {
		return errors.Wrap(err, "str")
	}
	s.Name = name

	major, err := r.Int()
	if err != nil {
		return errors.Wrap(err, "major")
	}
	minor, err := r.Int()
	if err != nil {
		return errors.Wrap(err, "minor")
	}
	revision, err := r.Int()
	if err != nil {
		return errors.Wrap(err, "revision")
	}

	s.Major, s.Minor, s.Revision = major, minor, revision

	if FeatureTimezone.In(v) {
		v, err := r.Str()
		if err != nil {
			return errors.Wrap(err, "timezone")
		}
		s.Timezone = v
	}
	if FeatureDisplayName.In(v) {
		v, err := r.Str()
		if err != nil {
			return errors.Wrap(err, "display name")
		}
		s.DisplayName = v
	}
	if FeatureVersionPatch.In(v) {
		path, err := r.Int()
		if err != nil {
			return errors.Wrap(err, "patch")
		}
		s.Patch = path
	}

	return nil
}

func (s *ServerHello) EncodeAware(b *Buffer, v int) {
	ServerCodeHello.Encode(b)
	b.PutString(s.Name)
	b.PutInt(s.Major)
	b.PutInt(s.Minor)
	b.PutInt(s.Revision)
	if FeatureTimezone.In(v) {
		b.PutString(s.Timezone)
	}
	if FeatureDisplayName.In(v) {
		b.PutString(s.DisplayName)
	}
	if FeatureVersionPatch.In(v) {
		b.PutInt(s.Patch)
	}
}