/
util.go
executable file
·88 lines (76 loc) · 2.44 KB
/
util.go
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
package servicecenter
import (
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/lager"
"github.com/go-chassis/go-chassis/core/registry"
"github.com/go-chassis/go-chassis/pkg/util/tags"
"github.com/go-chassis/go-sc-client"
"github.com/go-chassis/go-sc-client/model"
"gopkg.in/yaml.v2"
)
// parseSchemaContent parse schema content into SchemaContent structure
func parseSchemaContent(content []byte) (registry.SchemaContent, error) {
var (
err error
schema = ®istry.Schema{}
schemaContent = ®istry.SchemaContent{}
)
if err = yaml.Unmarshal(content, schema); err != nil {
return *schemaContent, err
}
if err = yaml.Unmarshal([]byte(schema.Schema), schemaContent); err != nil {
return *schemaContent, err
}
return *schemaContent, nil
}
// parseSchemaContent parse schema content into SchemaContent structure
func unmarshalSchemaContent(content []byte) (*registry.SchemaContent, error) {
var (
err error
schema = ®istry.Schema{}
schemaContent = ®istry.SchemaContent{}
)
if err = yaml.Unmarshal(content, schema); err != nil {
return schemaContent, err
}
if err = yaml.Unmarshal([]byte(schema.Schema), schemaContent); err != nil {
return schemaContent, err
}
return schemaContent, nil
}
// filterInstances filter instances
func filterInstances(providerInstances []*model.MicroServiceInstance) []*registry.MicroServiceInstance {
instances := make([]*registry.MicroServiceInstance, 0)
for _, ins := range providerInstances {
if ins.Status != model.MSInstanceUP {
continue
}
msi := ToMicroServiceInstance(ins)
instances = append(instances, msi)
}
return instances
}
func closeClient(r *client.RegistryClient) error {
err := r.Close()
if err != nil {
lager.Logger.Errorf(err, "Conn close failed.")
return err
}
lager.Logger.Debugf("Conn close success.")
return nil
}
func wrapTagsForServiceCenter(t utiltags.Tags) utiltags.Tags {
if t.KV != nil {
if v, ok := t.KV[common.BuildinTagVersion]; !ok || v == "" {
t.KV[common.BuildinTagVersion] = common.LatestVersion
t.Label += "|" + common.BuildinLabelVersion
}
if v, ok := t.KV[common.BuildinTagApp]; !ok || v == "" {
t.KV[common.BuildinTagApp] = config.GetGlobalAppID()
t.Label += "|" + common.BuildinTagApp + ":" + config.GetGlobalAppID()
}
return t
}
return utiltags.NewDefaultTag(common.LatestVersion, config.GetGlobalAppID())
}