mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 01:38:10 +00:00
Update custom-metrics-apiserver and metrics-server
This commit is contained in:
parent
4c673534f2
commit
b480e45a67
915 changed files with 63694 additions and 106514 deletions
195
vendor/sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/client/client.go
generated
vendored
Normal file
195
vendor/sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/client/client.go
generated
vendored
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"k8s.io/klog"
|
||||
"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
|
||||
)
|
||||
|
||||
// Tunnel provides ability to dial a connection through a tunnel.
|
||||
type Tunnel interface {
|
||||
// Dial connects to the address on the named network, similar to
|
||||
// what net.Dial does. The only supported protocol is tcp.
|
||||
Dial(protocol, address string) (net.Conn, error)
|
||||
}
|
||||
|
||||
type dialResult struct {
|
||||
err string
|
||||
connid int64
|
||||
}
|
||||
|
||||
// grpcTunnel implements Tunnel
|
||||
type grpcTunnel struct {
|
||||
stream client.ProxyService_ProxyClient
|
||||
pendingDial map[int64]chan<- dialResult
|
||||
conns map[int64]*conn
|
||||
pendingDialLock sync.RWMutex
|
||||
connsLock sync.RWMutex
|
||||
}
|
||||
|
||||
// CreateGrpcTunnel creates a Tunnel to dial to a remote server through a
|
||||
// gRPC based proxy service.
|
||||
func CreateGrpcTunnel(address string, opts ...grpc.DialOption) (Tunnel, error) {
|
||||
c, err := grpc.Dial(address, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
grpcClient := client.NewProxyServiceClient(c)
|
||||
|
||||
stream, err := grpcClient.Proxy(context.Background())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tunnel := &grpcTunnel{
|
||||
stream: stream,
|
||||
pendingDial: make(map[int64]chan<- dialResult),
|
||||
conns: make(map[int64]*conn),
|
||||
}
|
||||
|
||||
go tunnel.serve()
|
||||
|
||||
return tunnel, nil
|
||||
}
|
||||
|
||||
func (t *grpcTunnel) serve() {
|
||||
for {
|
||||
pkt, err := t.stream.Recv()
|
||||
if err == io.EOF {
|
||||
return
|
||||
}
|
||||
if err != nil || pkt == nil {
|
||||
klog.Warningf("stream read error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
klog.V(6).Infof("[tracing] recv packet, type: %s", pkt.Type)
|
||||
|
||||
switch pkt.Type {
|
||||
case client.PacketType_DIAL_RSP:
|
||||
resp := pkt.GetDialResponse()
|
||||
t.pendingDialLock.RLock()
|
||||
ch, ok := t.pendingDial[resp.Random]
|
||||
t.pendingDialLock.RUnlock()
|
||||
|
||||
if !ok {
|
||||
klog.Warning("DialResp not recognized; dropped")
|
||||
} else {
|
||||
ch <- dialResult{
|
||||
err: resp.Error,
|
||||
connid: resp.ConnectID,
|
||||
}
|
||||
}
|
||||
case client.PacketType_DATA:
|
||||
resp := pkt.GetData()
|
||||
// TODO: flow control
|
||||
t.connsLock.RLock()
|
||||
conn, ok := t.conns[resp.ConnectID]
|
||||
t.connsLock.RUnlock()
|
||||
|
||||
if ok {
|
||||
conn.readCh <- resp.Data
|
||||
} else {
|
||||
klog.Warningf("connection id %d not recognized", resp.ConnectID)
|
||||
}
|
||||
case client.PacketType_CLOSE_RSP:
|
||||
resp := pkt.GetCloseResponse()
|
||||
t.connsLock.RLock()
|
||||
conn, ok := t.conns[resp.ConnectID]
|
||||
t.connsLock.RUnlock()
|
||||
|
||||
if ok {
|
||||
close(conn.readCh)
|
||||
conn.closeCh <- resp.Error
|
||||
close(conn.closeCh)
|
||||
t.connsLock.Lock()
|
||||
delete(t.conns, resp.ConnectID)
|
||||
t.connsLock.Unlock()
|
||||
} else {
|
||||
klog.Warningf("connection id %d not recognized", resp.ConnectID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dial connects to the address on the named network, similar to
|
||||
// what net.Dial does. The only supported protocol is tcp.
|
||||
func (t *grpcTunnel) Dial(protocol, address string) (net.Conn, error) {
|
||||
if protocol != "tcp" {
|
||||
return nil, errors.New("protocol not supported")
|
||||
}
|
||||
|
||||
random := rand.Int63()
|
||||
resCh := make(chan dialResult)
|
||||
t.pendingDialLock.Lock()
|
||||
t.pendingDial[random] = resCh
|
||||
t.pendingDialLock.Unlock()
|
||||
defer func() {
|
||||
t.pendingDialLock.Lock()
|
||||
delete(t.pendingDial, random)
|
||||
t.pendingDialLock.Unlock()
|
||||
}()
|
||||
|
||||
req := &client.Packet{
|
||||
Type: client.PacketType_DIAL_REQ,
|
||||
Payload: &client.Packet_DialRequest{
|
||||
DialRequest: &client.DialRequest{
|
||||
Protocol: protocol,
|
||||
Address: address,
|
||||
Random: random,
|
||||
},
|
||||
},
|
||||
}
|
||||
klog.V(6).Infof("[tracing] send packet, type: %s", req.Type)
|
||||
|
||||
err := t.stream.Send(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
klog.Info("DIAL_REQ sent to proxy server")
|
||||
|
||||
c := &conn{stream: t.stream}
|
||||
|
||||
select {
|
||||
case res := <-resCh:
|
||||
if res.err != "" {
|
||||
return nil, errors.New(res.err)
|
||||
}
|
||||
c.connID = res.connid
|
||||
c.readCh = make(chan []byte, 10)
|
||||
c.closeCh = make(chan string)
|
||||
t.connsLock.Lock()
|
||||
t.conns[res.connid] = c
|
||||
t.connsLock.Unlock()
|
||||
case <-time.After(30 * time.Second):
|
||||
return nil, errors.New("dial timeout")
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
141
vendor/sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/client/conn.go
generated
vendored
Normal file
141
vendor/sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/client/conn.go
generated
vendored
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"k8s.io/klog"
|
||||
"sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
|
||||
)
|
||||
|
||||
// CloseTimeout is the timeout to wait CLOSE_RSP packet after a
|
||||
// successful delivery of CLOSE_REQ.
|
||||
const CloseTimeout = 10 * time.Second
|
||||
|
||||
// conn is an implementation of net.Conn, where the data is transported
|
||||
// over an established tunnel defined by a gRPC service ProxyService.
|
||||
type conn struct {
|
||||
stream client.ProxyService_ProxyClient
|
||||
connID int64
|
||||
readCh chan []byte
|
||||
closeCh chan string
|
||||
rdata []byte
|
||||
}
|
||||
|
||||
var _ net.Conn = &conn{}
|
||||
|
||||
// Write sends the data thru the connection over proxy service
|
||||
func (c *conn) Write(data []byte) (n int, err error) {
|
||||
req := &client.Packet{
|
||||
Type: client.PacketType_DATA,
|
||||
Payload: &client.Packet_Data{
|
||||
Data: &client.Data{
|
||||
ConnectID: c.connID,
|
||||
Data: data,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
klog.V(6).Infof("[tracing] send req, type: %s", req.Type)
|
||||
|
||||
err = c.stream.Send(req)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return len(data), err
|
||||
}
|
||||
|
||||
// Read receives data from the connection over proxy service
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
var data []byte
|
||||
|
||||
if c.rdata != nil {
|
||||
data = c.rdata
|
||||
} else {
|
||||
data = <-c.readCh
|
||||
}
|
||||
|
||||
if data == nil {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
if len(data) > len(b) {
|
||||
copy(b, data[:len(b)])
|
||||
c.rdata = data[len(b):]
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
c.rdata = nil
|
||||
copy(b, data)
|
||||
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
func (c *conn) LocalAddr() net.Addr {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) RemoteAddr() net.Addr {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) SetDeadline(t time.Time) error {
|
||||
return errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (c *conn) SetReadDeadline(t time.Time) error {
|
||||
return errors.New("not implemented")
|
||||
}
|
||||
|
||||
func (c *conn) SetWriteDeadline(t time.Time) error {
|
||||
return errors.New("not implemented")
|
||||
}
|
||||
|
||||
// Close closes the connection. It also sends CLOSE_REQ packet over
|
||||
// proxy service to notify remote to drop the connection.
|
||||
func (c *conn) Close() error {
|
||||
klog.Info("conn.Close()")
|
||||
req := &client.Packet{
|
||||
Type: client.PacketType_CLOSE_REQ,
|
||||
Payload: &client.Packet_CloseRequest{
|
||||
CloseRequest: &client.CloseRequest{
|
||||
ConnectID: c.connID,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
klog.V(6).Infof("[tracing] send req, type: %s", req.Type)
|
||||
|
||||
if err := c.stream.Send(req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
select {
|
||||
case errMsg := <-c.closeCh:
|
||||
if errMsg != "" {
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
return nil
|
||||
case <-time.After(CloseTimeout):
|
||||
}
|
||||
|
||||
return errors.New("close timeout")
|
||||
}
|
||||
653
vendor/sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client/client.pb.go
generated
vendored
Normal file
653
vendor/sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client/client.pb.go
generated
vendored
Normal file
|
|
@ -0,0 +1,653 @@
|
|||
/*
|
||||
Copyright The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: konnectivity-client/proto/client/client.proto
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
context "context"
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
math "math"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type PacketType int32
|
||||
|
||||
const (
|
||||
PacketType_DIAL_REQ PacketType = 0
|
||||
PacketType_DIAL_RSP PacketType = 1
|
||||
PacketType_CLOSE_REQ PacketType = 2
|
||||
PacketType_CLOSE_RSP PacketType = 3
|
||||
PacketType_DATA PacketType = 4
|
||||
)
|
||||
|
||||
var PacketType_name = map[int32]string{
|
||||
0: "DIAL_REQ",
|
||||
1: "DIAL_RSP",
|
||||
2: "CLOSE_REQ",
|
||||
3: "CLOSE_RSP",
|
||||
4: "DATA",
|
||||
}
|
||||
|
||||
var PacketType_value = map[string]int32{
|
||||
"DIAL_REQ": 0,
|
||||
"DIAL_RSP": 1,
|
||||
"CLOSE_REQ": 2,
|
||||
"CLOSE_RSP": 3,
|
||||
"DATA": 4,
|
||||
}
|
||||
|
||||
func (x PacketType) String() string {
|
||||
return proto.EnumName(PacketType_name, int32(x))
|
||||
}
|
||||
|
||||
func (PacketType) EnumDescriptor() ([]byte, []int) {
|
||||
return fileDescriptor_fec4258d9ecd175d, []int{0}
|
||||
}
|
||||
|
||||
type Error int32
|
||||
|
||||
const (
|
||||
Error_EOF Error = 0
|
||||
)
|
||||
|
||||
var Error_name = map[int32]string{
|
||||
0: "EOF",
|
||||
}
|
||||
|
||||
var Error_value = map[string]int32{
|
||||
"EOF": 0,
|
||||
}
|
||||
|
||||
func (x Error) String() string {
|
||||
return proto.EnumName(Error_name, int32(x))
|
||||
}
|
||||
|
||||
func (Error) EnumDescriptor() ([]byte, []int) {
|
||||
return fileDescriptor_fec4258d9ecd175d, []int{1}
|
||||
}
|
||||
|
||||
type Packet struct {
|
||||
Type PacketType `protobuf:"varint,1,opt,name=type,proto3,enum=PacketType" json:"type,omitempty"`
|
||||
// Types that are valid to be assigned to Payload:
|
||||
// *Packet_DialRequest
|
||||
// *Packet_DialResponse
|
||||
// *Packet_Data
|
||||
// *Packet_CloseRequest
|
||||
// *Packet_CloseResponse
|
||||
Payload isPacket_Payload `protobuf_oneof:"payload"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Packet) Reset() { *m = Packet{} }
|
||||
func (m *Packet) String() string { return proto.CompactTextString(m) }
|
||||
func (*Packet) ProtoMessage() {}
|
||||
func (*Packet) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_fec4258d9ecd175d, []int{0}
|
||||
}
|
||||
|
||||
func (m *Packet) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Packet.Unmarshal(m, b)
|
||||
}
|
||||
func (m *Packet) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_Packet.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *Packet) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_Packet.Merge(m, src)
|
||||
}
|
||||
func (m *Packet) XXX_Size() int {
|
||||
return xxx_messageInfo_Packet.Size(m)
|
||||
}
|
||||
func (m *Packet) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_Packet.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_Packet proto.InternalMessageInfo
|
||||
|
||||
func (m *Packet) GetType() PacketType {
|
||||
if m != nil {
|
||||
return m.Type
|
||||
}
|
||||
return PacketType_DIAL_REQ
|
||||
}
|
||||
|
||||
type isPacket_Payload interface {
|
||||
isPacket_Payload()
|
||||
}
|
||||
|
||||
type Packet_DialRequest struct {
|
||||
DialRequest *DialRequest `protobuf:"bytes,2,opt,name=dialRequest,proto3,oneof"`
|
||||
}
|
||||
|
||||
type Packet_DialResponse struct {
|
||||
DialResponse *DialResponse `protobuf:"bytes,3,opt,name=dialResponse,proto3,oneof"`
|
||||
}
|
||||
|
||||
type Packet_Data struct {
|
||||
Data *Data `protobuf:"bytes,4,opt,name=data,proto3,oneof"`
|
||||
}
|
||||
|
||||
type Packet_CloseRequest struct {
|
||||
CloseRequest *CloseRequest `protobuf:"bytes,5,opt,name=closeRequest,proto3,oneof"`
|
||||
}
|
||||
|
||||
type Packet_CloseResponse struct {
|
||||
CloseResponse *CloseResponse `protobuf:"bytes,6,opt,name=closeResponse,proto3,oneof"`
|
||||
}
|
||||
|
||||
func (*Packet_DialRequest) isPacket_Payload() {}
|
||||
|
||||
func (*Packet_DialResponse) isPacket_Payload() {}
|
||||
|
||||
func (*Packet_Data) isPacket_Payload() {}
|
||||
|
||||
func (*Packet_CloseRequest) isPacket_Payload() {}
|
||||
|
||||
func (*Packet_CloseResponse) isPacket_Payload() {}
|
||||
|
||||
func (m *Packet) GetPayload() isPacket_Payload {
|
||||
if m != nil {
|
||||
return m.Payload
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Packet) GetDialRequest() *DialRequest {
|
||||
if x, ok := m.GetPayload().(*Packet_DialRequest); ok {
|
||||
return x.DialRequest
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Packet) GetDialResponse() *DialResponse {
|
||||
if x, ok := m.GetPayload().(*Packet_DialResponse); ok {
|
||||
return x.DialResponse
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Packet) GetData() *Data {
|
||||
if x, ok := m.GetPayload().(*Packet_Data); ok {
|
||||
return x.Data
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Packet) GetCloseRequest() *CloseRequest {
|
||||
if x, ok := m.GetPayload().(*Packet_CloseRequest); ok {
|
||||
return x.CloseRequest
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Packet) GetCloseResponse() *CloseResponse {
|
||||
if x, ok := m.GetPayload().(*Packet_CloseResponse); ok {
|
||||
return x.CloseResponse
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// XXX_OneofWrappers is for the internal use of the proto package.
|
||||
func (*Packet) XXX_OneofWrappers() []interface{} {
|
||||
return []interface{}{
|
||||
(*Packet_DialRequest)(nil),
|
||||
(*Packet_DialResponse)(nil),
|
||||
(*Packet_Data)(nil),
|
||||
(*Packet_CloseRequest)(nil),
|
||||
(*Packet_CloseResponse)(nil),
|
||||
}
|
||||
}
|
||||
|
||||
type DialRequest struct {
|
||||
// tcp or udp?
|
||||
Protocol string `protobuf:"bytes,1,opt,name=protocol,proto3" json:"protocol,omitempty"`
|
||||
// node:port
|
||||
Address string `protobuf:"bytes,2,opt,name=address,proto3" json:"address,omitempty"`
|
||||
// random id for client, maybe should be longer
|
||||
Random int64 `protobuf:"varint,3,opt,name=random,proto3" json:"random,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *DialRequest) Reset() { *m = DialRequest{} }
|
||||
func (m *DialRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*DialRequest) ProtoMessage() {}
|
||||
func (*DialRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_fec4258d9ecd175d, []int{1}
|
||||
}
|
||||
|
||||
func (m *DialRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_DialRequest.Unmarshal(m, b)
|
||||
}
|
||||
func (m *DialRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_DialRequest.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *DialRequest) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_DialRequest.Merge(m, src)
|
||||
}
|
||||
func (m *DialRequest) XXX_Size() int {
|
||||
return xxx_messageInfo_DialRequest.Size(m)
|
||||
}
|
||||
func (m *DialRequest) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_DialRequest.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_DialRequest proto.InternalMessageInfo
|
||||
|
||||
func (m *DialRequest) GetProtocol() string {
|
||||
if m != nil {
|
||||
return m.Protocol
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *DialRequest) GetAddress() string {
|
||||
if m != nil {
|
||||
return m.Address
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *DialRequest) GetRandom() int64 {
|
||||
if m != nil {
|
||||
return m.Random
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type DialResponse struct {
|
||||
// error failed reason; enum?
|
||||
Error string `protobuf:"bytes,1,opt,name=error,proto3" json:"error,omitempty"`
|
||||
// connectID indicates the identifier of the connection
|
||||
ConnectID int64 `protobuf:"varint,2,opt,name=connectID,proto3" json:"connectID,omitempty"`
|
||||
// random copied from DialRequest
|
||||
Random int64 `protobuf:"varint,3,opt,name=random,proto3" json:"random,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *DialResponse) Reset() { *m = DialResponse{} }
|
||||
func (m *DialResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*DialResponse) ProtoMessage() {}
|
||||
func (*DialResponse) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_fec4258d9ecd175d, []int{2}
|
||||
}
|
||||
|
||||
func (m *DialResponse) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_DialResponse.Unmarshal(m, b)
|
||||
}
|
||||
func (m *DialResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_DialResponse.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *DialResponse) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_DialResponse.Merge(m, src)
|
||||
}
|
||||
func (m *DialResponse) XXX_Size() int {
|
||||
return xxx_messageInfo_DialResponse.Size(m)
|
||||
}
|
||||
func (m *DialResponse) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_DialResponse.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_DialResponse proto.InternalMessageInfo
|
||||
|
||||
func (m *DialResponse) GetError() string {
|
||||
if m != nil {
|
||||
return m.Error
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *DialResponse) GetConnectID() int64 {
|
||||
if m != nil {
|
||||
return m.ConnectID
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *DialResponse) GetRandom() int64 {
|
||||
if m != nil {
|
||||
return m.Random
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type CloseRequest struct {
|
||||
// connectID of the stream to close
|
||||
ConnectID int64 `protobuf:"varint,1,opt,name=connectID,proto3" json:"connectID,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *CloseRequest) Reset() { *m = CloseRequest{} }
|
||||
func (m *CloseRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*CloseRequest) ProtoMessage() {}
|
||||
func (*CloseRequest) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_fec4258d9ecd175d, []int{3}
|
||||
}
|
||||
|
||||
func (m *CloseRequest) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_CloseRequest.Unmarshal(m, b)
|
||||
}
|
||||
func (m *CloseRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_CloseRequest.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *CloseRequest) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_CloseRequest.Merge(m, src)
|
||||
}
|
||||
func (m *CloseRequest) XXX_Size() int {
|
||||
return xxx_messageInfo_CloseRequest.Size(m)
|
||||
}
|
||||
func (m *CloseRequest) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_CloseRequest.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_CloseRequest proto.InternalMessageInfo
|
||||
|
||||
func (m *CloseRequest) GetConnectID() int64 {
|
||||
if m != nil {
|
||||
return m.ConnectID
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type CloseResponse struct {
|
||||
// error message
|
||||
Error string `protobuf:"bytes,1,opt,name=error,proto3" json:"error,omitempty"`
|
||||
// connectID indicates the identifier of the connection
|
||||
ConnectID int64 `protobuf:"varint,2,opt,name=connectID,proto3" json:"connectID,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *CloseResponse) Reset() { *m = CloseResponse{} }
|
||||
func (m *CloseResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*CloseResponse) ProtoMessage() {}
|
||||
func (*CloseResponse) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_fec4258d9ecd175d, []int{4}
|
||||
}
|
||||
|
||||
func (m *CloseResponse) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_CloseResponse.Unmarshal(m, b)
|
||||
}
|
||||
func (m *CloseResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_CloseResponse.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *CloseResponse) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_CloseResponse.Merge(m, src)
|
||||
}
|
||||
func (m *CloseResponse) XXX_Size() int {
|
||||
return xxx_messageInfo_CloseResponse.Size(m)
|
||||
}
|
||||
func (m *CloseResponse) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_CloseResponse.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_CloseResponse proto.InternalMessageInfo
|
||||
|
||||
func (m *CloseResponse) GetError() string {
|
||||
if m != nil {
|
||||
return m.Error
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *CloseResponse) GetConnectID() int64 {
|
||||
if m != nil {
|
||||
return m.ConnectID
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
// connectID to connect to
|
||||
ConnectID int64 `protobuf:"varint,1,opt,name=connectID,proto3" json:"connectID,omitempty"`
|
||||
// error message if error happens
|
||||
Error string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
|
||||
// stream data
|
||||
Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Data) Reset() { *m = Data{} }
|
||||
func (m *Data) String() string { return proto.CompactTextString(m) }
|
||||
func (*Data) ProtoMessage() {}
|
||||
func (*Data) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_fec4258d9ecd175d, []int{5}
|
||||
}
|
||||
|
||||
func (m *Data) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Data.Unmarshal(m, b)
|
||||
}
|
||||
func (m *Data) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_Data.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *Data) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_Data.Merge(m, src)
|
||||
}
|
||||
func (m *Data) XXX_Size() int {
|
||||
return xxx_messageInfo_Data.Size(m)
|
||||
}
|
||||
func (m *Data) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_Data.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_Data proto.InternalMessageInfo
|
||||
|
||||
func (m *Data) GetConnectID() int64 {
|
||||
if m != nil {
|
||||
return m.ConnectID
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *Data) GetError() string {
|
||||
if m != nil {
|
||||
return m.Error
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Data) GetData() []byte {
|
||||
if m != nil {
|
||||
return m.Data
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterEnum("PacketType", PacketType_name, PacketType_value)
|
||||
proto.RegisterEnum("Error", Error_name, Error_value)
|
||||
proto.RegisterType((*Packet)(nil), "Packet")
|
||||
proto.RegisterType((*DialRequest)(nil), "DialRequest")
|
||||
proto.RegisterType((*DialResponse)(nil), "DialResponse")
|
||||
proto.RegisterType((*CloseRequest)(nil), "CloseRequest")
|
||||
proto.RegisterType((*CloseResponse)(nil), "CloseResponse")
|
||||
proto.RegisterType((*Data)(nil), "Data")
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterFile("konnectivity-client/proto/client/client.proto", fileDescriptor_fec4258d9ecd175d)
|
||||
}
|
||||
|
||||
var fileDescriptor_fec4258d9ecd175d = []byte{
|
||||
// 472 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x53, 0xd1, 0x6e, 0x9b, 0x30,
|
||||
0x14, 0x85, 0x00, 0x49, 0xb8, 0x21, 0x15, 0xb2, 0xa6, 0x09, 0x75, 0x93, 0x5a, 0xf1, 0x14, 0x55,
|
||||
0x0b, 0x54, 0xa9, 0x34, 0xed, 0x35, 0x0d, 0xa9, 0x52, 0xa9, 0x5a, 0x99, 0xd3, 0xa7, 0xee, 0x61,
|
||||
0xf2, 0xc0, 0x9a, 0x50, 0x18, 0x66, 0xb6, 0x97, 0x8d, 0x0f, 0xda, 0x7f, 0x4e, 0x18, 0x52, 0xc8,
|
||||
0xa4, 0x6d, 0x52, 0x9f, 0xe0, 0x1c, 0xdf, 0x73, 0x7c, 0x7d, 0xae, 0x0d, 0xf3, 0x1d, 0x2b, 0x0a,
|
||||
0x9a, 0xc8, 0x6c, 0x9f, 0xc9, 0x6a, 0x9e, 0xe4, 0x19, 0x2d, 0x64, 0x58, 0x72, 0x26, 0x59, 0xd8,
|
||||
0x82, 0xe6, 0x13, 0x28, 0xce, 0xff, 0x35, 0x80, 0x61, 0x4c, 0x92, 0x1d, 0x95, 0xe8, 0x0c, 0x4c,
|
||||
0x59, 0x95, 0xd4, 0xd3, 0xcf, 0xf5, 0xd9, 0xc9, 0x62, 0x12, 0x34, 0xf4, 0x43, 0x55, 0x52, 0xac,
|
||||
0x16, 0xd0, 0x25, 0x4c, 0xd2, 0x8c, 0xe4, 0x98, 0x7e, 0xfb, 0x4e, 0x85, 0xf4, 0x06, 0xe7, 0xfa,
|
||||
0x6c, 0xb2, 0x70, 0x82, 0xa8, 0xe3, 0x36, 0x1a, 0xee, 0x97, 0xa0, 0x2b, 0x70, 0x1a, 0x28, 0x4a,
|
||||
0x56, 0x08, 0xea, 0x19, 0x4a, 0x32, 0x6d, 0x25, 0x0d, 0xb9, 0xd1, 0xf0, 0x51, 0x11, 0x7a, 0x05,
|
||||
0x66, 0x4a, 0x24, 0xf1, 0x4c, 0x55, 0x6c, 0x05, 0x11, 0x91, 0x64, 0xa3, 0x61, 0x45, 0xd6, 0x8e,
|
||||
0x49, 0xce, 0x04, 0x3d, 0x34, 0x61, 0xb5, 0x8e, 0xab, 0x1e, 0x59, 0x3b, 0xf6, 0x8b, 0xd0, 0x5b,
|
||||
0x98, 0xb6, 0xb8, 0xed, 0x63, 0xa8, 0x54, 0x27, 0x07, 0xd5, 0x53, 0x23, 0xc7, 0x65, 0xd7, 0x36,
|
||||
0x8c, 0x4a, 0x52, 0xe5, 0x8c, 0xa4, 0xfe, 0x47, 0x98, 0xf4, 0xce, 0x89, 0x4e, 0x61, 0xac, 0xf2,
|
||||
0x4b, 0x58, 0xae, 0xf2, 0xb2, 0xf1, 0x13, 0x46, 0x1e, 0x8c, 0x48, 0x9a, 0x72, 0x2a, 0x84, 0x8a,
|
||||
0xc8, 0xc6, 0x07, 0x88, 0x5e, 0xc2, 0x90, 0x93, 0x22, 0x65, 0x5f, 0x55, 0x10, 0x06, 0x6e, 0x91,
|
||||
0xff, 0x08, 0x4e, 0x3f, 0x11, 0xf4, 0x02, 0x2c, 0xca, 0x39, 0xe3, 0xad, 0x75, 0x03, 0xd0, 0x6b,
|
||||
0xb0, 0x93, 0x66, 0xb6, 0xb7, 0x91, 0x72, 0x36, 0x70, 0x47, 0xfc, 0xd5, 0xfb, 0x0d, 0x38, 0xfd,
|
||||
0x6c, 0x8e, 0x5d, 0xf4, 0x3f, 0x5c, 0xfc, 0x15, 0x4c, 0x8f, 0x32, 0x79, 0x4e, 0x2b, 0xfe, 0x7b,
|
||||
0x30, 0xeb, 0x99, 0xfd, 0x7b, 0xab, 0xce, 0x79, 0xd0, 0x77, 0x46, 0xed, 0xf0, 0xeb, 0x43, 0x38,
|
||||
0xcd, 0xcc, 0x2f, 0x62, 0x80, 0xee, 0x2e, 0x22, 0x07, 0xc6, 0xd1, 0xed, 0xf2, 0xee, 0x13, 0x5e,
|
||||
0x7f, 0x70, 0xb5, 0x0e, 0x6d, 0x63, 0x57, 0x47, 0x53, 0xb0, 0x57, 0x77, 0xf7, 0xdb, 0xb5, 0x5a,
|
||||
0x1c, 0xf4, 0xe0, 0x36, 0x76, 0x0d, 0x34, 0x06, 0x33, 0x5a, 0x3e, 0x2c, 0x5d, 0xf3, 0xc2, 0x05,
|
||||
0x6b, 0xad, 0xb6, 0x1b, 0x81, 0xb1, 0xbe, 0xbf, 0x71, 0xb5, 0x45, 0x08, 0x4e, 0xcc, 0xd9, 0xcf,
|
||||
0x6a, 0x4b, 0xf9, 0x3e, 0x4b, 0x28, 0x3a, 0x03, 0x4b, 0x61, 0x34, 0x6a, 0xdf, 0xc1, 0xe9, 0xe1,
|
||||
0xc7, 0xd7, 0x66, 0xfa, 0xa5, 0x7e, 0x7d, 0xf3, 0x18, 0x89, 0xec, 0x8b, 0x08, 0x76, 0xef, 0x44,
|
||||
0x90, 0xb1, 0x90, 0x94, 0x99, 0xa0, 0x7c, 0x4f, 0xf9, 0xbc, 0xa0, 0xf2, 0x07, 0xe3, 0xbb, 0x79,
|
||||
0x59, 0xcb, 0xc3, 0xff, 0xbd, 0xc6, 0xcf, 0x43, 0x85, 0xae, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff,
|
||||
0x64, 0xe0, 0x62, 0xbe, 0xb8, 0x03, 0x00, 0x00,
|
||||
}
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ context.Context
|
||||
var _ grpc.ClientConn
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion4
|
||||
|
||||
// ProxyServiceClient is the client API for ProxyService service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
|
||||
type ProxyServiceClient interface {
|
||||
Proxy(ctx context.Context, opts ...grpc.CallOption) (ProxyService_ProxyClient, error)
|
||||
}
|
||||
|
||||
type proxyServiceClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewProxyServiceClient(cc *grpc.ClientConn) ProxyServiceClient {
|
||||
return &proxyServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *proxyServiceClient) Proxy(ctx context.Context, opts ...grpc.CallOption) (ProxyService_ProxyClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &_ProxyService_serviceDesc.Streams[0], "/ProxyService/Proxy", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &proxyServiceProxyClient{stream}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type ProxyService_ProxyClient interface {
|
||||
Send(*Packet) error
|
||||
Recv() (*Packet, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type proxyServiceProxyClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *proxyServiceProxyClient) Send(m *Packet) error {
|
||||
return x.ClientStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *proxyServiceProxyClient) Recv() (*Packet, error) {
|
||||
m := new(Packet)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// ProxyServiceServer is the server API for ProxyService service.
|
||||
type ProxyServiceServer interface {
|
||||
Proxy(ProxyService_ProxyServer) error
|
||||
}
|
||||
|
||||
// UnimplementedProxyServiceServer can be embedded to have forward compatible implementations.
|
||||
type UnimplementedProxyServiceServer struct {
|
||||
}
|
||||
|
||||
func (*UnimplementedProxyServiceServer) Proxy(srv ProxyService_ProxyServer) error {
|
||||
return status.Errorf(codes.Unimplemented, "method Proxy not implemented")
|
||||
}
|
||||
|
||||
func RegisterProxyServiceServer(s *grpc.Server, srv ProxyServiceServer) {
|
||||
s.RegisterService(&_ProxyService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _ProxyService_Proxy_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
return srv.(ProxyServiceServer).Proxy(&proxyServiceProxyServer{stream})
|
||||
}
|
||||
|
||||
type ProxyService_ProxyServer interface {
|
||||
Send(*Packet) error
|
||||
Recv() (*Packet, error)
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type proxyServiceProxyServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *proxyServiceProxyServer) Send(m *Packet) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *proxyServiceProxyServer) Recv() (*Packet, error) {
|
||||
m := new(Packet)
|
||||
if err := x.ServerStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
var _ProxyService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "ProxyService",
|
||||
HandlerType: (*ProxyServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{},
|
||||
Streams: []grpc.StreamDesc{
|
||||
{
|
||||
StreamName: "Proxy",
|
||||
Handler: _ProxyService_Proxy_Handler,
|
||||
ServerStreams: true,
|
||||
ClientStreams: true,
|
||||
},
|
||||
},
|
||||
Metadata: "konnectivity-client/proto/client/client.proto",
|
||||
}
|
||||
95
vendor/sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client/client.proto
generated
vendored
Normal file
95
vendor/sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client/client.proto
generated
vendored
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
// Copyright The Kubernetes Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
// Retransmit?
|
||||
// Sliding windows?
|
||||
|
||||
option go_package = "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client";
|
||||
|
||||
service ProxyService {
|
||||
rpc Proxy(stream Packet) returns (stream Packet) {}
|
||||
}
|
||||
|
||||
enum PacketType {
|
||||
DIAL_REQ = 0;
|
||||
DIAL_RSP = 1;
|
||||
CLOSE_REQ = 2;
|
||||
CLOSE_RSP = 3;
|
||||
DATA = 4;
|
||||
}
|
||||
|
||||
enum Error {
|
||||
EOF = 0;
|
||||
// ...
|
||||
}
|
||||
|
||||
message Packet {
|
||||
PacketType type = 1;
|
||||
|
||||
oneof payload {
|
||||
DialRequest dialRequest = 2;
|
||||
DialResponse dialResponse = 3;
|
||||
Data data = 4;
|
||||
CloseRequest closeRequest = 5;
|
||||
CloseResponse closeResponse = 6;
|
||||
}
|
||||
}
|
||||
|
||||
message DialRequest {
|
||||
// tcp or udp?
|
||||
string protocol = 1;
|
||||
|
||||
// node:port
|
||||
string address = 2;
|
||||
|
||||
// random id for client, maybe should be longer
|
||||
int64 random = 3;
|
||||
}
|
||||
|
||||
message DialResponse {
|
||||
// error failed reason; enum?
|
||||
string error = 1;
|
||||
|
||||
// connectID indicates the identifier of the connection
|
||||
int64 connectID = 2;
|
||||
|
||||
// random copied from DialRequest
|
||||
int64 random = 3;
|
||||
}
|
||||
|
||||
message CloseRequest {
|
||||
// connectID of the stream to close
|
||||
int64 connectID = 1;
|
||||
}
|
||||
|
||||
message CloseResponse {
|
||||
// error message
|
||||
string error = 1;
|
||||
|
||||
// connectID indicates the identifier of the connection
|
||||
int64 connectID = 2;
|
||||
}
|
||||
|
||||
message Data {
|
||||
// connectID to connect to
|
||||
int64 connectID = 1;
|
||||
|
||||
// error message if error happens
|
||||
string error = 2;
|
||||
|
||||
// stream data
|
||||
bytes data = 3;
|
||||
}
|
||||
4
vendor/sigs.k8s.io/metrics-server/pkg/api/interfaces.go
generated
vendored
4
vendor/sigs.k8s.io/metrics-server/pkg/api/interfaces.go
generated
vendored
|
|
@ -51,7 +51,7 @@ type PodMetricsGetter interface {
|
|||
// GetContainerMetrics gets the latest metrics for all containers in each listed pod,
|
||||
// returning both the metrics and the associated collection timestamp.
|
||||
// If a pod is missing, the container metrics should be nil for that pod.
|
||||
GetContainerMetrics(pods ...apitypes.NamespacedName) ([]TimeInfo, [][]metrics.ContainerMetrics, error)
|
||||
GetContainerMetrics(pods ...apitypes.NamespacedName) ([]TimeInfo, [][]metrics.ContainerMetrics)
|
||||
}
|
||||
|
||||
// NodeMetricsGetter knows how to fetch metrics for a node.
|
||||
|
|
@ -59,5 +59,5 @@ type NodeMetricsGetter interface {
|
|||
// GetNodeMetrics gets the latest metrics for the given nodes,
|
||||
// returning both the metrics and the associated collection timestamp.
|
||||
// If a node is missing, the resourcelist should be nil for that node.
|
||||
GetNodeMetrics(nodes ...string) ([]TimeInfo, []corev1.ResourceList, error)
|
||||
GetNodeMetrics(nodes ...string) ([]TimeInfo, []corev1.ResourceList)
|
||||
}
|
||||
|
|
|
|||
35
vendor/sigs.k8s.io/metrics-server/pkg/api/node.go
generated
vendored
35
vendor/sigs.k8s.io/metrics-server/pkg/api/node.go
generated
vendored
|
|
@ -76,25 +76,30 @@ func (m *nodeMetrics) NewList() runtime.Object {
|
|||
// Lister interface
|
||||
func (m *nodeMetrics) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
|
||||
labelSelector := labels.Everything()
|
||||
fieldSelector := fields.Everything()
|
||||
if options != nil && options.LabelSelector != nil {
|
||||
labelSelector = options.LabelSelector
|
||||
}
|
||||
if options != nil && options.FieldSelector != nil {
|
||||
fieldSelector = options.FieldSelector
|
||||
}
|
||||
nodes, err := m.nodeLister.ListWithPredicate(func(node *v1.Node) bool {
|
||||
if labelSelector.Empty() && fieldSelector.Empty() {
|
||||
return true
|
||||
}
|
||||
fieldsSet := generic.AddObjectMetaFieldsSet(make(fields.Set, 2), &node.ObjectMeta, true)
|
||||
return labelSelector.Matches(labels.Set(node.Labels)) && fieldSelector.Matches(fieldsSet)
|
||||
})
|
||||
nodes, err := m.nodeLister.List(labelSelector)
|
||||
if err != nil {
|
||||
errMsg := fmt.Errorf("Error while listing nodes for selector %v: %v", labelSelector, err)
|
||||
klog.Error(errMsg)
|
||||
return &metrics.NodeMetricsList{}, errMsg
|
||||
}
|
||||
if options != nil && options.FieldSelector != nil {
|
||||
newNodes := make([]*v1.Node, 0, len(nodes))
|
||||
fields := make(fields.Set, 2)
|
||||
for _, node := range nodes {
|
||||
for k := range fields {
|
||||
delete(fields, k)
|
||||
}
|
||||
fieldsSet := generic.AddObjectMetaFieldsSet(fields, &node.ObjectMeta, true)
|
||||
if !options.FieldSelector.Matches(fieldsSet) {
|
||||
continue
|
||||
}
|
||||
newNodes = append(newNodes, node)
|
||||
}
|
||||
nodes = newNodes
|
||||
}
|
||||
|
||||
names := make([]string, len(nodes))
|
||||
for i, node := range nodes {
|
||||
|
|
@ -185,17 +190,11 @@ func addNodeMetricsToTable(table *metav1beta1.Table, nodes ...metrics.NodeMetric
|
|||
}
|
||||
|
||||
func (m *nodeMetrics) getNodeMetrics(names ...string) ([]metrics.NodeMetrics, error) {
|
||||
timestamps, usages, err := m.metrics.GetNodeMetrics(names...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
timestamps, usages := m.metrics.GetNodeMetrics(names...)
|
||||
res := make([]metrics.NodeMetrics, 0, len(names))
|
||||
|
||||
for i, name := range names {
|
||||
if usages[i] == nil {
|
||||
klog.Errorf("unable to fetch node metrics for node %q: no metrics known for node", name)
|
||||
|
||||
continue
|
||||
}
|
||||
res = append(res, metrics.NodeMetrics{
|
||||
|
|
|
|||
7
vendor/sigs.k8s.io/metrics-server/pkg/api/pod.go
generated
vendored
7
vendor/sigs.k8s.io/metrics-server/pkg/api/pod.go
generated
vendored
|
|
@ -230,11 +230,7 @@ func (m *podMetrics) getPodMetrics(pods ...*v1.Pod) ([]metrics.PodMetrics, error
|
|||
Namespace: pod.Namespace,
|
||||
}
|
||||
}
|
||||
timestamps, containerMetrics, err := m.metrics.GetContainerMetrics(namespacedNames...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
timestamps, containerMetrics := m.metrics.GetContainerMetrics(namespacedNames...)
|
||||
res := make([]metrics.PodMetrics, 0, len(pods))
|
||||
|
||||
for i, pod := range pods {
|
||||
|
|
@ -243,7 +239,6 @@ func (m *podMetrics) getPodMetrics(pods ...*v1.Pod) ([]metrics.PodMetrics, error
|
|||
continue
|
||||
}
|
||||
if containerMetrics[i] == nil {
|
||||
klog.Errorf("unable to fetch pod metrics for pod %s/%s: no metrics known for pod", pod.Namespace, pod.Name)
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
|||
112
vendor/sigs.k8s.io/structured-merge-diff/typed/remove.go
generated
vendored
112
vendor/sigs.k8s.io/structured-merge-diff/typed/remove.go
generated
vendored
|
|
@ -1,112 +0,0 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package typed
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
type removingWalker struct {
|
||||
value *value.Value
|
||||
schema *schema.Schema
|
||||
toRemove *fieldpath.Set
|
||||
}
|
||||
|
||||
func removeItemsWithSchema(value *value.Value, toRemove *fieldpath.Set, schema *schema.Schema, typeRef schema.TypeRef) {
|
||||
w := &removingWalker{
|
||||
value: value,
|
||||
schema: schema,
|
||||
toRemove: toRemove,
|
||||
}
|
||||
resolveSchema(schema, typeRef, value, w)
|
||||
}
|
||||
|
||||
// doLeaf should be called on leaves before descending into children, if there
|
||||
// will be a descent. It modifies w.inLeaf.
|
||||
func (w *removingWalker) doLeaf() ValidationErrors { return nil }
|
||||
|
||||
func (w *removingWalker) doScalar(t *schema.Scalar) ValidationErrors { return nil }
|
||||
|
||||
func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
|
||||
l := w.value.ListValue
|
||||
|
||||
// If list is null, empty, or atomic just return
|
||||
if l == nil || len(l.Items) == 0 || t.ElementRelationship == schema.Atomic {
|
||||
return nil
|
||||
}
|
||||
|
||||
newItems := []value.Value{}
|
||||
for i := range l.Items {
|
||||
item := l.Items[i]
|
||||
// Ignore error because we have already validated this list
|
||||
pe, _ := listItemToPathElement(t, i, item)
|
||||
path, _ := fieldpath.MakePath(pe)
|
||||
if w.toRemove.Has(path) {
|
||||
continue
|
||||
}
|
||||
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
|
||||
removeItemsWithSchema(&l.Items[i], subset, w.schema, t.ElementType)
|
||||
}
|
||||
newItems = append(newItems, l.Items[i])
|
||||
}
|
||||
l.Items = newItems
|
||||
if len(l.Items) == 0 {
|
||||
w.value.ListValue = nil
|
||||
w.value.Null = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *removingWalker) doMap(t *schema.Map) ValidationErrors {
|
||||
m := w.value.MapValue
|
||||
|
||||
// If map is null, empty, or atomic just return
|
||||
if m == nil || len(m.Items) == 0 || t.ElementRelationship == schema.Atomic {
|
||||
return nil
|
||||
}
|
||||
|
||||
fieldTypes := map[string]schema.TypeRef{}
|
||||
for _, structField := range t.Fields {
|
||||
fieldTypes[structField.Name] = structField.Type
|
||||
}
|
||||
|
||||
newMap := &value.Map{}
|
||||
for i := range m.Items {
|
||||
item := m.Items[i]
|
||||
pe := fieldpath.PathElement{FieldName: &item.Name}
|
||||
path, _ := fieldpath.MakePath(pe)
|
||||
fieldType := t.ElementType
|
||||
if ft, ok := fieldTypes[item.Name]; ok {
|
||||
fieldType = ft
|
||||
} else {
|
||||
if w.toRemove.Has(path) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
|
||||
removeItemsWithSchema(&m.Items[i].Value, subset, w.schema, fieldType)
|
||||
}
|
||||
newMap.Set(item.Name, m.Items[i].Value)
|
||||
}
|
||||
w.value.MapValue = newMap
|
||||
if len(w.value.MapValue.Items) == 0 {
|
||||
w.value.MapValue = nil
|
||||
w.value.Null = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*removingWalker) errorf(_ string, _ ...interface{}) ValidationErrors { return nil }
|
||||
226
vendor/sigs.k8s.io/structured-merge-diff/typed/validate.go
generated
vendored
226
vendor/sigs.k8s.io/structured-merge-diff/typed/validate.go
generated
vendored
|
|
@ -1,226 +0,0 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package typed
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
)
|
||||
|
||||
var vPool = sync.Pool{
|
||||
New: func() interface{} { return &validatingObjectWalker{} },
|
||||
}
|
||||
|
||||
func (tv TypedValue) walker() *validatingObjectWalker {
|
||||
v := vPool.Get().(*validatingObjectWalker)
|
||||
v.value = tv.value
|
||||
v.schema = tv.schema
|
||||
v.typeRef = tv.typeRef
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) finished() {
|
||||
v.value = value.Value{}
|
||||
v.schema = nil
|
||||
v.typeRef = schema.TypeRef{}
|
||||
v.leafFieldCallback = nil
|
||||
v.nodeFieldCallback = nil
|
||||
v.inLeaf = false
|
||||
vPool.Put(v)
|
||||
}
|
||||
|
||||
type validatingObjectWalker struct {
|
||||
errorFormatter
|
||||
value value.Value
|
||||
schema *schema.Schema
|
||||
typeRef schema.TypeRef
|
||||
|
||||
// If set, this is called on "leaf fields":
|
||||
// * scalars: int/string/float/bool
|
||||
// * atomic maps and lists
|
||||
// * untyped fields
|
||||
leafFieldCallback func(fieldpath.Path)
|
||||
|
||||
// If set, this is called on "node fields":
|
||||
// * list items
|
||||
// * map items
|
||||
nodeFieldCallback func(fieldpath.Path)
|
||||
|
||||
// internal housekeeping--don't set when constructing.
|
||||
inLeaf bool // Set to true if we're in a "big leaf"--atomic map/list
|
||||
|
||||
// Allocate only as many walkers as needed for the depth by storing them here.
|
||||
spareWalkers *[]*validatingObjectWalker
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) prepareDescent(pe fieldpath.PathElement, tr schema.TypeRef) *validatingObjectWalker {
|
||||
if v.spareWalkers == nil {
|
||||
// first descent.
|
||||
v.spareWalkers = &[]*validatingObjectWalker{}
|
||||
}
|
||||
var v2 *validatingObjectWalker
|
||||
if n := len(*v.spareWalkers); n > 0 {
|
||||
v2, *v.spareWalkers = (*v.spareWalkers)[n-1], (*v.spareWalkers)[:n-1]
|
||||
} else {
|
||||
v2 = &validatingObjectWalker{}
|
||||
}
|
||||
*v2 = *v
|
||||
v2.typeRef = tr
|
||||
v2.errorFormatter.descend(pe)
|
||||
return v2
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) finishDescent(v2 *validatingObjectWalker) {
|
||||
// if the descent caused a realloc, ensure that we reuse the buffer
|
||||
// for the next sibling.
|
||||
v.errorFormatter = v2.errorFormatter.parent()
|
||||
*v.spareWalkers = append(*v.spareWalkers, v2)
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) validate() ValidationErrors {
|
||||
return resolveSchema(v.schema, v.typeRef, &v.value, v)
|
||||
}
|
||||
|
||||
// doLeaf should be called on leaves before descending into children, if there
|
||||
// will be a descent. It modifies v.inLeaf.
|
||||
func (v *validatingObjectWalker) doLeaf() {
|
||||
if v.inLeaf {
|
||||
// We're in a "big leaf", an atomic map or list. Ignore
|
||||
// subsequent leaves.
|
||||
return
|
||||
}
|
||||
v.inLeaf = true
|
||||
|
||||
if v.leafFieldCallback != nil {
|
||||
// At the moment, this is only used to build fieldsets; we can
|
||||
// add more than the path in here if needed.
|
||||
v.leafFieldCallback(v.path)
|
||||
}
|
||||
}
|
||||
|
||||
// doNode should be called on nodes after descending into children
|
||||
func (v *validatingObjectWalker) doNode() {
|
||||
if v.inLeaf {
|
||||
// We're in a "big leaf", an atomic map or list. Ignore
|
||||
// subsequent leaves.
|
||||
return
|
||||
}
|
||||
|
||||
if v.nodeFieldCallback != nil {
|
||||
// At the moment, this is only used to build fieldsets; we can
|
||||
// add more than the path in here if needed.
|
||||
v.nodeFieldCallback(v.path)
|
||||
}
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) doScalar(t *schema.Scalar) ValidationErrors {
|
||||
if errs := v.validateScalar(t, &v.value, ""); len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
// All scalars are leaf fields.
|
||||
v.doLeaf()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) visitListItems(t *schema.List, list *value.List) (errs ValidationErrors) {
|
||||
observedKeys := fieldpath.MakePathElementSet(len(list.Items))
|
||||
for i, child := range list.Items {
|
||||
pe, err := listItemToPathElement(t, i, child)
|
||||
if err != nil {
|
||||
errs = append(errs, v.errorf("element %v: %v", i, err.Error())...)
|
||||
// If we can't construct the path element, we can't
|
||||
// even report errors deeper in the schema, so bail on
|
||||
// this element.
|
||||
continue
|
||||
}
|
||||
if observedKeys.Has(pe) {
|
||||
errs = append(errs, v.errorf("duplicate entries for key %v", pe.String())...)
|
||||
}
|
||||
observedKeys.Insert(pe)
|
||||
v2 := v.prepareDescent(pe, t.ElementType)
|
||||
v2.value = child
|
||||
errs = append(errs, v2.validate()...)
|
||||
|
||||
v2.doNode()
|
||||
v.finishDescent(v2)
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) doList(t *schema.List) (errs ValidationErrors) {
|
||||
list, err := listValue(v.value)
|
||||
if err != nil {
|
||||
return v.error(err)
|
||||
}
|
||||
|
||||
if t.ElementRelationship == schema.Atomic {
|
||||
v.doLeaf()
|
||||
}
|
||||
|
||||
if list == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
errs = v.visitListItems(t, list)
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) visitMapItems(t *schema.Map, m *value.Map) (errs ValidationErrors) {
|
||||
for i := range m.Items {
|
||||
item := &m.Items[i]
|
||||
pe := fieldpath.PathElement{FieldName: &item.Name}
|
||||
|
||||
if sf, ok := t.FindField(item.Name); ok {
|
||||
v2 := v.prepareDescent(pe, sf.Type)
|
||||
v2.value = item.Value
|
||||
errs = append(errs, v2.validate()...)
|
||||
v.finishDescent(v2)
|
||||
} else {
|
||||
v2 := v.prepareDescent(pe, t.ElementType)
|
||||
v2.value = item.Value
|
||||
errs = append(errs, v2.validate()...)
|
||||
v2.doNode()
|
||||
v.finishDescent(v2)
|
||||
}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) doMap(t *schema.Map) (errs ValidationErrors) {
|
||||
m, err := mapValue(v.value)
|
||||
if err != nil {
|
||||
return v.error(err)
|
||||
}
|
||||
|
||||
if t.ElementRelationship == schema.Atomic {
|
||||
v.doLeaf()
|
||||
}
|
||||
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
errs = v.visitMapItems(t, m)
|
||||
|
||||
return errs
|
||||
}
|
||||
201
vendor/sigs.k8s.io/structured-merge-diff/v3/LICENSE
generated
vendored
Normal file
201
vendor/sigs.k8s.io/structured-merge-diff/v3/LICENSE
generated
vendored
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
|
@ -21,7 +21,7 @@ import (
|
|||
"sort"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
// PathElement describes how to select a child field given a containing object.
|
||||
|
|
@ -34,7 +34,7 @@ type PathElement struct {
|
|||
|
||||
// Key selects the list element which has fields matching those given.
|
||||
// The containing object must be an associative list with map typed
|
||||
// elements.
|
||||
// elements. They are sorted alphabetically.
|
||||
Key *value.FieldList
|
||||
|
||||
// Value selects the list element with the given value. The containing
|
||||
|
|
@ -49,50 +49,90 @@ type PathElement struct {
|
|||
|
||||
// Less provides an order for path elements.
|
||||
func (e PathElement) Less(rhs PathElement) bool {
|
||||
return e.Compare(rhs) < 0
|
||||
}
|
||||
|
||||
// Compare provides an order for path elements.
|
||||
func (e PathElement) Compare(rhs PathElement) int {
|
||||
if e.FieldName != nil {
|
||||
if rhs.FieldName == nil {
|
||||
return true
|
||||
return -1
|
||||
}
|
||||
return *e.FieldName < *rhs.FieldName
|
||||
return strings.Compare(*e.FieldName, *rhs.FieldName)
|
||||
} else if rhs.FieldName != nil {
|
||||
return false
|
||||
return 1
|
||||
}
|
||||
|
||||
if e.Key != nil {
|
||||
if rhs.Key == nil {
|
||||
return true
|
||||
return -1
|
||||
}
|
||||
return e.Key.Less(*rhs.Key)
|
||||
return e.Key.Compare(*rhs.Key)
|
||||
} else if rhs.Key != nil {
|
||||
return false
|
||||
return 1
|
||||
}
|
||||
|
||||
if e.Value != nil {
|
||||
if rhs.Value == nil {
|
||||
return true
|
||||
return -1
|
||||
}
|
||||
return e.Value.Less(*rhs.Value)
|
||||
return value.Compare(*e.Value, *rhs.Value)
|
||||
} else if rhs.Value != nil {
|
||||
return false
|
||||
return 1
|
||||
}
|
||||
|
||||
if e.Index != nil {
|
||||
if rhs.Index == nil {
|
||||
return true
|
||||
return -1
|
||||
}
|
||||
return *e.Index < *rhs.Index
|
||||
if *e.Index < *rhs.Index {
|
||||
return -1
|
||||
} else if *e.Index == *rhs.Index {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
} else if rhs.Index != nil {
|
||||
// Yes, I know the next statement is the same. But this way
|
||||
// the obvious way of extending the function wil be bug-free.
|
||||
return false
|
||||
return 1
|
||||
}
|
||||
|
||||
return false
|
||||
return 0
|
||||
}
|
||||
|
||||
// Equals returns true if both path elements are equal.
|
||||
func (e PathElement) Equals(rhs PathElement) bool {
|
||||
return !e.Less(rhs) && !rhs.Less(e)
|
||||
if e.FieldName != nil {
|
||||
if rhs.FieldName == nil {
|
||||
return false
|
||||
}
|
||||
return *e.FieldName == *rhs.FieldName
|
||||
} else if rhs.FieldName != nil {
|
||||
return false
|
||||
}
|
||||
if e.Key != nil {
|
||||
if rhs.Key == nil {
|
||||
return false
|
||||
}
|
||||
return e.Key.Equals(*rhs.Key)
|
||||
} else if rhs.Key != nil {
|
||||
return false
|
||||
}
|
||||
if e.Value != nil {
|
||||
if rhs.Value == nil {
|
||||
return false
|
||||
}
|
||||
return value.Equals(*e.Value, *rhs.Value)
|
||||
} else if rhs.Value != nil {
|
||||
return false
|
||||
}
|
||||
if e.Index != nil {
|
||||
if rhs.Index == nil {
|
||||
return false
|
||||
}
|
||||
return *e.Index == *rhs.Index
|
||||
} else if rhs.Index != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// String presents the path element as a human-readable string.
|
||||
|
|
@ -103,12 +143,12 @@ func (e PathElement) String() string {
|
|||
case e.Key != nil:
|
||||
strs := make([]string, len(*e.Key))
|
||||
for i, k := range *e.Key {
|
||||
strs[i] = fmt.Sprintf("%v=%v", k.Name, k.Value)
|
||||
strs[i] = fmt.Sprintf("%v=%v", k.Name, value.ToString(k.Value))
|
||||
}
|
||||
// Keys are supposed to be sorted.
|
||||
return "[" + strings.Join(strs, ",") + "]"
|
||||
case e.Value != nil:
|
||||
return fmt.Sprintf("[=%v]", e.Value)
|
||||
return fmt.Sprintf("[=%v]", value.ToString(*e.Value))
|
||||
case e.Index != nil:
|
||||
return fmt.Sprintf("[%v]", *e.Index)
|
||||
default:
|
||||
|
|
@ -127,10 +167,7 @@ func KeyByFields(nameValues ...interface{}) *value.FieldList {
|
|||
}
|
||||
out := value.FieldList{}
|
||||
for i := 0; i < len(nameValues)-1; i += 2 {
|
||||
out = append(out, value.Field{
|
||||
Name: nameValues[i].(string),
|
||||
Value: nameValues[i+1].(value.Value),
|
||||
})
|
||||
out = append(out, value.Field{Name: nameValues[i].(string), Value: value.NewValueInterface(nameValues[i+1])})
|
||||
}
|
||||
out.Sort()
|
||||
return &out
|
||||
|
|
@ -17,7 +17,7 @@ limitations under the License.
|
|||
package fieldpath
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
// SetFromValue creates a set containing every leaf field mentioned in v.
|
||||
|
|
@ -25,9 +25,10 @@ func SetFromValue(v value.Value) *Set {
|
|||
s := NewSet()
|
||||
|
||||
w := objectWalker{
|
||||
path: Path{},
|
||||
value: v,
|
||||
do: func(p Path) { s.Insert(p) },
|
||||
path: Path{},
|
||||
value: v,
|
||||
allocator: value.NewFreelistAllocator(),
|
||||
do: func(p Path) { s.Insert(p) },
|
||||
}
|
||||
|
||||
w.walk()
|
||||
|
|
@ -35,45 +36,52 @@ func SetFromValue(v value.Value) *Set {
|
|||
}
|
||||
|
||||
type objectWalker struct {
|
||||
path Path
|
||||
value value.Value
|
||||
path Path
|
||||
value value.Value
|
||||
allocator value.Allocator
|
||||
|
||||
do func(Path)
|
||||
}
|
||||
|
||||
func (w *objectWalker) walk() {
|
||||
switch {
|
||||
case w.value.Null:
|
||||
case w.value.FloatValue != nil:
|
||||
case w.value.IntValue != nil:
|
||||
case w.value.StringValue != nil:
|
||||
case w.value.BooleanValue != nil:
|
||||
case w.value.IsNull():
|
||||
case w.value.IsFloat():
|
||||
case w.value.IsInt():
|
||||
case w.value.IsString():
|
||||
case w.value.IsBool():
|
||||
// All leaf fields handled the same way (after the switch
|
||||
// statement).
|
||||
|
||||
// Descend
|
||||
case w.value.ListValue != nil:
|
||||
case w.value.IsList():
|
||||
// If the list were atomic, we'd break here, but we don't have
|
||||
// a schema, so we can't tell.
|
||||
|
||||
for i, child := range w.value.ListValue.Items {
|
||||
l := w.value.AsListUsing(w.allocator)
|
||||
defer w.allocator.Free(l)
|
||||
iter := l.RangeUsing(w.allocator)
|
||||
defer w.allocator.Free(iter)
|
||||
for iter.Next() {
|
||||
i, value := iter.Item()
|
||||
w2 := *w
|
||||
w2.path = append(w.path, GuessBestListPathElement(i, child))
|
||||
w2.value = child
|
||||
w2.path = append(w.path, w.GuessBestListPathElement(i, value))
|
||||
w2.value = value
|
||||
w2.walk()
|
||||
}
|
||||
return
|
||||
case w.value.MapValue != nil:
|
||||
case w.value.IsMap():
|
||||
// If the map/struct were atomic, we'd break here, but we don't
|
||||
// have a schema, so we can't tell.
|
||||
|
||||
for i := range w.value.MapValue.Items {
|
||||
child := w.value.MapValue.Items[i]
|
||||
m := w.value.AsMapUsing(w.allocator)
|
||||
defer w.allocator.Free(m)
|
||||
m.IterateUsing(w.allocator, func(k string, val value.Value) bool {
|
||||
w2 := *w
|
||||
w2.path = append(w.path, PathElement{FieldName: &child.Name})
|
||||
w2.value = child.Value
|
||||
w2.path = append(w.path, PathElement{FieldName: &k})
|
||||
w2.value = val
|
||||
w2.walk()
|
||||
}
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -96,25 +104,27 @@ var AssociativeListCandidateFieldNames = []string{
|
|||
// referencing by index is acceptable. Currently this is done by checking
|
||||
// whether item has any of the fields listed in
|
||||
// AssociativeListCandidateFieldNames which have scalar values.
|
||||
func GuessBestListPathElement(index int, item value.Value) PathElement {
|
||||
if item.MapValue == nil {
|
||||
func (w *objectWalker) GuessBestListPathElement(index int, item value.Value) PathElement {
|
||||
if !item.IsMap() {
|
||||
// Non map items could be parts of sets or regular "atomic"
|
||||
// lists. We won't try to guess whether something should be a
|
||||
// set or not.
|
||||
return PathElement{Index: &index}
|
||||
}
|
||||
|
||||
m := item.AsMapUsing(w.allocator)
|
||||
defer w.allocator.Free(m)
|
||||
var keys value.FieldList
|
||||
for _, name := range AssociativeListCandidateFieldNames {
|
||||
f, ok := item.MapValue.Get(name)
|
||||
f, ok := m.Get(name)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
// only accept primitive/scalar types as keys.
|
||||
if f.Value.Null || f.Value.MapValue != nil || f.Value.ListValue != nil {
|
||||
if f.IsNull() || f.IsMap() || f.IsList() {
|
||||
continue
|
||||
}
|
||||
keys = append(keys, *f)
|
||||
keys = append(keys, value.Field{Name: name, Value: f})
|
||||
}
|
||||
if len(keys) > 0 {
|
||||
keys.Sort()
|
||||
|
|
@ -13,6 +13,11 @@ limitations under the License.
|
|||
|
||||
package fieldpath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// APIVersion describes the version of an object or of a fieldset.
|
||||
type APIVersion string
|
||||
|
||||
|
|
@ -53,6 +58,37 @@ func (v versionedSet) Applied() bool {
|
|||
// what version).
|
||||
type ManagedFields map[string]VersionedSet
|
||||
|
||||
// Equals returns true if the two managedfields are the same, false
|
||||
// otherwise.
|
||||
func (lhs ManagedFields) Equals(rhs ManagedFields) bool {
|
||||
if len(lhs) != len(rhs) {
|
||||
return false
|
||||
}
|
||||
|
||||
for manager, left := range lhs {
|
||||
right, ok := rhs[manager]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if left.APIVersion() != right.APIVersion() || left.Applied() != right.Applied() {
|
||||
return false
|
||||
}
|
||||
if !left.Set().Equals(right.Set()) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Copy the list, this is mostly a shallow copy.
|
||||
func (lhs ManagedFields) Copy() ManagedFields {
|
||||
copy := ManagedFields{}
|
||||
for manager, set := range lhs {
|
||||
copy[manager] = set
|
||||
}
|
||||
return copy
|
||||
}
|
||||
|
||||
// Difference returns a symmetric difference between two Managers. If a
|
||||
// given user's entry has version X in lhs and version Y in rhs, then
|
||||
// the return value for that user will be from rhs. If the difference for
|
||||
|
|
@ -95,3 +131,14 @@ func (lhs ManagedFields) Difference(rhs ManagedFields) ManagedFields {
|
|||
|
||||
return diff
|
||||
}
|
||||
|
||||
func (lhs ManagedFields) String() string {
|
||||
s := strings.Builder{}
|
||||
for k, v := range lhs {
|
||||
fmt.Fprintf(&s, "%s:\n", k)
|
||||
fmt.Fprintf(&s, "- Applied: %v\n", v.Applied())
|
||||
fmt.Fprintf(&s, "- APIVersion: %v\n", v.APIVersion())
|
||||
fmt.Fprintf(&s, "- Set: %v\n", v.Set())
|
||||
}
|
||||
return s.String()
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
// Path describes how to select a potentially deeply-nested child field given a
|
||||
|
|
@ -37,32 +37,35 @@ func (fp Path) String() string {
|
|||
|
||||
// Equals returns true if the two paths are equivalent.
|
||||
func (fp Path) Equals(fp2 Path) bool {
|
||||
return !fp.Less(fp2) && !fp2.Less(fp)
|
||||
if len(fp) != len(fp2) {
|
||||
return false
|
||||
}
|
||||
for i := range fp {
|
||||
if !fp[i].Equals(fp2[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Less provides a lexical order for Paths.
|
||||
func (fp Path) Less(rhs Path) bool {
|
||||
func (fp Path) Compare(rhs Path) int {
|
||||
i := 0
|
||||
for {
|
||||
if i >= len(fp) && i >= len(rhs) {
|
||||
// Paths are the same length and all items are equal.
|
||||
return false
|
||||
return 0
|
||||
}
|
||||
if i >= len(fp) {
|
||||
// LHS is shorter.
|
||||
return true
|
||||
return -1
|
||||
}
|
||||
if i >= len(rhs) {
|
||||
// RHS is shorter.
|
||||
return false
|
||||
return 1
|
||||
}
|
||||
if fp[i].Less(rhs[i]) {
|
||||
// LHS is less; return
|
||||
return true
|
||||
}
|
||||
if rhs[i].Less(fp[i]) {
|
||||
// RHS is less; return
|
||||
return false
|
||||
if c := fp[i].Compare(rhs[i]); c != 0 {
|
||||
return c
|
||||
}
|
||||
// The items are equal; continue.
|
||||
i++
|
||||
|
|
@ -19,7 +19,7 @@ package fieldpath
|
|||
import (
|
||||
"sort"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
// PathElementValueMap is a map from PathElement to value.Value.
|
||||
|
|
@ -76,10 +76,10 @@ func (s *PathElementValueMap) Get(pe PathElement) (value.Value, bool) {
|
|||
return !s.members[i].PathElement.Less(pe)
|
||||
})
|
||||
if loc == len(s.members) {
|
||||
return value.Value{}, false
|
||||
return nil, false
|
||||
}
|
||||
if s.members[loc].PathElement.Equals(pe) {
|
||||
return s.members[loc].Value, true
|
||||
}
|
||||
return value.Value{}, false
|
||||
return nil, false
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ import (
|
|||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
var ErrUnknownPathElementType = errors.New("unknown path element type")
|
||||
|
|
@ -84,6 +84,7 @@ func DeserializePathElement(s string) (PathElement, error) {
|
|||
iter := readPool.BorrowIterator(b)
|
||||
defer readPool.ReturnIterator(iter)
|
||||
fields := value.FieldList{}
|
||||
|
||||
iter.ReadObjectCB(func(iter *jsoniter.Iterator, key string) bool {
|
||||
v, err := value.ReadJSONIter(iter)
|
||||
if err != nil {
|
||||
|
|
@ -134,19 +135,20 @@ func serializePathElementToWriter(w io.Writer, pe PathElement) error {
|
|||
return err
|
||||
}
|
||||
stream.WriteObjectStart()
|
||||
|
||||
for i, field := range *pe.Key {
|
||||
if i > 0 {
|
||||
stream.WriteMore()
|
||||
}
|
||||
stream.WriteObjectField(field.Name)
|
||||
field.Value.WriteJSONStream(stream)
|
||||
value.WriteJSONStream(field.Value, stream)
|
||||
}
|
||||
stream.WriteObjectEnd()
|
||||
case pe.Value != nil:
|
||||
if _, err := stream.Write(peValueSepBytes); err != nil {
|
||||
return err
|
||||
}
|
||||
pe.Value.WriteJSONStream(stream)
|
||||
value.WriteJSONStream(*pe.Value, stream)
|
||||
case pe.Index != nil:
|
||||
if _, err := stream.Write(peIndexSepBytes); err != nil {
|
||||
return err
|
||||
|
|
@ -40,7 +40,7 @@ func (s *Set) ToJSONStream(w io.Writer) error {
|
|||
var r reusableBuilder
|
||||
|
||||
stream.WriteObjectStart()
|
||||
err := s.emitContents_v1(false, stream, &r)
|
||||
err := s.emitContentsV1(false, stream, &r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -76,7 +76,7 @@ func (r *reusableBuilder) reset() *bytes.Buffer {
|
|||
return &r.Buffer
|
||||
}
|
||||
|
||||
func (s *Set) emitContents_v1(includeSelf bool, stream *jsoniter.Stream, r *reusableBuilder) error {
|
||||
func (s *Set) emitContentsV1(includeSelf bool, stream *jsoniter.Stream, r *reusableBuilder) error {
|
||||
mi, ci := 0, 0
|
||||
first := true
|
||||
preWrite := func() {
|
||||
|
|
@ -87,11 +87,17 @@ func (s *Set) emitContents_v1(includeSelf bool, stream *jsoniter.Stream, r *reus
|
|||
stream.WriteMore()
|
||||
}
|
||||
|
||||
if includeSelf && !(len(s.Members.members) == 0 && len(s.Children.members) == 0) {
|
||||
preWrite()
|
||||
stream.WriteObjectField(".")
|
||||
stream.WriteEmptyObject()
|
||||
}
|
||||
|
||||
for mi < len(s.Members.members) && ci < len(s.Children.members) {
|
||||
mpe := s.Members.members[mi]
|
||||
cpe := s.Children.members[ci].pathElement
|
||||
|
||||
if mpe.Less(cpe) {
|
||||
if c := mpe.Compare(cpe); c < 0 {
|
||||
preWrite()
|
||||
if err := serializePathElementToWriter(r.reset(), mpe); err != nil {
|
||||
return err
|
||||
|
|
@ -99,14 +105,14 @@ func (s *Set) emitContents_v1(includeSelf bool, stream *jsoniter.Stream, r *reus
|
|||
stream.WriteObjectField(r.unsafeString())
|
||||
stream.WriteEmptyObject()
|
||||
mi++
|
||||
} else if cpe.Less(mpe) {
|
||||
} else if c > 0 {
|
||||
preWrite()
|
||||
if err := serializePathElementToWriter(r.reset(), cpe); err != nil {
|
||||
return err
|
||||
}
|
||||
stream.WriteObjectField(r.unsafeString())
|
||||
stream.WriteObjectStart()
|
||||
if err := s.Children.members[ci].set.emitContents_v1(false, stream, r); err != nil {
|
||||
if err := s.Children.members[ci].set.emitContentsV1(false, stream, r); err != nil {
|
||||
return err
|
||||
}
|
||||
stream.WriteObjectEnd()
|
||||
|
|
@ -118,7 +124,7 @@ func (s *Set) emitContents_v1(includeSelf bool, stream *jsoniter.Stream, r *reus
|
|||
}
|
||||
stream.WriteObjectField(r.unsafeString())
|
||||
stream.WriteObjectStart()
|
||||
if err := s.Children.members[ci].set.emitContents_v1(true, stream, r); err != nil {
|
||||
if err := s.Children.members[ci].set.emitContentsV1(true, stream, r); err != nil {
|
||||
return err
|
||||
}
|
||||
stream.WriteObjectEnd()
|
||||
|
|
@ -148,18 +154,13 @@ func (s *Set) emitContents_v1(includeSelf bool, stream *jsoniter.Stream, r *reus
|
|||
}
|
||||
stream.WriteObjectField(r.unsafeString())
|
||||
stream.WriteObjectStart()
|
||||
if err := s.Children.members[ci].set.emitContents_v1(false, stream, r); err != nil {
|
||||
if err := s.Children.members[ci].set.emitContentsV1(false, stream, r); err != nil {
|
||||
return err
|
||||
}
|
||||
stream.WriteObjectEnd()
|
||||
ci++
|
||||
}
|
||||
|
||||
if includeSelf && !first {
|
||||
preWrite()
|
||||
stream.WriteObjectField(".")
|
||||
stream.WriteEmptyObject()
|
||||
}
|
||||
return manageMemory(stream)
|
||||
}
|
||||
|
||||
|
|
@ -168,7 +169,7 @@ func (s *Set) FromJSON(r io.Reader) error {
|
|||
// The iterator pool is completely useless for memory management, grrr.
|
||||
iter := jsoniter.Parse(jsoniter.ConfigCompatibleWithStandardLibrary, r, 4096)
|
||||
|
||||
found, _ := readIter_v1(iter)
|
||||
found, _ := readIterV1(iter)
|
||||
if found == nil {
|
||||
*s = Set{}
|
||||
} else {
|
||||
|
|
@ -179,7 +180,7 @@ func (s *Set) FromJSON(r io.Reader) error {
|
|||
|
||||
// returns true if this subtree is also (or only) a member of parent; s is nil
|
||||
// if there are no further children.
|
||||
func readIter_v1(iter *jsoniter.Iterator) (children *Set, isMember bool) {
|
||||
func readIterV1(iter *jsoniter.Iterator) (children *Set, isMember bool) {
|
||||
iter.ReadMapCB(func(iter *jsoniter.Iterator, key string) bool {
|
||||
if key == "." {
|
||||
isMember = true
|
||||
|
|
@ -198,7 +199,7 @@ func readIter_v1(iter *jsoniter.Iterator) (children *Set, isMember bool) {
|
|||
iter.Skip()
|
||||
return true
|
||||
}
|
||||
grandchildren, childIsMember := readIter_v1(iter)
|
||||
grandchildren, childIsMember := readIterV1(iter)
|
||||
if childIsMember {
|
||||
if children == nil {
|
||||
children = &Set{}
|
||||
|
|
@ -21,7 +21,7 @@ import (
|
|||
"sort"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/fieldpath"
|
||||
)
|
||||
|
||||
// Conflict is a conflict on a specific field with the current manager of
|
||||
|
|
@ -16,8 +16,8 @@ package merge
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/typed"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/typed"
|
||||
)
|
||||
|
||||
// Converter is an interface to the conversion logic. The converter
|
||||
|
|
@ -148,7 +148,8 @@ func (s *Updater) Update(liveObject, newObject *typed.TypedValue, version fieldp
|
|||
|
||||
// Apply should be called when Apply is run, given the current object as
|
||||
// well as the configuration that is applied. This will merge the object
|
||||
// and return it.
|
||||
// and return it. If the object hasn't changed, nil is returned (the
|
||||
// managers can still have changed though).
|
||||
func (s *Updater) Apply(liveObject, configObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string, force bool) (*typed.TypedValue, fieldpath.ManagedFields, error) {
|
||||
managers = shallowCopyManagers(managers)
|
||||
var err error
|
||||
|
|
@ -178,10 +179,13 @@ func (s *Updater) Apply(liveObject, configObject *typed.TypedValue, version fiel
|
|||
if err != nil {
|
||||
return nil, fieldpath.ManagedFields{}, fmt.Errorf("failed to prune fields: %v", err)
|
||||
}
|
||||
managers, _, err = s.update(liveObject, newObject, version, managers, manager, force)
|
||||
managers, compare, err := s.update(liveObject, newObject, version, managers, manager, force)
|
||||
if err != nil {
|
||||
return nil, fieldpath.ManagedFields{}, err
|
||||
}
|
||||
if compare.IsSame() {
|
||||
newObject = nil
|
||||
}
|
||||
return newObject, managers, nil
|
||||
}
|
||||
|
||||
|
|
@ -208,6 +212,7 @@ func (s *Updater) prune(merged *typed.TypedValue, managers fieldpath.ManagedFiel
|
|||
}
|
||||
return nil, fmt.Errorf("failed to convert merged object to last applied version: %v", err)
|
||||
}
|
||||
|
||||
pruned := convertedMerged.RemoveItems(lastSet.Set())
|
||||
pruned, err = s.addBackOwnedItems(convertedMerged, pruned, managers, applyingManager)
|
||||
if err != nil {
|
||||
|
|
@ -17,12 +17,16 @@ limitations under the License.
|
|||
package schema
|
||||
|
||||
// Equals returns true iff the two Schemas are equal.
|
||||
func (a Schema) Equals(b Schema) bool {
|
||||
func (a *Schema) Equals(b *Schema) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
|
||||
if len(a.Types) != len(b.Types) {
|
||||
return false
|
||||
}
|
||||
for i := range a.Types {
|
||||
if !a.Types[i].Equals(b.Types[i]) {
|
||||
if !a.Types[i].Equals(&b.Types[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
@ -33,7 +37,10 @@ func (a Schema) Equals(b Schema) bool {
|
|||
//
|
||||
// Note that two typerefs that have an equivalent type but where one is
|
||||
// inlined and the other is named, are not considered equal.
|
||||
func (a TypeRef) Equals(b TypeRef) bool {
|
||||
func (a *TypeRef) Equals(b *TypeRef) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
if (a.NamedType == nil) != (b.NamedType == nil) {
|
||||
return false
|
||||
}
|
||||
|
|
@ -43,19 +50,25 @@ func (a TypeRef) Equals(b TypeRef) bool {
|
|||
}
|
||||
//return true
|
||||
}
|
||||
return a.Inlined.Equals(b.Inlined)
|
||||
return a.Inlined.Equals(&b.Inlined)
|
||||
}
|
||||
|
||||
// Equals returns true iff the two TypeDefs are equal.
|
||||
func (a TypeDef) Equals(b TypeDef) bool {
|
||||
func (a *TypeDef) Equals(b *TypeDef) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
if a.Name != b.Name {
|
||||
return false
|
||||
}
|
||||
return a.Atom.Equals(b.Atom)
|
||||
return a.Atom.Equals(&b.Atom)
|
||||
}
|
||||
|
||||
// Equals returns true iff the two Atoms are equal.
|
||||
func (a Atom) Equals(b Atom) bool {
|
||||
func (a *Atom) Equals(b *Atom) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
if (a.Scalar == nil) != (b.Scalar == nil) {
|
||||
return false
|
||||
}
|
||||
|
|
@ -69,16 +82,19 @@ func (a Atom) Equals(b Atom) bool {
|
|||
case a.Scalar != nil:
|
||||
return *a.Scalar == *b.Scalar
|
||||
case a.List != nil:
|
||||
return a.List.Equals(*b.List)
|
||||
return a.List.Equals(b.List)
|
||||
case a.Map != nil:
|
||||
return a.Map.Equals(*b.Map)
|
||||
return a.Map.Equals(b.Map)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Equals returns true iff the two Maps are equal.
|
||||
func (a Map) Equals(b Map) bool {
|
||||
if !a.ElementType.Equals(b.ElementType) {
|
||||
func (a *Map) Equals(b *Map) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
if !a.ElementType.Equals(&b.ElementType) {
|
||||
return false
|
||||
}
|
||||
if a.ElementRelationship != b.ElementRelationship {
|
||||
|
|
@ -88,7 +104,7 @@ func (a Map) Equals(b Map) bool {
|
|||
return false
|
||||
}
|
||||
for i := range a.Fields {
|
||||
if !a.Fields[i].Equals(b.Fields[i]) {
|
||||
if !a.Fields[i].Equals(&b.Fields[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
@ -96,7 +112,7 @@ func (a Map) Equals(b Map) bool {
|
|||
return false
|
||||
}
|
||||
for i := range a.Unions {
|
||||
if !a.Unions[i].Equals(b.Unions[i]) {
|
||||
if !a.Unions[i].Equals(&b.Unions[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
@ -104,7 +120,10 @@ func (a Map) Equals(b Map) bool {
|
|||
}
|
||||
|
||||
// Equals returns true iff the two Unions are equal.
|
||||
func (a Union) Equals(b Union) bool {
|
||||
func (a *Union) Equals(b *Union) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
if (a.Discriminator == nil) != (b.Discriminator == nil) {
|
||||
return false
|
||||
}
|
||||
|
|
@ -120,7 +139,7 @@ func (a Union) Equals(b Union) bool {
|
|||
return false
|
||||
}
|
||||
for i := range a.Fields {
|
||||
if !a.Fields[i].Equals(b.Fields[i]) {
|
||||
if !a.Fields[i].Equals(&b.Fields[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
@ -128,7 +147,10 @@ func (a Union) Equals(b Union) bool {
|
|||
}
|
||||
|
||||
// Equals returns true iff the two UnionFields are equal.
|
||||
func (a UnionField) Equals(b UnionField) bool {
|
||||
func (a *UnionField) Equals(b *UnionField) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
if a.FieldName != b.FieldName {
|
||||
return false
|
||||
}
|
||||
|
|
@ -139,16 +161,22 @@ func (a UnionField) Equals(b UnionField) bool {
|
|||
}
|
||||
|
||||
// Equals returns true iff the two StructFields are equal.
|
||||
func (a StructField) Equals(b StructField) bool {
|
||||
func (a *StructField) Equals(b *StructField) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
if a.Name != b.Name {
|
||||
return false
|
||||
}
|
||||
return a.Type.Equals(b.Type)
|
||||
return a.Type.Equals(&b.Type)
|
||||
}
|
||||
|
||||
// Equals returns true iff the two Lists are equal.
|
||||
func (a List) Equals(b List) bool {
|
||||
if !a.ElementType.Equals(b.ElementType) {
|
||||
func (a *List) Equals(b *List) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == nil && b == nil
|
||||
}
|
||||
if !a.ElementType.Equals(&b.ElementType) {
|
||||
return false
|
||||
}
|
||||
if a.ElementRelationship != b.ElementRelationship {
|
||||
|
|
@ -21,14 +21,14 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
// ValidationError reports an error about a particular field
|
||||
type ValidationError struct {
|
||||
Path fieldpath.Path
|
||||
Path string
|
||||
ErrorMessage string
|
||||
}
|
||||
|
||||
|
|
@ -56,75 +56,83 @@ func (errs ValidationErrors) Error() string {
|
|||
return strings.Join(messages, "\n")
|
||||
}
|
||||
|
||||
// errorFormatter makes it easy to keep a list of validation errors. They
|
||||
// should all be packed into a single error object before leaving the package
|
||||
// boundary, since it's weird to have functions not return a plain error type.
|
||||
type errorFormatter struct {
|
||||
path fieldpath.Path
|
||||
}
|
||||
|
||||
func (ef *errorFormatter) descend(pe fieldpath.PathElement) {
|
||||
ef.path = append(ef.path, pe)
|
||||
}
|
||||
|
||||
// parent returns the parent, for the purpose of buffer reuse. It's an error to
|
||||
// call this if there is no parent.
|
||||
func (ef *errorFormatter) parent() errorFormatter {
|
||||
return errorFormatter{
|
||||
path: ef.path[:len(ef.path)-1],
|
||||
// Set the given path to all the validation errors.
|
||||
func (errs ValidationErrors) WithPath(p string) ValidationErrors {
|
||||
for i := range errs {
|
||||
errs[i].Path = p
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (ef errorFormatter) errorf(format string, args ...interface{}) ValidationErrors {
|
||||
// WithPrefix prefixes all errors path with the given pathelement. This
|
||||
// is useful when unwinding the stack on errors.
|
||||
func (errs ValidationErrors) WithPrefix(prefix string) ValidationErrors {
|
||||
for i := range errs {
|
||||
errs[i].Path = prefix + errs[i].Path
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
// WithLazyPrefix prefixes all errors path with the given pathelement.
|
||||
// This is useful when unwinding the stack on errors. Prefix is
|
||||
// computed lazily only if there is an error.
|
||||
func (errs ValidationErrors) WithLazyPrefix(fn func() string) ValidationErrors {
|
||||
if len(errs) == 0 {
|
||||
return errs
|
||||
}
|
||||
prefix := ""
|
||||
if fn != nil {
|
||||
prefix = fn()
|
||||
}
|
||||
for i := range errs {
|
||||
errs[i].Path = prefix + errs[i].Path
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func errorf(format string, args ...interface{}) ValidationErrors {
|
||||
return ValidationErrors{{
|
||||
Path: append(fieldpath.Path{}, ef.path...),
|
||||
ErrorMessage: fmt.Sprintf(format, args...),
|
||||
}}
|
||||
}
|
||||
|
||||
func (ef errorFormatter) error(err error) ValidationErrors {
|
||||
return ValidationErrors{{
|
||||
Path: append(fieldpath.Path{}, ef.path...),
|
||||
ErrorMessage: err.Error(),
|
||||
}}
|
||||
}
|
||||
|
||||
func (ef errorFormatter) prefixError(prefix string, err error) ValidationErrors {
|
||||
return ValidationErrors{{
|
||||
Path: append(fieldpath.Path{}, ef.path...),
|
||||
ErrorMessage: prefix + err.Error(),
|
||||
}}
|
||||
}
|
||||
|
||||
type atomHandler interface {
|
||||
doScalar(*schema.Scalar) ValidationErrors
|
||||
doList(*schema.List) ValidationErrors
|
||||
doMap(*schema.Map) ValidationErrors
|
||||
|
||||
errorf(msg string, args ...interface{}) ValidationErrors
|
||||
}
|
||||
|
||||
func resolveSchema(s *schema.Schema, tr schema.TypeRef, v *value.Value, ah atomHandler) ValidationErrors {
|
||||
func resolveSchema(s *schema.Schema, tr schema.TypeRef, v value.Value, ah atomHandler) ValidationErrors {
|
||||
a, ok := s.Resolve(tr)
|
||||
if !ok {
|
||||
return ah.errorf("schema error: no type found matching: %v", *tr.NamedType)
|
||||
return errorf("schema error: no type found matching: %v", *tr.NamedType)
|
||||
}
|
||||
|
||||
a = deduceAtom(a, v)
|
||||
return handleAtom(a, tr, ah)
|
||||
}
|
||||
|
||||
func deduceAtom(a schema.Atom, v *value.Value) schema.Atom {
|
||||
// deduceAtom determines which of the possible types in atom 'atom' applies to value 'val'.
|
||||
// If val is of a type allowed by atom, return a copy of atom with all other types set to nil.
|
||||
// if val is nil, or is not of a type allowed by atom, just return the original atom,
|
||||
// and validation will fail at a later stage. (with a more useful error)
|
||||
func deduceAtom(atom schema.Atom, val value.Value) schema.Atom {
|
||||
switch {
|
||||
case v == nil:
|
||||
case v.FloatValue != nil, v.IntValue != nil, v.StringValue != nil, v.BooleanValue != nil:
|
||||
return schema.Atom{Scalar: a.Scalar}
|
||||
case v.ListValue != nil:
|
||||
return schema.Atom{List: a.List}
|
||||
case v.MapValue != nil:
|
||||
return schema.Atom{Map: a.Map}
|
||||
case val == nil:
|
||||
case val.IsFloat(), val.IsInt(), val.IsString(), val.IsBool():
|
||||
if atom.Scalar != nil {
|
||||
return schema.Atom{Scalar: atom.Scalar}
|
||||
}
|
||||
case val.IsList():
|
||||
if atom.List != nil {
|
||||
return schema.Atom{List: atom.List}
|
||||
}
|
||||
case val.IsMap():
|
||||
if atom.Map != nil {
|
||||
return schema.Atom{Map: atom.Map}
|
||||
}
|
||||
}
|
||||
return a
|
||||
return atom
|
||||
}
|
||||
|
||||
func handleAtom(a schema.Atom, tr schema.TypeRef, ah atomHandler) ValidationErrors {
|
||||
|
|
@ -142,80 +150,55 @@ func handleAtom(a schema.Atom, tr schema.TypeRef, ah atomHandler) ValidationErro
|
|||
name = "named type: " + *tr.NamedType
|
||||
}
|
||||
|
||||
return ah.errorf("schema error: invalid atom: %v", name)
|
||||
}
|
||||
|
||||
func (ef errorFormatter) validateScalar(t *schema.Scalar, v *value.Value, prefix string) (errs ValidationErrors) {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
if v.Null {
|
||||
return nil
|
||||
}
|
||||
switch *t {
|
||||
case schema.Numeric:
|
||||
if v.FloatValue == nil && v.IntValue == nil {
|
||||
// TODO: should the schema separate int and float?
|
||||
return ef.errorf("%vexpected numeric (int or float), got %v", prefix, v)
|
||||
}
|
||||
case schema.String:
|
||||
if v.StringValue == nil {
|
||||
return ef.errorf("%vexpected string, got %v", prefix, v)
|
||||
}
|
||||
case schema.Boolean:
|
||||
if v.BooleanValue == nil {
|
||||
return ef.errorf("%vexpected boolean, got %v", prefix, v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return errorf("schema error: invalid atom: %v", name)
|
||||
}
|
||||
|
||||
// Returns the list, or an error. Reminder: nil is a valid list and might be returned.
|
||||
func listValue(val value.Value) (*value.List, error) {
|
||||
switch {
|
||||
case val.Null:
|
||||
func listValue(a value.Allocator, val value.Value) (value.List, error) {
|
||||
if val.IsNull() {
|
||||
// Null is a valid list.
|
||||
return nil, nil
|
||||
case val.ListValue != nil:
|
||||
return val.ListValue, nil
|
||||
default:
|
||||
}
|
||||
if !val.IsList() {
|
||||
return nil, fmt.Errorf("expected list, got %v", val)
|
||||
}
|
||||
return val.AsListUsing(a), nil
|
||||
}
|
||||
|
||||
// Returns the map, or an error. Reminder: nil is a valid map and might be returned.
|
||||
func mapValue(val value.Value) (*value.Map, error) {
|
||||
switch {
|
||||
case val.Null:
|
||||
func mapValue(a value.Allocator, val value.Value) (value.Map, error) {
|
||||
if val == nil {
|
||||
return nil, fmt.Errorf("expected map, got nil")
|
||||
}
|
||||
if val.IsNull() {
|
||||
// Null is a valid map.
|
||||
return nil, nil
|
||||
case val.MapValue != nil:
|
||||
return val.MapValue, nil
|
||||
default:
|
||||
}
|
||||
if !val.IsMap() {
|
||||
return nil, fmt.Errorf("expected map, got %v", val)
|
||||
}
|
||||
return val.AsMapUsing(a), nil
|
||||
}
|
||||
|
||||
func keyedAssociativeListItemToPathElement(list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
|
||||
func keyedAssociativeListItemToPathElement(a value.Allocator, list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
|
||||
pe := fieldpath.PathElement{}
|
||||
if child.Null {
|
||||
if child.IsNull() {
|
||||
// For now, the keys are required which means that null entries
|
||||
// are illegal.
|
||||
return pe, errors.New("associative list with keys may not have a null element")
|
||||
}
|
||||
if child.MapValue == nil {
|
||||
if !child.IsMap() {
|
||||
return pe, errors.New("associative list with keys may not have non-map elements")
|
||||
}
|
||||
keyMap := value.FieldList{}
|
||||
m := child.AsMapUsing(a)
|
||||
defer a.Free(m)
|
||||
for _, fieldName := range list.Keys {
|
||||
var fieldValue value.Value
|
||||
field, ok := child.MapValue.Get(fieldName)
|
||||
if ok {
|
||||
fieldValue = field.Value
|
||||
if val, ok := m.Get(fieldName); ok {
|
||||
keyMap = append(keyMap, value.Field{Name: fieldName, Value: val})
|
||||
} else {
|
||||
// Treat keys as required.
|
||||
return pe, fmt.Errorf("associative list with keys has an element that omits key field %q", fieldName)
|
||||
}
|
||||
keyMap = append(keyMap, value.Field{Name: fieldName, Value: fieldValue})
|
||||
}
|
||||
keyMap.Sort()
|
||||
pe.Key = &keyMap
|
||||
|
|
@ -225,15 +208,15 @@ func keyedAssociativeListItemToPathElement(list *schema.List, index int, child v
|
|||
func setItemToPathElement(list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
|
||||
pe := fieldpath.PathElement{}
|
||||
switch {
|
||||
case child.MapValue != nil:
|
||||
case child.IsMap():
|
||||
// TODO: atomic maps should be acceptable.
|
||||
return pe, errors.New("associative list without keys has an element that's a map type")
|
||||
case child.ListValue != nil:
|
||||
case child.IsList():
|
||||
// Should we support a set of lists? For the moment
|
||||
// let's say we don't.
|
||||
// TODO: atomic lists should be acceptable.
|
||||
return pe, errors.New("not supported: associative list with lists as elements")
|
||||
case child.Null:
|
||||
case child.IsNull():
|
||||
return pe, errors.New("associative list without keys has an element that's an explicit null")
|
||||
default:
|
||||
// We are a set type.
|
||||
|
|
@ -242,10 +225,10 @@ func setItemToPathElement(list *schema.List, index int, child value.Value) (fiel
|
|||
}
|
||||
}
|
||||
|
||||
func listItemToPathElement(list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
|
||||
func listItemToPathElement(a value.Allocator, list *schema.List, index int, child value.Value) (fieldpath.PathElement, error) {
|
||||
if list.ElementRelationship == schema.Associative {
|
||||
if len(list.Keys) > 0 {
|
||||
return keyedAssociativeListItemToPathElement(list, index, child)
|
||||
return keyedAssociativeListItemToPathElement(a, list, index, child)
|
||||
}
|
||||
|
||||
// If there's no keys, then we must be a set of primitives.
|
||||
|
|
@ -17,18 +17,22 @@ limitations under the License.
|
|||
package typed
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
"math"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/v3/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
type mergingWalker struct {
|
||||
errorFormatter
|
||||
lhs *value.Value
|
||||
rhs *value.Value
|
||||
lhs value.Value
|
||||
rhs value.Value
|
||||
schema *schema.Schema
|
||||
typeRef schema.TypeRef
|
||||
|
||||
// Current path that we are merging
|
||||
path fieldpath.Path
|
||||
|
||||
// How to merge. Called after schema validation for all leaf fields.
|
||||
rule mergeRule
|
||||
|
||||
|
|
@ -37,13 +41,15 @@ type mergingWalker struct {
|
|||
postItemHook mergeRule
|
||||
|
||||
// output of the merge operation (nil if none)
|
||||
out *value.Value
|
||||
out *interface{}
|
||||
|
||||
// internal housekeeping--don't set when constructing.
|
||||
inLeaf bool // Set to true if we're in a "big leaf"--atomic map/list
|
||||
|
||||
// Allocate only as many walkers as needed for the depth by storing them here.
|
||||
spareWalkers *[]*mergingWalker
|
||||
|
||||
allocator value.Allocator
|
||||
}
|
||||
|
||||
// merge rules examine w.lhs and w.rhs (up to one of which may be nil) and
|
||||
|
|
@ -54,29 +60,29 @@ type mergeRule func(w *mergingWalker)
|
|||
var (
|
||||
ruleKeepRHS = mergeRule(func(w *mergingWalker) {
|
||||
if w.rhs != nil {
|
||||
v := *w.rhs
|
||||
v := w.rhs.Unstructured()
|
||||
w.out = &v
|
||||
} else if w.lhs != nil {
|
||||
v := *w.lhs
|
||||
v := w.lhs.Unstructured()
|
||||
w.out = &v
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// merge sets w.out.
|
||||
func (w *mergingWalker) merge() (errs ValidationErrors) {
|
||||
func (w *mergingWalker) merge(prefixFn func() string) (errs ValidationErrors) {
|
||||
if w.lhs == nil && w.rhs == nil {
|
||||
// check this condidition here instead of everywhere below.
|
||||
return w.errorf("at least one of lhs and rhs must be provided")
|
||||
return errorf("at least one of lhs and rhs must be provided")
|
||||
}
|
||||
a, ok := w.schema.Resolve(w.typeRef)
|
||||
if !ok {
|
||||
return w.errorf("schema error: no type found matching: %v", *w.typeRef.NamedType)
|
||||
return errorf("schema error: no type found matching: %v", *w.typeRef.NamedType)
|
||||
}
|
||||
|
||||
alhs := deduceAtom(a, w.lhs)
|
||||
arhs := deduceAtom(a, w.rhs)
|
||||
if alhs.Equals(arhs) {
|
||||
if alhs.Equals(&arhs) {
|
||||
errs = append(errs, handleAtom(arhs, w.typeRef, w)...)
|
||||
} else {
|
||||
w2 := *w
|
||||
|
|
@ -87,7 +93,7 @@ func (w *mergingWalker) merge() (errs ValidationErrors) {
|
|||
if !w.inLeaf && w.postItemHook != nil {
|
||||
w.postItemHook(w)
|
||||
}
|
||||
return errs
|
||||
return errs.WithLazyPrefix(prefixFn)
|
||||
}
|
||||
|
||||
// doLeaf should be called on leaves before descending into children, if there
|
||||
|
|
@ -105,8 +111,8 @@ func (w *mergingWalker) doLeaf() {
|
|||
}
|
||||
|
||||
func (w *mergingWalker) doScalar(t *schema.Scalar) (errs ValidationErrors) {
|
||||
errs = append(errs, w.validateScalar(t, w.lhs, "lhs: ")...)
|
||||
errs = append(errs, w.validateScalar(t, w.rhs, "rhs: ")...)
|
||||
errs = append(errs, validateScalar(t, w.lhs, "lhs: ")...)
|
||||
errs = append(errs, validateScalar(t, w.rhs, "rhs: ")...)
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
|
@ -130,7 +136,7 @@ func (w *mergingWalker) prepareDescent(pe fieldpath.PathElement, tr schema.TypeR
|
|||
}
|
||||
*w2 = *w
|
||||
w2.typeRef = tr
|
||||
w2.errorFormatter.descend(pe)
|
||||
w2.path = append(w2.path, pe)
|
||||
w2.lhs = nil
|
||||
w2.rhs = nil
|
||||
w2.out = nil
|
||||
|
|
@ -140,48 +146,53 @@ func (w *mergingWalker) prepareDescent(pe fieldpath.PathElement, tr schema.TypeR
|
|||
func (w *mergingWalker) finishDescent(w2 *mergingWalker) {
|
||||
// if the descent caused a realloc, ensure that we reuse the buffer
|
||||
// for the next sibling.
|
||||
w.errorFormatter = w2.errorFormatter.parent()
|
||||
w.path = w2.path[:len(w2.path)-1]
|
||||
*w.spareWalkers = append(*w.spareWalkers, w2)
|
||||
}
|
||||
|
||||
func (w *mergingWalker) derefMap(prefix string, v *value.Value, dest **value.Map) (errs ValidationErrors) {
|
||||
// taking dest as input so that it can be called as a one-liner with
|
||||
// append.
|
||||
func (w *mergingWalker) derefMap(prefix string, v value.Value) (value.Map, ValidationErrors) {
|
||||
if v == nil {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
m, err := mapValue(*v)
|
||||
m, err := mapValue(w.allocator, v)
|
||||
if err != nil {
|
||||
return w.prefixError(prefix, err)
|
||||
return nil, errorf("%v: %v", prefix, err)
|
||||
}
|
||||
*dest = m
|
||||
return nil
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs *value.List) (errs ValidationErrors) {
|
||||
out := &value.List{}
|
||||
func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (errs ValidationErrors) {
|
||||
rLen := 0
|
||||
if rhs != nil {
|
||||
rLen = rhs.Length()
|
||||
}
|
||||
lLen := 0
|
||||
if lhs != nil {
|
||||
lLen = lhs.Length()
|
||||
}
|
||||
out := make([]interface{}, 0, int(math.Max(float64(rLen), float64(lLen))))
|
||||
|
||||
// TODO: ordering is totally wrong.
|
||||
// TODO: might as well make the map order work the same way.
|
||||
|
||||
// This is a cheap hack to at least make the output order stable.
|
||||
rhsOrder := []fieldpath.PathElement{}
|
||||
rhsOrder := make([]fieldpath.PathElement, 0, rLen)
|
||||
|
||||
// First, collect all RHS children.
|
||||
var observedRHS fieldpath.PathElementValueMap
|
||||
observedRHS := fieldpath.MakePathElementValueMap(rLen)
|
||||
if rhs != nil {
|
||||
observedRHS = fieldpath.MakePathElementValueMap(len(rhs.Items))
|
||||
for i, child := range rhs.Items {
|
||||
pe, err := listItemToPathElement(t, i, child)
|
||||
for i := 0; i < rhs.Length(); i++ {
|
||||
child := rhs.At(i)
|
||||
pe, err := listItemToPathElement(w.allocator, t, i, child)
|
||||
if err != nil {
|
||||
errs = append(errs, w.errorf("rhs: element %v: %v", i, err.Error())...)
|
||||
errs = append(errs, errorf("rhs: element %v: %v", i, err.Error())...)
|
||||
// If we can't construct the path element, we can't
|
||||
// even report errors deeper in the schema, so bail on
|
||||
// this element.
|
||||
continue
|
||||
}
|
||||
if _, ok := observedRHS.Get(pe); ok {
|
||||
errs = append(errs, w.errorf("rhs: duplicate entries for key %v", pe.String())...)
|
||||
errs = append(errs, errorf("rhs: duplicate entries for key %v", pe.String())...)
|
||||
}
|
||||
observedRHS.Insert(pe, child)
|
||||
rhsOrder = append(rhsOrder, pe)
|
||||
|
|
@ -189,32 +200,31 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs *value.List) (er
|
|||
}
|
||||
|
||||
// Then merge with LHS children.
|
||||
var observedLHS fieldpath.PathElementSet
|
||||
observedLHS := fieldpath.MakePathElementSet(lLen)
|
||||
if lhs != nil {
|
||||
observedLHS = fieldpath.MakePathElementSet(len(lhs.Items))
|
||||
for i, child := range lhs.Items {
|
||||
pe, err := listItemToPathElement(t, i, child)
|
||||
for i := 0; i < lhs.Length(); i++ {
|
||||
child := lhs.At(i)
|
||||
pe, err := listItemToPathElement(w.allocator, t, i, child)
|
||||
if err != nil {
|
||||
errs = append(errs, w.errorf("lhs: element %v: %v", i, err.Error())...)
|
||||
errs = append(errs, errorf("lhs: element %v: %v", i, err.Error())...)
|
||||
// If we can't construct the path element, we can't
|
||||
// even report errors deeper in the schema, so bail on
|
||||
// this element.
|
||||
continue
|
||||
}
|
||||
if observedLHS.Has(pe) {
|
||||
errs = append(errs, w.errorf("lhs: duplicate entries for key %v", pe.String())...)
|
||||
errs = append(errs, errorf("lhs: duplicate entries for key %v", pe.String())...)
|
||||
continue
|
||||
}
|
||||
observedLHS.Insert(pe)
|
||||
w2 := w.prepareDescent(pe, t.ElementType)
|
||||
w2.lhs = &child
|
||||
w2.lhs = value.Value(child)
|
||||
if rchild, ok := observedRHS.Get(pe); ok {
|
||||
w2.rhs = &rchild
|
||||
w2.rhs = rchild
|
||||
}
|
||||
if newErrs := w2.merge(); len(newErrs) > 0 {
|
||||
errs = append(errs, newErrs...)
|
||||
} else if w2.out != nil {
|
||||
out.Items = append(out.Items, *w2.out)
|
||||
errs = append(errs, w2.merge(pe.String)...)
|
||||
if w2.out != nil {
|
||||
out = append(out, *w2.out)
|
||||
}
|
||||
w.finishDescent(w2)
|
||||
}
|
||||
|
|
@ -226,45 +236,47 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs *value.List) (er
|
|||
}
|
||||
value, _ := observedRHS.Get(pe)
|
||||
w2 := w.prepareDescent(pe, t.ElementType)
|
||||
w2.rhs = &value
|
||||
if newErrs := w2.merge(); len(newErrs) > 0 {
|
||||
errs = append(errs, newErrs...)
|
||||
} else if w2.out != nil {
|
||||
out.Items = append(out.Items, *w2.out)
|
||||
w2.rhs = value
|
||||
errs = append(errs, w2.merge(pe.String)...)
|
||||
if w2.out != nil {
|
||||
out = append(out, *w2.out)
|
||||
}
|
||||
w.finishDescent(w2)
|
||||
}
|
||||
|
||||
if len(out.Items) > 0 {
|
||||
w.out = &value.Value{ListValue: out}
|
||||
if len(out) > 0 {
|
||||
i := interface{}(out)
|
||||
w.out = &i
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (w *mergingWalker) derefList(prefix string, v *value.Value, dest **value.List) (errs ValidationErrors) {
|
||||
// taking dest as input so that it can be called as a one-liner with
|
||||
// append.
|
||||
func (w *mergingWalker) derefList(prefix string, v value.Value) (value.List, ValidationErrors) {
|
||||
if v == nil {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
l, err := listValue(*v)
|
||||
l, err := listValue(w.allocator, v)
|
||||
if err != nil {
|
||||
return w.prefixError(prefix, err)
|
||||
return nil, errorf("%v: %v", prefix, err)
|
||||
}
|
||||
*dest = l
|
||||
return nil
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (w *mergingWalker) doList(t *schema.List) (errs ValidationErrors) {
|
||||
var lhs, rhs *value.List
|
||||
w.derefList("lhs: ", w.lhs, &lhs)
|
||||
w.derefList("rhs: ", w.rhs, &rhs)
|
||||
lhs, _ := w.derefList("lhs: ", w.lhs)
|
||||
if lhs != nil {
|
||||
defer w.allocator.Free(lhs)
|
||||
}
|
||||
rhs, _ := w.derefList("rhs: ", w.rhs)
|
||||
if rhs != nil {
|
||||
defer w.allocator.Free(rhs)
|
||||
}
|
||||
|
||||
// If both lhs and rhs are empty/null, treat it as a
|
||||
// leaf: this helps preserve the empty/null
|
||||
// distinction.
|
||||
emptyPromoteToLeaf := (lhs == nil || len(lhs.Items) == 0) &&
|
||||
(rhs == nil || len(rhs.Items) == 0)
|
||||
emptyPromoteToLeaf := (lhs == nil || lhs.Length() == 0) && (rhs == nil || rhs.Length() == 0)
|
||||
|
||||
if t.ElementRelationship == schema.Atomic || emptyPromoteToLeaf {
|
||||
w.doLeaf()
|
||||
|
|
@ -280,72 +292,51 @@ func (w *mergingWalker) doList(t *schema.List) (errs ValidationErrors) {
|
|||
return errs
|
||||
}
|
||||
|
||||
func (w *mergingWalker) visitMapItems(t *schema.Map, lhs, rhs *value.Map) (errs ValidationErrors) {
|
||||
out := &value.Map{}
|
||||
func (w *mergingWalker) visitMapItem(t *schema.Map, out map[string]interface{}, key string, lhs, rhs value.Value) (errs ValidationErrors) {
|
||||
fieldType := t.ElementType
|
||||
if sf, ok := t.FindField(key); ok {
|
||||
fieldType = sf.Type
|
||||
}
|
||||
pe := fieldpath.PathElement{FieldName: &key}
|
||||
w2 := w.prepareDescent(pe, fieldType)
|
||||
w2.lhs = lhs
|
||||
w2.rhs = rhs
|
||||
errs = append(errs, w2.merge(pe.String)...)
|
||||
if w2.out != nil {
|
||||
out[key] = *w2.out
|
||||
}
|
||||
w.finishDescent(w2)
|
||||
return errs
|
||||
}
|
||||
|
||||
if lhs != nil {
|
||||
for i := range lhs.Items {
|
||||
litem := &lhs.Items[i]
|
||||
fieldType := t.ElementType
|
||||
if sf, ok := t.FindField(litem.Name); ok {
|
||||
fieldType = sf.Type
|
||||
}
|
||||
w2 := w.prepareDescent(fieldpath.PathElement{FieldName: &litem.Name}, fieldType)
|
||||
w2.lhs = &litem.Value
|
||||
if rhs != nil {
|
||||
if ritem, ok := rhs.Get(litem.Name); ok {
|
||||
w2.rhs = &ritem.Value
|
||||
}
|
||||
}
|
||||
if newErrs := w2.merge(); len(newErrs) > 0 {
|
||||
errs = append(errs, newErrs...)
|
||||
} else if w2.out != nil {
|
||||
out.Items = append(out.Items, value.Field{litem.Name, *w2.out})
|
||||
}
|
||||
w.finishDescent(w2)
|
||||
}
|
||||
func (w *mergingWalker) visitMapItems(t *schema.Map, lhs, rhs value.Map) (errs ValidationErrors) {
|
||||
out := map[string]interface{}{}
|
||||
|
||||
value.MapZipUsing(w.allocator, lhs, rhs, value.Unordered, func(key string, lhsValue, rhsValue value.Value) bool {
|
||||
errs = append(errs, w.visitMapItem(t, out, key, lhsValue, rhsValue)...)
|
||||
return true
|
||||
})
|
||||
if len(out) > 0 {
|
||||
i := interface{}(out)
|
||||
w.out = &i
|
||||
}
|
||||
|
||||
if rhs != nil {
|
||||
for j := range rhs.Items {
|
||||
ritem := &rhs.Items[j]
|
||||
if lhs != nil {
|
||||
if _, ok := lhs.Get(ritem.Name); ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
fieldType := t.ElementType
|
||||
if sf, ok := t.FindField(ritem.Name); ok {
|
||||
fieldType = sf.Type
|
||||
}
|
||||
w2 := w.prepareDescent(fieldpath.PathElement{FieldName: &ritem.Name}, fieldType)
|
||||
w2.rhs = &ritem.Value
|
||||
if newErrs := w2.merge(); len(newErrs) > 0 {
|
||||
errs = append(errs, newErrs...)
|
||||
} else if w2.out != nil {
|
||||
out.Items = append(out.Items, value.Field{ritem.Name, *w2.out})
|
||||
}
|
||||
w.finishDescent(w2)
|
||||
}
|
||||
}
|
||||
|
||||
if len(out.Items) > 0 {
|
||||
w.out = &value.Value{MapValue: out}
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (w *mergingWalker) doMap(t *schema.Map) (errs ValidationErrors) {
|
||||
var lhs, rhs *value.Map
|
||||
w.derefMap("lhs: ", w.lhs, &lhs)
|
||||
w.derefMap("rhs: ", w.rhs, &rhs)
|
||||
|
||||
lhs, _ := w.derefMap("lhs: ", w.lhs)
|
||||
if lhs != nil {
|
||||
defer w.allocator.Free(lhs)
|
||||
}
|
||||
rhs, _ := w.derefMap("rhs: ", w.rhs)
|
||||
if rhs != nil {
|
||||
defer w.allocator.Free(rhs)
|
||||
}
|
||||
// If both lhs and rhs are empty/null, treat it as a
|
||||
// leaf: this helps preserve the empty/null
|
||||
// distinction.
|
||||
emptyPromoteToLeaf := (lhs == nil || len(lhs.Items) == 0) &&
|
||||
(rhs == nil || len(rhs.Items) == 0)
|
||||
emptyPromoteToLeaf := (lhs == nil || lhs.Empty()) && (rhs == nil || rhs.Empty())
|
||||
|
||||
if t.ElementRelationship == schema.Atomic || emptyPromoteToLeaf {
|
||||
w.doLeaf()
|
||||
|
|
@ -20,8 +20,8 @@ import (
|
|||
"fmt"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
// YAMLObject is an object encoded in YAML.
|
||||
|
|
@ -94,19 +94,33 @@ func (p ParseableType) IsValid() bool {
|
|||
// FromYAML parses a yaml string into an object with the current schema
|
||||
// and the type "typename" or an error if validation fails.
|
||||
func (p ParseableType) FromYAML(object YAMLObject) (*TypedValue, error) {
|
||||
v, err := value.FromYAML([]byte(object))
|
||||
var v interface{}
|
||||
err := yaml.Unmarshal([]byte(object), &v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return AsTyped(v, p.Schema, p.TypeRef)
|
||||
return AsTyped(value.NewValueInterface(v), p.Schema, p.TypeRef)
|
||||
}
|
||||
|
||||
// FromUnstructured converts a go interface to a TypedValue. It will return an
|
||||
// FromUnstructured converts a go "interface{}" type, typically an
|
||||
// unstructured object in Kubernetes world, to a TypedValue. It returns an
|
||||
// error if the resulting object fails schema validation.
|
||||
// The provided interface{} must be one of: map[string]interface{},
|
||||
// map[interface{}]interface{}, []interface{}, int types, float types,
|
||||
// string or boolean. Nested interface{} must also be one of these types.
|
||||
func (p ParseableType) FromUnstructured(in interface{}) (*TypedValue, error) {
|
||||
v, err := value.FromUnstructured(in)
|
||||
return AsTyped(value.NewValueInterface(in), p.Schema, p.TypeRef)
|
||||
}
|
||||
|
||||
// FromStructured converts a go "interface{}" type, typically an structured object in
|
||||
// Kubernetes, to a TypedValue. It will return an error if the resulting object fails
|
||||
// schema validation. The provided "interface{}" value must be a pointer so that the
|
||||
// value can be modified via reflection. The provided "interface{}" may contain structs
|
||||
// and types that are converted to Values by the jsonMarshaler interface.
|
||||
func (p ParseableType) FromStructured(in interface{}) (*TypedValue, error) {
|
||||
v, err := value.NewValueReflect(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("error creating struct value reflector: %v", err)
|
||||
}
|
||||
return AsTyped(v, p.Schema, p.TypeRef)
|
||||
}
|
||||
113
vendor/sigs.k8s.io/structured-merge-diff/v3/typed/remove.go
generated
vendored
Normal file
113
vendor/sigs.k8s.io/structured-merge-diff/v3/typed/remove.go
generated
vendored
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package typed
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/structured-merge-diff/v3/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
type removingWalker struct {
|
||||
value value.Value
|
||||
out interface{}
|
||||
schema *schema.Schema
|
||||
toRemove *fieldpath.Set
|
||||
allocator value.Allocator
|
||||
}
|
||||
|
||||
func removeItemsWithSchema(val value.Value, toRemove *fieldpath.Set, schema *schema.Schema, typeRef schema.TypeRef) value.Value {
|
||||
w := &removingWalker{
|
||||
value: val,
|
||||
schema: schema,
|
||||
toRemove: toRemove,
|
||||
allocator: value.NewFreelistAllocator(),
|
||||
}
|
||||
resolveSchema(schema, typeRef, val, w)
|
||||
return value.NewValueInterface(w.out)
|
||||
}
|
||||
|
||||
func (w *removingWalker) doScalar(t *schema.Scalar) ValidationErrors {
|
||||
w.out = w.value.Unstructured()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
|
||||
l := w.value.AsListUsing(w.allocator)
|
||||
defer w.allocator.Free(l)
|
||||
// If list is null, empty, or atomic just return
|
||||
if l == nil || l.Length() == 0 || t.ElementRelationship == schema.Atomic {
|
||||
return nil
|
||||
}
|
||||
|
||||
var newItems []interface{}
|
||||
iter := l.RangeUsing(w.allocator)
|
||||
defer w.allocator.Free(iter)
|
||||
for iter.Next() {
|
||||
i, item := iter.Item()
|
||||
// Ignore error because we have already validated this list
|
||||
pe, _ := listItemToPathElement(w.allocator, t, i, item)
|
||||
path, _ := fieldpath.MakePath(pe)
|
||||
if w.toRemove.Has(path) {
|
||||
continue
|
||||
}
|
||||
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
|
||||
item = removeItemsWithSchema(item, subset, w.schema, t.ElementType)
|
||||
}
|
||||
newItems = append(newItems, item.Unstructured())
|
||||
}
|
||||
if len(newItems) > 0 {
|
||||
w.out = newItems
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *removingWalker) doMap(t *schema.Map) ValidationErrors {
|
||||
m := w.value.AsMapUsing(w.allocator)
|
||||
if m != nil {
|
||||
defer w.allocator.Free(m)
|
||||
}
|
||||
// If map is null, empty, or atomic just return
|
||||
if m == nil || m.Empty() || t.ElementRelationship == schema.Atomic {
|
||||
return nil
|
||||
}
|
||||
|
||||
fieldTypes := map[string]schema.TypeRef{}
|
||||
for _, structField := range t.Fields {
|
||||
fieldTypes[structField.Name] = structField.Type
|
||||
}
|
||||
|
||||
newMap := map[string]interface{}{}
|
||||
m.Iterate(func(k string, val value.Value) bool {
|
||||
pe := fieldpath.PathElement{FieldName: &k}
|
||||
path, _ := fieldpath.MakePath(pe)
|
||||
fieldType := t.ElementType
|
||||
if ft, ok := fieldTypes[k]; ok {
|
||||
fieldType = ft
|
||||
} else {
|
||||
if w.toRemove.Has(path) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
|
||||
val = removeItemsWithSchema(val, subset, w.schema, fieldType)
|
||||
}
|
||||
newMap[k] = val.Unstructured()
|
||||
return true
|
||||
})
|
||||
if len(newMap) > 0 {
|
||||
w.out = newMap
|
||||
}
|
||||
return nil
|
||||
}
|
||||
166
vendor/sigs.k8s.io/structured-merge-diff/v3/typed/tofieldset.go
generated
vendored
Normal file
166
vendor/sigs.k8s.io/structured-merge-diff/v3/typed/tofieldset.go
generated
vendored
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package typed
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/v3/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
var tPool = sync.Pool{
|
||||
New: func() interface{} { return &toFieldSetWalker{} },
|
||||
}
|
||||
|
||||
func (tv TypedValue) toFieldSetWalker() *toFieldSetWalker {
|
||||
v := tPool.Get().(*toFieldSetWalker)
|
||||
v.value = tv.value
|
||||
v.schema = tv.schema
|
||||
v.typeRef = tv.typeRef
|
||||
v.set = &fieldpath.Set{}
|
||||
v.allocator = value.NewFreelistAllocator()
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *toFieldSetWalker) finished() {
|
||||
v.schema = nil
|
||||
v.typeRef = schema.TypeRef{}
|
||||
v.path = nil
|
||||
v.set = nil
|
||||
tPool.Put(v)
|
||||
}
|
||||
|
||||
type toFieldSetWalker struct {
|
||||
value value.Value
|
||||
schema *schema.Schema
|
||||
typeRef schema.TypeRef
|
||||
|
||||
set *fieldpath.Set
|
||||
path fieldpath.Path
|
||||
|
||||
// Allocate only as many walkers as needed for the depth by storing them here.
|
||||
spareWalkers *[]*toFieldSetWalker
|
||||
allocator value.Allocator
|
||||
}
|
||||
|
||||
func (v *toFieldSetWalker) prepareDescent(pe fieldpath.PathElement, tr schema.TypeRef) *toFieldSetWalker {
|
||||
if v.spareWalkers == nil {
|
||||
// first descent.
|
||||
v.spareWalkers = &[]*toFieldSetWalker{}
|
||||
}
|
||||
var v2 *toFieldSetWalker
|
||||
if n := len(*v.spareWalkers); n > 0 {
|
||||
v2, *v.spareWalkers = (*v.spareWalkers)[n-1], (*v.spareWalkers)[:n-1]
|
||||
} else {
|
||||
v2 = &toFieldSetWalker{}
|
||||
}
|
||||
*v2 = *v
|
||||
v2.typeRef = tr
|
||||
v2.path = append(v2.path, pe)
|
||||
return v2
|
||||
}
|
||||
|
||||
func (v *toFieldSetWalker) finishDescent(v2 *toFieldSetWalker) {
|
||||
// if the descent caused a realloc, ensure that we reuse the buffer
|
||||
// for the next sibling.
|
||||
v.path = v2.path[:len(v2.path)-1]
|
||||
*v.spareWalkers = append(*v.spareWalkers, v2)
|
||||
}
|
||||
|
||||
func (v *toFieldSetWalker) toFieldSet() ValidationErrors {
|
||||
return resolveSchema(v.schema, v.typeRef, v.value, v)
|
||||
}
|
||||
|
||||
func (v *toFieldSetWalker) doScalar(t *schema.Scalar) ValidationErrors {
|
||||
v.set.Insert(v.path)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *toFieldSetWalker) visitListItems(t *schema.List, list value.List) (errs ValidationErrors) {
|
||||
for i := 0; i < list.Length(); i++ {
|
||||
child := list.At(i)
|
||||
pe, _ := listItemToPathElement(v.allocator, t, i, child)
|
||||
v2 := v.prepareDescent(pe, t.ElementType)
|
||||
v2.value = child
|
||||
errs = append(errs, v2.toFieldSet()...)
|
||||
|
||||
v2.set.Insert(v2.path)
|
||||
v.finishDescent(v2)
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v *toFieldSetWalker) doList(t *schema.List) (errs ValidationErrors) {
|
||||
list, _ := listValue(v.allocator, v.value)
|
||||
if list != nil {
|
||||
defer v.allocator.Free(list)
|
||||
}
|
||||
if t.ElementRelationship == schema.Atomic {
|
||||
v.set.Insert(v.path)
|
||||
return nil
|
||||
}
|
||||
|
||||
if list == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
errs = v.visitListItems(t, list)
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v *toFieldSetWalker) visitMapItems(t *schema.Map, m value.Map) (errs ValidationErrors) {
|
||||
m.Iterate(func(key string, val value.Value) bool {
|
||||
pe := fieldpath.PathElement{FieldName: &key}
|
||||
|
||||
tr := t.ElementType
|
||||
if sf, ok := t.FindField(key); ok {
|
||||
tr = sf.Type
|
||||
}
|
||||
v2 := v.prepareDescent(pe, tr)
|
||||
v2.value = val
|
||||
errs = append(errs, v2.toFieldSet()...)
|
||||
if _, ok := t.FindField(key); !ok {
|
||||
v2.set.Insert(v2.path)
|
||||
}
|
||||
v.finishDescent(v2)
|
||||
return true
|
||||
})
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v *toFieldSetWalker) doMap(t *schema.Map) (errs ValidationErrors) {
|
||||
m, _ := mapValue(v.allocator, v.value)
|
||||
if m != nil {
|
||||
defer v.allocator.Free(m)
|
||||
}
|
||||
if t.ElementRelationship == schema.Atomic {
|
||||
v.set.Insert(v.path)
|
||||
return nil
|
||||
}
|
||||
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
errs = v.visitMapItems(t, m)
|
||||
|
||||
return errs
|
||||
}
|
||||
|
|
@ -21,9 +21,9 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
// AsTyped accepts a value and a type and returns a TypedValue. 'v' must have
|
||||
|
|
@ -62,15 +62,15 @@ type TypedValue struct {
|
|||
}
|
||||
|
||||
// AsValue removes the type from the TypedValue and only keeps the value.
|
||||
func (tv TypedValue) AsValue() *value.Value {
|
||||
return &tv.value
|
||||
func (tv TypedValue) AsValue() value.Value {
|
||||
return tv.value
|
||||
}
|
||||
|
||||
// Validate returns an error with a list of every spec violation.
|
||||
func (tv TypedValue) Validate() error {
|
||||
w := tv.walker()
|
||||
defer w.finished()
|
||||
if errs := w.validate(); len(errs) != 0 {
|
||||
if errs := w.validate(nil); len(errs) != 0 {
|
||||
return errs
|
||||
}
|
||||
return nil
|
||||
|
|
@ -79,15 +79,12 @@ func (tv TypedValue) Validate() error {
|
|||
// ToFieldSet creates a set containing every leaf field and item mentioned, or
|
||||
// validation errors, if any were encountered.
|
||||
func (tv TypedValue) ToFieldSet() (*fieldpath.Set, error) {
|
||||
s := fieldpath.NewSet()
|
||||
w := tv.walker()
|
||||
w := tv.toFieldSetWalker()
|
||||
defer w.finished()
|
||||
w.leafFieldCallback = func(p fieldpath.Path) { s.Insert(p) }
|
||||
w.nodeFieldCallback = func(p fieldpath.Path) { s.Insert(p) }
|
||||
if errs := w.validate(); len(errs) != 0 {
|
||||
if errs := w.toFieldSet(); len(errs) != 0 {
|
||||
return nil, errs
|
||||
}
|
||||
return s, nil
|
||||
return w.set, nil
|
||||
}
|
||||
|
||||
// Merge returns the result of merging tv and pso ("partially specified
|
||||
|
|
@ -122,7 +119,7 @@ func (tv TypedValue) Compare(rhs *TypedValue) (c *Comparison, err error) {
|
|||
c.Added.Insert(w.path)
|
||||
} else if w.rhs == nil {
|
||||
c.Removed.Insert(w.path)
|
||||
} else if !w.rhs.Equals(*w.lhs) {
|
||||
} else if !value.Equals(w.rhs, w.lhs) {
|
||||
// TODO: Equality is not sufficient for this.
|
||||
// Need to implement equality check on the value type.
|
||||
c.Modified.Insert(w.path)
|
||||
|
|
@ -143,8 +140,7 @@ func (tv TypedValue) Compare(rhs *TypedValue) (c *Comparison, err error) {
|
|||
|
||||
// RemoveItems removes each provided list or map item from the value.
|
||||
func (tv TypedValue) RemoveItems(items *fieldpath.Set) *TypedValue {
|
||||
tv.value, _ = value.FromUnstructured(tv.value.ToUnstructured(true))
|
||||
removeItemsWithSchema(&tv.value, items, tv.schema, tv.typeRef)
|
||||
tv.value = removeItemsWithSchema(tv.value, items, tv.schema, tv.typeRef)
|
||||
return &tv
|
||||
}
|
||||
|
||||
|
|
@ -162,11 +158,11 @@ func (tv TypedValue) NormalizeUnions(new *TypedValue) (*TypedValue, error) {
|
|||
var errs ValidationErrors
|
||||
var normalizeFn = func(w *mergingWalker) {
|
||||
if w.rhs != nil {
|
||||
v := *w.rhs
|
||||
v := w.rhs.Unstructured()
|
||||
w.out = &v
|
||||
}
|
||||
if err := normalizeUnions(w); err != nil {
|
||||
errs = append(errs, w.error(err)...)
|
||||
errs = append(errs, errorf(err.Error())...)
|
||||
}
|
||||
}
|
||||
out, mergeErrs := merge(&tv, new, func(w *mergingWalker) {}, normalizeFn)
|
||||
|
|
@ -188,11 +184,11 @@ func (tv TypedValue) NormalizeUnionsApply(new *TypedValue) (*TypedValue, error)
|
|||
var errs ValidationErrors
|
||||
var normalizeFn = func(w *mergingWalker) {
|
||||
if w.rhs != nil {
|
||||
v := *w.rhs
|
||||
v := w.rhs.Unstructured()
|
||||
w.out = &v
|
||||
}
|
||||
if err := normalizeUnionsApply(w); err != nil {
|
||||
errs = append(errs, w.error(err)...)
|
||||
errs = append(errs, errorf(err.Error())...)
|
||||
}
|
||||
}
|
||||
out, mergeErrs := merge(&tv, new, func(w *mergingWalker) {}, normalizeFn)
|
||||
|
|
@ -206,7 +202,7 @@ func (tv TypedValue) NormalizeUnionsApply(new *TypedValue) (*TypedValue, error)
|
|||
}
|
||||
|
||||
func (tv TypedValue) Empty() *TypedValue {
|
||||
tv.value = value.Value{Null: true}
|
||||
tv.value = value.NewValueInterface(nil)
|
||||
return &tv
|
||||
}
|
||||
|
||||
|
|
@ -216,12 +212,10 @@ var mwPool = sync.Pool{
|
|||
|
||||
func merge(lhs, rhs *TypedValue, rule, postRule mergeRule) (*TypedValue, error) {
|
||||
if lhs.schema != rhs.schema {
|
||||
return nil, errorFormatter{}.
|
||||
errorf("expected objects with types from the same schema")
|
||||
return nil, errorf("expected objects with types from the same schema")
|
||||
}
|
||||
if !lhs.typeRef.Equals(rhs.typeRef) {
|
||||
return nil, errorFormatter{}.
|
||||
errorf("expected objects of the same type, but got %v and %v", lhs.typeRef, rhs.typeRef)
|
||||
if !lhs.typeRef.Equals(&rhs.typeRef) {
|
||||
return nil, errorf("expected objects of the same type, but got %v and %v", lhs.typeRef, rhs.typeRef)
|
||||
}
|
||||
|
||||
mw := mwPool.Get().(*mergingWalker)
|
||||
|
|
@ -238,14 +232,17 @@ func merge(lhs, rhs *TypedValue, rule, postRule mergeRule) (*TypedValue, error)
|
|||
mwPool.Put(mw)
|
||||
}()
|
||||
|
||||
mw.lhs = &lhs.value
|
||||
mw.rhs = &rhs.value
|
||||
mw.lhs = lhs.value
|
||||
mw.rhs = rhs.value
|
||||
mw.schema = lhs.schema
|
||||
mw.typeRef = lhs.typeRef
|
||||
mw.rule = rule
|
||||
mw.postItemHook = postRule
|
||||
if mw.allocator == nil {
|
||||
mw.allocator = value.NewFreelistAllocator()
|
||||
}
|
||||
|
||||
errs := mw.merge()
|
||||
errs := mw.merge(nil)
|
||||
if len(errs) > 0 {
|
||||
return nil, errs
|
||||
}
|
||||
|
|
@ -254,10 +251,8 @@ func merge(lhs, rhs *TypedValue, rule, postRule mergeRule) (*TypedValue, error)
|
|||
schema: lhs.schema,
|
||||
typeRef: lhs.typeRef,
|
||||
}
|
||||
if mw.out == nil {
|
||||
out.value = value.Value{Null: true}
|
||||
} else {
|
||||
out.value = *mw.out
|
||||
if mw.out != nil {
|
||||
out.value = value.NewValueInterface(*mw.out)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
|
@ -20,8 +20,8 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/value"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
func normalizeUnions(w *mergingWalker) error {
|
||||
|
|
@ -34,12 +34,12 @@ func normalizeUnions(w *mergingWalker) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
old := &value.Map{}
|
||||
if w.lhs != nil {
|
||||
old = w.lhs.MapValue
|
||||
var old value.Map
|
||||
if w.lhs != nil && !w.lhs.IsNull() {
|
||||
old = w.lhs.AsMap()
|
||||
}
|
||||
for _, union := range atom.Map.Unions {
|
||||
if err := newUnion(&union).Normalize(old, w.rhs.MapValue, w.out.MapValue); err != nil {
|
||||
if err := newUnion(&union).Normalize(old, w.rhs.AsMap(), value.NewValueInterface(*w.out).AsMap()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
@ -56,14 +56,17 @@ func normalizeUnionsApply(w *mergingWalker) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
old := &value.Map{}
|
||||
if w.lhs != nil {
|
||||
old = w.lhs.MapValue
|
||||
var old value.Map
|
||||
if w.lhs != nil && !w.lhs.IsNull() {
|
||||
old = w.lhs.AsMap()
|
||||
}
|
||||
|
||||
for _, union := range atom.Map.Unions {
|
||||
if err := newUnion(&union).NormalizeApply(old, w.rhs.MapValue, w.out.MapValue); err != nil {
|
||||
out := value.NewValueInterface(*w.out)
|
||||
if err := newUnion(&union).NormalizeApply(old, w.rhs.AsMap(), out.AsMap()); err != nil {
|
||||
return err
|
||||
}
|
||||
*w.out = out.Unstructured()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -105,38 +108,38 @@ type discriminator struct {
|
|||
name string
|
||||
}
|
||||
|
||||
func (d *discriminator) Set(m *value.Map, v discriminated) {
|
||||
func (d *discriminator) Set(m value.Map, v discriminated) {
|
||||
if d == nil {
|
||||
return
|
||||
}
|
||||
m.Set(d.name, value.StringValue(string(v)))
|
||||
m.Set(d.name, value.NewValueInterface(string(v)))
|
||||
}
|
||||
|
||||
func (d *discriminator) Get(m *value.Map) discriminated {
|
||||
func (d *discriminator) Get(m value.Map) discriminated {
|
||||
if d == nil || m == nil {
|
||||
return ""
|
||||
}
|
||||
f, ok := m.Get(d.name)
|
||||
val, ok := m.Get(d.name)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if f.Value.StringValue == nil {
|
||||
if !val.IsString() {
|
||||
return ""
|
||||
}
|
||||
return discriminated(*f.Value.StringValue)
|
||||
return discriminated(val.AsString())
|
||||
}
|
||||
|
||||
type fieldsSet map[field]struct{}
|
||||
|
||||
// newFieldsSet returns a map of the fields that are part of the union and are set
|
||||
// in the given map.
|
||||
func newFieldsSet(m *value.Map, fields []field) fieldsSet {
|
||||
func newFieldsSet(m value.Map, fields []field) fieldsSet {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
set := fieldsSet{}
|
||||
for _, f := range fields {
|
||||
if subField, ok := m.Get(string(f)); ok && !subField.Value.Null {
|
||||
if subField, ok := m.Get(string(f)); ok && !subField.IsNull() {
|
||||
set.Add(f)
|
||||
}
|
||||
}
|
||||
|
|
@ -212,7 +215,7 @@ func newUnion(su *schema.Union) *union {
|
|||
|
||||
// clear removes all the fields in map that are part of the union, but
|
||||
// the one we decided to keep.
|
||||
func (u *union) clear(m *value.Map, f field) {
|
||||
func (u *union) clear(m value.Map, f field) {
|
||||
for _, fieldName := range u.f {
|
||||
if field(fieldName) != f {
|
||||
m.Delete(string(fieldName))
|
||||
|
|
@ -220,7 +223,7 @@ func (u *union) clear(m *value.Map, f field) {
|
|||
}
|
||||
}
|
||||
|
||||
func (u *union) Normalize(old, new, out *value.Map) error {
|
||||
func (u *union) Normalize(old, new, out value.Map) error {
|
||||
os := newFieldsSet(old, u.f)
|
||||
ns := newFieldsSet(new, u.f)
|
||||
diff := ns.Difference(os)
|
||||
|
|
@ -240,7 +243,7 @@ func (u *union) Normalize(old, new, out *value.Map) error {
|
|||
return fmt.Errorf("multiple fields set without discriminator change: %v", ns)
|
||||
}
|
||||
|
||||
// Update discriminiator if it needs to be deduced.
|
||||
// Set discriminiator if it needs to be deduced.
|
||||
if u.deduceInvalidDiscriminator && len(ns) == 1 {
|
||||
u.d.Set(out, u.dn.toDiscriminated(*ns.One()))
|
||||
}
|
||||
|
|
@ -248,7 +251,7 @@ func (u *union) Normalize(old, new, out *value.Map) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (u *union) NormalizeApply(applied, merged, out *value.Map) error {
|
||||
func (u *union) NormalizeApply(applied, merged, out value.Map) error {
|
||||
as := newFieldsSet(applied, u.f)
|
||||
if len(as) > 1 {
|
||||
return fmt.Errorf("more than one field of union applied: %v", as)
|
||||
195
vendor/sigs.k8s.io/structured-merge-diff/v3/typed/validate.go
generated
vendored
Normal file
195
vendor/sigs.k8s.io/structured-merge-diff/v3/typed/validate.go
generated
vendored
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package typed
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"sigs.k8s.io/structured-merge-diff/v3/fieldpath"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/schema"
|
||||
"sigs.k8s.io/structured-merge-diff/v3/value"
|
||||
)
|
||||
|
||||
var vPool = sync.Pool{
|
||||
New: func() interface{} { return &validatingObjectWalker{} },
|
||||
}
|
||||
|
||||
func (tv TypedValue) walker() *validatingObjectWalker {
|
||||
v := vPool.Get().(*validatingObjectWalker)
|
||||
v.value = tv.value
|
||||
v.schema = tv.schema
|
||||
v.typeRef = tv.typeRef
|
||||
if v.allocator == nil {
|
||||
v.allocator = value.NewFreelistAllocator()
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) finished() {
|
||||
v.schema = nil
|
||||
v.typeRef = schema.TypeRef{}
|
||||
vPool.Put(v)
|
||||
}
|
||||
|
||||
type validatingObjectWalker struct {
|
||||
value value.Value
|
||||
schema *schema.Schema
|
||||
typeRef schema.TypeRef
|
||||
|
||||
// Allocate only as many walkers as needed for the depth by storing them here.
|
||||
spareWalkers *[]*validatingObjectWalker
|
||||
allocator value.Allocator
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) prepareDescent(tr schema.TypeRef) *validatingObjectWalker {
|
||||
if v.spareWalkers == nil {
|
||||
// first descent.
|
||||
v.spareWalkers = &[]*validatingObjectWalker{}
|
||||
}
|
||||
var v2 *validatingObjectWalker
|
||||
if n := len(*v.spareWalkers); n > 0 {
|
||||
v2, *v.spareWalkers = (*v.spareWalkers)[n-1], (*v.spareWalkers)[:n-1]
|
||||
} else {
|
||||
v2 = &validatingObjectWalker{}
|
||||
}
|
||||
*v2 = *v
|
||||
v2.typeRef = tr
|
||||
return v2
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) finishDescent(v2 *validatingObjectWalker) {
|
||||
// if the descent caused a realloc, ensure that we reuse the buffer
|
||||
// for the next sibling.
|
||||
*v.spareWalkers = append(*v.spareWalkers, v2)
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) validate(prefixFn func() string) ValidationErrors {
|
||||
return resolveSchema(v.schema, v.typeRef, v.value, v).WithLazyPrefix(prefixFn)
|
||||
}
|
||||
|
||||
func validateScalar(t *schema.Scalar, v value.Value, prefix string) (errs ValidationErrors) {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
if v.IsNull() {
|
||||
return nil
|
||||
}
|
||||
switch *t {
|
||||
case schema.Numeric:
|
||||
if !v.IsFloat() && !v.IsInt() {
|
||||
// TODO: should the schema separate int and float?
|
||||
return errorf("%vexpected numeric (int or float), got %T", prefix, v)
|
||||
}
|
||||
case schema.String:
|
||||
if !v.IsString() {
|
||||
return errorf("%vexpected string, got %#v", prefix, v)
|
||||
}
|
||||
case schema.Boolean:
|
||||
if !v.IsBool() {
|
||||
return errorf("%vexpected boolean, got %v", prefix, v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) doScalar(t *schema.Scalar) ValidationErrors {
|
||||
if errs := validateScalar(t, v.value, ""); len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) visitListItems(t *schema.List, list value.List) (errs ValidationErrors) {
|
||||
observedKeys := fieldpath.MakePathElementSet(list.Length())
|
||||
for i := 0; i < list.Length(); i++ {
|
||||
child := list.AtUsing(v.allocator, i)
|
||||
defer v.allocator.Free(child)
|
||||
var pe fieldpath.PathElement
|
||||
if t.ElementRelationship != schema.Associative {
|
||||
pe.Index = &i
|
||||
} else {
|
||||
var err error
|
||||
pe, err = listItemToPathElement(v.allocator, t, i, child)
|
||||
if err != nil {
|
||||
errs = append(errs, errorf("element %v: %v", i, err.Error())...)
|
||||
// If we can't construct the path element, we can't
|
||||
// even report errors deeper in the schema, so bail on
|
||||
// this element.
|
||||
return
|
||||
}
|
||||
if observedKeys.Has(pe) {
|
||||
errs = append(errs, errorf("duplicate entries for key %v", pe.String())...)
|
||||
}
|
||||
observedKeys.Insert(pe)
|
||||
}
|
||||
v2 := v.prepareDescent(t.ElementType)
|
||||
v2.value = child
|
||||
errs = append(errs, v2.validate(pe.String)...)
|
||||
v.finishDescent(v2)
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) doList(t *schema.List) (errs ValidationErrors) {
|
||||
list, err := listValue(v.allocator, v.value)
|
||||
if err != nil {
|
||||
return errorf(err.Error())
|
||||
}
|
||||
|
||||
if list == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
defer v.allocator.Free(list)
|
||||
errs = v.visitListItems(t, list)
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) visitMapItems(t *schema.Map, m value.Map) (errs ValidationErrors) {
|
||||
m.IterateUsing(v.allocator, func(key string, val value.Value) bool {
|
||||
pe := fieldpath.PathElement{FieldName: &key}
|
||||
tr := t.ElementType
|
||||
if sf, ok := t.FindField(key); ok {
|
||||
tr = sf.Type
|
||||
} else if (t.ElementType == schema.TypeRef{}) {
|
||||
errs = append(errs, errorf("field not declared in schema").WithPrefix(pe.String())...)
|
||||
return false
|
||||
}
|
||||
v2 := v.prepareDescent(tr)
|
||||
v2.value = val
|
||||
// Giving pe.String as a parameter actually increases the allocations.
|
||||
errs = append(errs, v2.validate(func() string { return pe.String() })...)
|
||||
v.finishDescent(v2)
|
||||
return true
|
||||
})
|
||||
return errs
|
||||
}
|
||||
|
||||
func (v *validatingObjectWalker) doMap(t *schema.Map) (errs ValidationErrors) {
|
||||
m, err := mapValue(v.allocator, v.value)
|
||||
if err != nil {
|
||||
return errorf(err.Error())
|
||||
}
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
defer v.allocator.Free(m)
|
||||
errs = v.visitMapItems(t, m)
|
||||
|
||||
return errs
|
||||
}
|
||||
203
vendor/sigs.k8s.io/structured-merge-diff/v3/value/allocator.go
generated
vendored
Normal file
203
vendor/sigs.k8s.io/structured-merge-diff/v3/value/allocator.go
generated
vendored
Normal file
|
|
@ -0,0 +1,203 @@
|
|||
/*
|
||||
Copyright 2020 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
// Allocator provides a value object allocation strategy.
|
||||
// Value objects can be allocated by passing an allocator to the "Using"
|
||||
// receiver functions on the value interfaces, e.g. Map.ZipUsing(allocator, ...).
|
||||
// Value objects returned from "Using" functions should be given back to the allocator
|
||||
// once longer needed by calling Allocator.Free(Value).
|
||||
type Allocator interface {
|
||||
// Free gives the allocator back any value objects returned by the "Using"
|
||||
// receiver functions on the value interfaces.
|
||||
// interface{} may be any of: Value, Map, List or Range.
|
||||
Free(interface{})
|
||||
|
||||
// The unexported functions are for "Using" receiver functions of the value types
|
||||
// to request what they need from the allocator.
|
||||
allocValueUnstructured() *valueUnstructured
|
||||
allocListUnstructuredRange() *listUnstructuredRange
|
||||
allocValueReflect() *valueReflect
|
||||
allocMapReflect() *mapReflect
|
||||
allocStructReflect() *structReflect
|
||||
allocListReflect() *listReflect
|
||||
allocListReflectRange() *listReflectRange
|
||||
}
|
||||
|
||||
// HeapAllocator simply allocates objects to the heap. It is the default
|
||||
// allocator used receiver functions on the value interfaces that do not accept
|
||||
// an allocator and should be used whenever allocating objects that will not
|
||||
// be given back to an allocator by calling Allocator.Free(Value).
|
||||
var HeapAllocator = &heapAllocator{}
|
||||
|
||||
type heapAllocator struct{}
|
||||
|
||||
func (p *heapAllocator) allocValueUnstructured() *valueUnstructured {
|
||||
return &valueUnstructured{}
|
||||
}
|
||||
|
||||
func (p *heapAllocator) allocListUnstructuredRange() *listUnstructuredRange {
|
||||
return &listUnstructuredRange{vv: &valueUnstructured{}}
|
||||
}
|
||||
|
||||
func (p *heapAllocator) allocValueReflect() *valueReflect {
|
||||
return &valueReflect{}
|
||||
}
|
||||
|
||||
func (p *heapAllocator) allocStructReflect() *structReflect {
|
||||
return &structReflect{}
|
||||
}
|
||||
|
||||
func (p *heapAllocator) allocMapReflect() *mapReflect {
|
||||
return &mapReflect{}
|
||||
}
|
||||
|
||||
func (p *heapAllocator) allocListReflect() *listReflect {
|
||||
return &listReflect{}
|
||||
}
|
||||
|
||||
func (p *heapAllocator) allocListReflectRange() *listReflectRange {
|
||||
return &listReflectRange{vr: &valueReflect{}}
|
||||
}
|
||||
|
||||
func (p *heapAllocator) Free(_ interface{}) {}
|
||||
|
||||
// NewFreelistAllocator creates freelist based allocator.
|
||||
// This allocator provides fast allocation and freeing of short lived value objects.
|
||||
//
|
||||
// The freelists are bounded in size by freelistMaxSize. If more than this amount of value objects is
|
||||
// allocated at once, the excess will be returned to the heap for garbage collection when freed.
|
||||
//
|
||||
// This allocator is unsafe and must not be accessed concurrently by goroutines.
|
||||
//
|
||||
// This allocator works well for traversal of value data trees. Typical usage is to acquire
|
||||
// a freelist at the beginning of the traversal and use it through out
|
||||
// for all temporary value access.
|
||||
func NewFreelistAllocator() Allocator {
|
||||
return &freelistAllocator{
|
||||
valueUnstructured: &freelist{new: func() interface{} {
|
||||
return &valueUnstructured{}
|
||||
}},
|
||||
listUnstructuredRange: &freelist{new: func() interface{} {
|
||||
return &listUnstructuredRange{vv: &valueUnstructured{}}
|
||||
}},
|
||||
valueReflect: &freelist{new: func() interface{} {
|
||||
return &valueReflect{}
|
||||
}},
|
||||
mapReflect: &freelist{new: func() interface{} {
|
||||
return &mapReflect{}
|
||||
}},
|
||||
structReflect: &freelist{new: func() interface{} {
|
||||
return &structReflect{}
|
||||
}},
|
||||
listReflect: &freelist{new: func() interface{} {
|
||||
return &listReflect{}
|
||||
}},
|
||||
listReflectRange: &freelist{new: func() interface{} {
|
||||
return &listReflectRange{vr: &valueReflect{}}
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
// Bound memory usage of freelists. This prevents the processing of very large lists from leaking memory.
|
||||
// This limit is large enough for endpoints objects containing 1000 IP address entries. Freed objects
|
||||
// that don't fit into the freelist are orphaned on the heap to be garbage collected.
|
||||
const freelistMaxSize = 1000
|
||||
|
||||
type freelistAllocator struct {
|
||||
valueUnstructured *freelist
|
||||
listUnstructuredRange *freelist
|
||||
valueReflect *freelist
|
||||
mapReflect *freelist
|
||||
structReflect *freelist
|
||||
listReflect *freelist
|
||||
listReflectRange *freelist
|
||||
}
|
||||
|
||||
type freelist struct {
|
||||
list []interface{}
|
||||
new func() interface{}
|
||||
}
|
||||
|
||||
func (f *freelist) allocate() interface{} {
|
||||
var w2 interface{}
|
||||
if n := len(f.list); n > 0 {
|
||||
w2, f.list = f.list[n-1], f.list[:n-1]
|
||||
} else {
|
||||
w2 = f.new()
|
||||
}
|
||||
return w2
|
||||
}
|
||||
|
||||
func (f *freelist) free(v interface{}) {
|
||||
if len(f.list) < freelistMaxSize {
|
||||
f.list = append(f.list, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *freelistAllocator) Free(value interface{}) {
|
||||
switch v := value.(type) {
|
||||
case *valueUnstructured:
|
||||
v.Value = nil // don't hold references to unstructured objects
|
||||
w.valueUnstructured.free(v)
|
||||
case *listUnstructuredRange:
|
||||
v.vv.Value = nil // don't hold references to unstructured objects
|
||||
w.listUnstructuredRange.free(v)
|
||||
case *valueReflect:
|
||||
v.ParentMapKey = nil
|
||||
v.ParentMap = nil
|
||||
w.valueReflect.free(v)
|
||||
case *mapReflect:
|
||||
w.mapReflect.free(v)
|
||||
case *structReflect:
|
||||
w.structReflect.free(v)
|
||||
case *listReflect:
|
||||
w.listReflect.free(v)
|
||||
case *listReflectRange:
|
||||
v.vr.ParentMapKey = nil
|
||||
v.vr.ParentMap = nil
|
||||
w.listReflectRange.free(v)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *freelistAllocator) allocValueUnstructured() *valueUnstructured {
|
||||
return w.valueUnstructured.allocate().(*valueUnstructured)
|
||||
}
|
||||
|
||||
func (w *freelistAllocator) allocListUnstructuredRange() *listUnstructuredRange {
|
||||
return w.listUnstructuredRange.allocate().(*listUnstructuredRange)
|
||||
}
|
||||
|
||||
func (w *freelistAllocator) allocValueReflect() *valueReflect {
|
||||
return w.valueReflect.allocate().(*valueReflect)
|
||||
}
|
||||
|
||||
func (w *freelistAllocator) allocStructReflect() *structReflect {
|
||||
return w.structReflect.allocate().(*structReflect)
|
||||
}
|
||||
|
||||
func (w *freelistAllocator) allocMapReflect() *mapReflect {
|
||||
return w.mapReflect.allocate().(*mapReflect)
|
||||
}
|
||||
|
||||
func (w *freelistAllocator) allocListReflect() *listReflect {
|
||||
return w.listReflect.allocate().(*listReflect)
|
||||
}
|
||||
|
||||
func (w *freelistAllocator) allocListReflectRange() *listReflectRange {
|
||||
return w.listReflectRange.allocate().(*listReflectRange)
|
||||
}
|
||||
97
vendor/sigs.k8s.io/structured-merge-diff/v3/value/fields.go
generated
vendored
Normal file
97
vendor/sigs.k8s.io/structured-merge-diff/v3/value/fields.go
generated
vendored
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Field is an individual key-value pair.
|
||||
type Field struct {
|
||||
Name string
|
||||
Value Value
|
||||
}
|
||||
|
||||
// FieldList is a list of key-value pairs. Each field is expected to
|
||||
// have a different name.
|
||||
type FieldList []Field
|
||||
|
||||
// Sort sorts the field list by Name.
|
||||
func (f FieldList) Sort() {
|
||||
if len(f) < 2 {
|
||||
return
|
||||
}
|
||||
if len(f) == 2 {
|
||||
if f[1].Name < f[0].Name {
|
||||
f[0], f[1] = f[1], f[0]
|
||||
}
|
||||
return
|
||||
}
|
||||
sort.SliceStable(f, func(i, j int) bool {
|
||||
return f[i].Name < f[j].Name
|
||||
})
|
||||
}
|
||||
|
||||
// Less compares two lists lexically.
|
||||
func (f FieldList) Less(rhs FieldList) bool {
|
||||
return f.Compare(rhs) == -1
|
||||
}
|
||||
|
||||
// Compare compares two lists lexically. The result will be 0 if f==rhs, -1
|
||||
// if f < rhs, and +1 if f > rhs.
|
||||
func (f FieldList) Compare(rhs FieldList) int {
|
||||
i := 0
|
||||
for {
|
||||
if i >= len(f) && i >= len(rhs) {
|
||||
// Maps are the same length and all items are equal.
|
||||
return 0
|
||||
}
|
||||
if i >= len(f) {
|
||||
// F is shorter.
|
||||
return -1
|
||||
}
|
||||
if i >= len(rhs) {
|
||||
// RHS is shorter.
|
||||
return 1
|
||||
}
|
||||
if c := strings.Compare(f[i].Name, rhs[i].Name); c != 0 {
|
||||
return c
|
||||
}
|
||||
if c := Compare(f[i].Value, rhs[i].Value); c != 0 {
|
||||
return c
|
||||
}
|
||||
// The items are equal; continue.
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
// Equals returns true if the two fieldslist are equals, false otherwise.
|
||||
func (f FieldList) Equals(rhs FieldList) bool {
|
||||
if len(f) != len(rhs) {
|
||||
return false
|
||||
}
|
||||
for i := range f {
|
||||
if f[i].Name != rhs[i].Name {
|
||||
return false
|
||||
}
|
||||
if !Equals(f[i].Value, rhs[i].Value) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
91
vendor/sigs.k8s.io/structured-merge-diff/v3/value/jsontagutil.go
generated
vendored
Normal file
91
vendor/sigs.k8s.io/structured-merge-diff/v3/value/jsontagutil.go
generated
vendored
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TODO: This implements the same functionality as https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/runtime/converter.go#L236
|
||||
// but is based on the highly efficient approach from https://golang.org/src/encoding/json/encode.go
|
||||
|
||||
func lookupJsonTags(f reflect.StructField) (name string, omit bool, inline bool, omitempty bool) {
|
||||
tag := f.Tag.Get("json")
|
||||
if tag == "-" {
|
||||
return "", true, false, false
|
||||
}
|
||||
name, opts := parseTag(tag)
|
||||
if name == "" {
|
||||
name = f.Name
|
||||
}
|
||||
return name, false, opts.Contains("inline"), opts.Contains("omitempty")
|
||||
}
|
||||
|
||||
func isZero(v reflect.Value) bool {
|
||||
switch v.Kind() {
|
||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
||||
return v.Len() == 0
|
||||
case reflect.Bool:
|
||||
return !v.Bool()
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return v.Int() == 0
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return v.Uint() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return v.Float() == 0
|
||||
case reflect.Interface, reflect.Ptr:
|
||||
return v.IsNil()
|
||||
case reflect.Chan, reflect.Func:
|
||||
panic(fmt.Sprintf("unsupported type: %v", v.Type()))
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type tagOptions string
|
||||
|
||||
// parseTag splits a struct field's json tag into its name and
|
||||
// comma-separated options.
|
||||
func parseTag(tag string) (string, tagOptions) {
|
||||
if idx := strings.Index(tag, ","); idx != -1 {
|
||||
return tag[:idx], tagOptions(tag[idx+1:])
|
||||
}
|
||||
return tag, tagOptions("")
|
||||
}
|
||||
|
||||
// Contains reports whether a comma-separated list of options
|
||||
// contains a particular substr flag. substr must be surrounded by a
|
||||
// string boundary or commas.
|
||||
func (o tagOptions) Contains(optionName string) bool {
|
||||
if len(o) == 0 {
|
||||
return false
|
||||
}
|
||||
s := string(o)
|
||||
for s != "" {
|
||||
var next string
|
||||
i := strings.Index(s, ",")
|
||||
if i >= 0 {
|
||||
s, next = s[:i], s[i+1:]
|
||||
}
|
||||
if s == optionName {
|
||||
return true
|
||||
}
|
||||
s = next
|
||||
}
|
||||
return false
|
||||
}
|
||||
139
vendor/sigs.k8s.io/structured-merge-diff/v3/value/list.go
generated
vendored
Normal file
139
vendor/sigs.k8s.io/structured-merge-diff/v3/value/list.go
generated
vendored
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
// List represents a list object.
|
||||
type List interface {
|
||||
// Length returns how many items can be found in the map.
|
||||
Length() int
|
||||
// At returns the item at the given position in the map. It will
|
||||
// panic if the index is out of range.
|
||||
At(int) Value
|
||||
// AtUsing uses the provided allocator and returns the item at the given
|
||||
// position in the map. It will panic if the index is out of range.
|
||||
// The returned Value should be given back to the Allocator when no longer needed
|
||||
// by calling Allocator.Free(Value).
|
||||
AtUsing(Allocator, int) Value
|
||||
// Range returns a ListRange for iterating over the items in the list.
|
||||
Range() ListRange
|
||||
// RangeUsing uses the provided allocator and returns a ListRange for
|
||||
// iterating over the items in the list.
|
||||
// The returned Range should be given back to the Allocator when no longer needed
|
||||
// by calling Allocator.Free(Value).
|
||||
RangeUsing(Allocator) ListRange
|
||||
// Equals compares the two lists, and return true if they are the same, false otherwise.
|
||||
// Implementations can use ListEquals as a general implementation for this methods.
|
||||
Equals(List) bool
|
||||
// EqualsUsing uses the provided allocator and compares the two lists, and return true if
|
||||
// they are the same, false otherwise. Implementations can use ListEqualsUsing as a general
|
||||
// implementation for this methods.
|
||||
EqualsUsing(Allocator, List) bool
|
||||
}
|
||||
|
||||
// ListRange represents a single iteration across the items of a list.
|
||||
type ListRange interface {
|
||||
// Next increments to the next item in the range, if there is one, and returns true, or returns false if there are no more items.
|
||||
Next() bool
|
||||
// Item returns the index and value of the current item in the range. or panics if there is no current item.
|
||||
// For efficiency, Item may reuse the values returned by previous Item calls. Callers should be careful avoid holding
|
||||
// pointers to the value returned by Item() that escape the iteration loop since they become invalid once either
|
||||
// Item() or Allocator.Free() is called.
|
||||
Item() (index int, value Value)
|
||||
}
|
||||
|
||||
var EmptyRange = &emptyRange{}
|
||||
|
||||
type emptyRange struct{}
|
||||
|
||||
func (_ *emptyRange) Next() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (_ *emptyRange) Item() (index int, value Value) {
|
||||
panic("Item called on empty ListRange")
|
||||
}
|
||||
|
||||
// ListEquals compares two lists lexically.
|
||||
// WARN: This is a naive implementation, calling lhs.Equals(rhs) is typically the most efficient.
|
||||
func ListEquals(lhs, rhs List) bool {
|
||||
return ListEqualsUsing(HeapAllocator, lhs, rhs)
|
||||
}
|
||||
|
||||
// ListEqualsUsing uses the provided allocator and compares two lists lexically.
|
||||
// WARN: This is a naive implementation, calling lhs.EqualsUsing(allocator, rhs) is typically the most efficient.
|
||||
func ListEqualsUsing(a Allocator, lhs, rhs List) bool {
|
||||
if lhs.Length() != rhs.Length() {
|
||||
return false
|
||||
}
|
||||
|
||||
lhsRange := lhs.RangeUsing(a)
|
||||
defer a.Free(lhsRange)
|
||||
rhsRange := rhs.RangeUsing(a)
|
||||
defer a.Free(rhsRange)
|
||||
|
||||
for lhsRange.Next() && rhsRange.Next() {
|
||||
_, lv := lhsRange.Item()
|
||||
_, rv := rhsRange.Item()
|
||||
if !EqualsUsing(a, lv, rv) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ListLess compares two lists lexically.
|
||||
func ListLess(lhs, rhs List) bool {
|
||||
return ListCompare(lhs, rhs) == -1
|
||||
}
|
||||
|
||||
// ListCompare compares two lists lexically. The result will be 0 if l==rhs, -1
|
||||
// if l < rhs, and +1 if l > rhs.
|
||||
func ListCompare(lhs, rhs List) int {
|
||||
return ListCompareUsing(HeapAllocator, lhs, rhs)
|
||||
}
|
||||
|
||||
// ListCompareUsing uses the provided allocator and compares two lists lexically. The result will be 0 if l==rhs, -1
|
||||
// if l < rhs, and +1 if l > rhs.
|
||||
func ListCompareUsing(a Allocator, lhs, rhs List) int {
|
||||
lhsRange := lhs.RangeUsing(a)
|
||||
defer a.Free(lhsRange)
|
||||
rhsRange := rhs.RangeUsing(a)
|
||||
defer a.Free(rhsRange)
|
||||
|
||||
for {
|
||||
lhsOk := lhsRange.Next()
|
||||
rhsOk := rhsRange.Next()
|
||||
if !lhsOk && !rhsOk {
|
||||
// Lists are the same length and all items are equal.
|
||||
return 0
|
||||
}
|
||||
if !lhsOk {
|
||||
// LHS is shorter.
|
||||
return -1
|
||||
}
|
||||
if !rhsOk {
|
||||
// RHS is shorter.
|
||||
return 1
|
||||
}
|
||||
_, lv := lhsRange.Item()
|
||||
_, rv := rhsRange.Item()
|
||||
if c := CompareUsing(a, lv, rv); c != 0 {
|
||||
return c
|
||||
}
|
||||
// The items are equal; continue.
|
||||
}
|
||||
}
|
||||
98
vendor/sigs.k8s.io/structured-merge-diff/v3/value/listreflect.go
generated
vendored
Normal file
98
vendor/sigs.k8s.io/structured-merge-diff/v3/value/listreflect.go
generated
vendored
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type listReflect struct {
|
||||
Value reflect.Value
|
||||
}
|
||||
|
||||
func (r listReflect) Length() int {
|
||||
val := r.Value
|
||||
return val.Len()
|
||||
}
|
||||
|
||||
func (r listReflect) At(i int) Value {
|
||||
val := r.Value
|
||||
return mustWrapValueReflect(val.Index(i), nil, nil)
|
||||
}
|
||||
|
||||
func (r listReflect) AtUsing(a Allocator, i int) Value {
|
||||
val := r.Value
|
||||
return a.allocValueReflect().mustReuse(val.Index(i), nil, nil, nil)
|
||||
}
|
||||
|
||||
func (r listReflect) Unstructured() interface{} {
|
||||
l := r.Length()
|
||||
result := make([]interface{}, l)
|
||||
for i := 0; i < l; i++ {
|
||||
result[i] = r.At(i).Unstructured()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (r listReflect) Range() ListRange {
|
||||
return r.RangeUsing(HeapAllocator)
|
||||
}
|
||||
|
||||
func (r listReflect) RangeUsing(a Allocator) ListRange {
|
||||
length := r.Value.Len()
|
||||
if length == 0 {
|
||||
return EmptyRange
|
||||
}
|
||||
rr := a.allocListReflectRange()
|
||||
rr.list = r.Value
|
||||
rr.i = -1
|
||||
rr.entry = TypeReflectEntryOf(r.Value.Type().Elem())
|
||||
return rr
|
||||
}
|
||||
|
||||
func (r listReflect) Equals(other List) bool {
|
||||
return r.EqualsUsing(HeapAllocator, other)
|
||||
}
|
||||
func (r listReflect) EqualsUsing(a Allocator, other List) bool {
|
||||
if otherReflectList, ok := other.(*listReflect); ok {
|
||||
return reflect.DeepEqual(r.Value.Interface(), otherReflectList.Value.Interface())
|
||||
}
|
||||
return ListEqualsUsing(a, &r, other)
|
||||
}
|
||||
|
||||
type listReflectRange struct {
|
||||
list reflect.Value
|
||||
vr *valueReflect
|
||||
i int
|
||||
entry *TypeReflectCacheEntry
|
||||
}
|
||||
|
||||
func (r *listReflectRange) Next() bool {
|
||||
r.i += 1
|
||||
return r.i < r.list.Len()
|
||||
}
|
||||
|
||||
func (r *listReflectRange) Item() (index int, value Value) {
|
||||
if r.i < 0 {
|
||||
panic("Item() called before first calling Next()")
|
||||
}
|
||||
if r.i >= r.list.Len() {
|
||||
panic("Item() called on ListRange with no more items")
|
||||
}
|
||||
v := r.list.Index(r.i)
|
||||
return r.i, r.vr.mustReuse(v, r.entry, nil, nil)
|
||||
}
|
||||
74
vendor/sigs.k8s.io/structured-merge-diff/v3/value/listunstructured.go
generated
vendored
Normal file
74
vendor/sigs.k8s.io/structured-merge-diff/v3/value/listunstructured.go
generated
vendored
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
type listUnstructured []interface{}
|
||||
|
||||
func (l listUnstructured) Length() int {
|
||||
return len(l)
|
||||
}
|
||||
|
||||
func (l listUnstructured) At(i int) Value {
|
||||
return NewValueInterface(l[i])
|
||||
}
|
||||
|
||||
func (l listUnstructured) AtUsing(a Allocator, i int) Value {
|
||||
return a.allocValueUnstructured().reuse(l[i])
|
||||
}
|
||||
|
||||
func (l listUnstructured) Equals(other List) bool {
|
||||
return l.EqualsUsing(HeapAllocator, other)
|
||||
}
|
||||
|
||||
func (l listUnstructured) EqualsUsing(a Allocator, other List) bool {
|
||||
return ListEqualsUsing(a, &l, other)
|
||||
}
|
||||
|
||||
func (l listUnstructured) Range() ListRange {
|
||||
return l.RangeUsing(HeapAllocator)
|
||||
}
|
||||
|
||||
func (l listUnstructured) RangeUsing(a Allocator) ListRange {
|
||||
if len(l) == 0 {
|
||||
return EmptyRange
|
||||
}
|
||||
r := a.allocListUnstructuredRange()
|
||||
r.list = l
|
||||
r.i = -1
|
||||
return r
|
||||
}
|
||||
|
||||
type listUnstructuredRange struct {
|
||||
list listUnstructured
|
||||
vv *valueUnstructured
|
||||
i int
|
||||
}
|
||||
|
||||
func (r *listUnstructuredRange) Next() bool {
|
||||
r.i += 1
|
||||
return r.i < len(r.list)
|
||||
}
|
||||
|
||||
func (r *listUnstructuredRange) Item() (index int, value Value) {
|
||||
if r.i < 0 {
|
||||
panic("Item() called before first calling Next()")
|
||||
}
|
||||
if r.i >= len(r.list) {
|
||||
panic("Item() called on ListRange with no more items")
|
||||
}
|
||||
return r.i, r.vv.reuse(r.list[r.i])
|
||||
}
|
||||
270
vendor/sigs.k8s.io/structured-merge-diff/v3/value/map.go
generated
vendored
Normal file
270
vendor/sigs.k8s.io/structured-merge-diff/v3/value/map.go
generated
vendored
Normal file
|
|
@ -0,0 +1,270 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Map represents a Map or go structure.
|
||||
type Map interface {
|
||||
// Set changes or set the value of the given key.
|
||||
Set(key string, val Value)
|
||||
// Get returns the value for the given key, if present, or (nil, false) otherwise.
|
||||
Get(key string) (Value, bool)
|
||||
// GetUsing uses the provided allocator and returns the value for the given key,
|
||||
// if present, or (nil, false) otherwise.
|
||||
// The returned Value should be given back to the Allocator when no longer needed
|
||||
// by calling Allocator.Free(Value).
|
||||
GetUsing(a Allocator, key string) (Value, bool)
|
||||
// Has returns true if the key is present, or false otherwise.
|
||||
Has(key string) bool
|
||||
// Delete removes the key from the map.
|
||||
Delete(key string)
|
||||
// Equals compares the two maps, and return true if they are the same, false otherwise.
|
||||
// Implementations can use MapEquals as a general implementation for this methods.
|
||||
Equals(other Map) bool
|
||||
// EqualsUsing uses the provided allocator and compares the two maps, and return true if
|
||||
// they are the same, false otherwise. Implementations can use MapEqualsUsing as a general
|
||||
// implementation for this methods.
|
||||
EqualsUsing(a Allocator, other Map) bool
|
||||
// Iterate runs the given function for each key/value in the
|
||||
// map. Returning false in the closure prematurely stops the
|
||||
// iteration.
|
||||
Iterate(func(key string, value Value) bool) bool
|
||||
// IterateUsing uses the provided allocator and runs the given function for each key/value
|
||||
// in the map. Returning false in the closure prematurely stops the iteration.
|
||||
IterateUsing(Allocator, func(key string, value Value) bool) bool
|
||||
// Length returns the number of items in the map.
|
||||
Length() int
|
||||
// Empty returns true if the map is empty.
|
||||
Empty() bool
|
||||
// Zip iterates over the entries of two maps together. If both maps contain a value for a given key, fn is called
|
||||
// with the values from both maps, otherwise it is called with the value of the map that contains the key and nil
|
||||
// for the map that does not contain the key. Returning false in the closure prematurely stops the iteration.
|
||||
Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool
|
||||
// ZipUsing uses the provided allocator and iterates over the entries of two maps together. If both maps
|
||||
// contain a value for a given key, fn is called with the values from both maps, otherwise it is called with
|
||||
// the value of the map that contains the key and nil for the map that does not contain the key. Returning
|
||||
// false in the closure prematurely stops the iteration.
|
||||
ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool
|
||||
}
|
||||
|
||||
// MapTraverseOrder defines the map traversal ordering available.
|
||||
type MapTraverseOrder int
|
||||
|
||||
const (
|
||||
// Unordered indicates that the map traversal has no ordering requirement.
|
||||
Unordered = iota
|
||||
// LexicalKeyOrder indicates that the map traversal is ordered by key, lexically.
|
||||
LexicalKeyOrder
|
||||
)
|
||||
|
||||
// MapZip iterates over the entries of two maps together. If both maps contain a value for a given key, fn is called
|
||||
// with the values from both maps, otherwise it is called with the value of the map that contains the key and nil
|
||||
// for the other map. Returning false in the closure prematurely stops the iteration.
|
||||
func MapZip(lhs, rhs Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
return MapZipUsing(HeapAllocator, lhs, rhs, order, fn)
|
||||
}
|
||||
|
||||
// MapZipUsing uses the provided allocator and iterates over the entries of two maps together. If both maps
|
||||
// contain a value for a given key, fn is called with the values from both maps, otherwise it is called with
|
||||
// the value of the map that contains the key and nil for the other map. Returning false in the closure
|
||||
// prematurely stops the iteration.
|
||||
func MapZipUsing(a Allocator, lhs, rhs Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
if lhs != nil {
|
||||
return lhs.ZipUsing(a, rhs, order, fn)
|
||||
}
|
||||
if rhs != nil {
|
||||
return rhs.ZipUsing(a, lhs, order, func(key string, rhs, lhs Value) bool { // arg positions of lhs and rhs deliberately swapped
|
||||
return fn(key, lhs, rhs)
|
||||
})
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// defaultMapZip provides a default implementation of Zip for implementations that do not need to provide
|
||||
// their own optimized implementation.
|
||||
func defaultMapZip(a Allocator, lhs, rhs Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
switch order {
|
||||
case Unordered:
|
||||
return unorderedMapZip(a, lhs, rhs, fn)
|
||||
case LexicalKeyOrder:
|
||||
return lexicalKeyOrderedMapZip(a, lhs, rhs, fn)
|
||||
default:
|
||||
panic("Unsupported map order")
|
||||
}
|
||||
}
|
||||
|
||||
func unorderedMapZip(a Allocator, lhs, rhs Map, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
if (lhs == nil || lhs.Empty()) && (rhs == nil || rhs.Empty()) {
|
||||
return true
|
||||
}
|
||||
|
||||
if lhs != nil {
|
||||
ok := lhs.IterateUsing(a, func(key string, lhsValue Value) bool {
|
||||
var rhsValue Value
|
||||
if rhs != nil {
|
||||
if item, ok := rhs.GetUsing(a, key); ok {
|
||||
rhsValue = item
|
||||
defer a.Free(rhsValue)
|
||||
}
|
||||
}
|
||||
return fn(key, lhsValue, rhsValue)
|
||||
})
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if rhs != nil {
|
||||
return rhs.IterateUsing(a, func(key string, rhsValue Value) bool {
|
||||
if lhs == nil || !lhs.Has(key) {
|
||||
return fn(key, nil, rhsValue)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func lexicalKeyOrderedMapZip(a Allocator, lhs, rhs Map, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
var lhsLength, rhsLength int
|
||||
var orderedLength int // rough estimate of length of union of map keys
|
||||
if lhs != nil {
|
||||
lhsLength = lhs.Length()
|
||||
orderedLength = lhsLength
|
||||
}
|
||||
if rhs != nil {
|
||||
rhsLength = rhs.Length()
|
||||
if rhsLength > orderedLength {
|
||||
orderedLength = rhsLength
|
||||
}
|
||||
}
|
||||
if lhsLength == 0 && rhsLength == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
ordered := make([]string, 0, orderedLength)
|
||||
if lhs != nil {
|
||||
lhs.IterateUsing(a, func(key string, _ Value) bool {
|
||||
ordered = append(ordered, key)
|
||||
return true
|
||||
})
|
||||
}
|
||||
if rhs != nil {
|
||||
rhs.IterateUsing(a, func(key string, _ Value) bool {
|
||||
if lhs == nil || !lhs.Has(key) {
|
||||
ordered = append(ordered, key)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
sort.Strings(ordered)
|
||||
for _, key := range ordered {
|
||||
var litem, ritem Value
|
||||
if lhs != nil {
|
||||
litem, _ = lhs.GetUsing(a, key)
|
||||
}
|
||||
if rhs != nil {
|
||||
ritem, _ = rhs.GetUsing(a, key)
|
||||
}
|
||||
ok := fn(key, litem, ritem)
|
||||
if litem != nil {
|
||||
a.Free(litem)
|
||||
}
|
||||
if ritem != nil {
|
||||
a.Free(ritem)
|
||||
}
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// MapLess compares two maps lexically.
|
||||
func MapLess(lhs, rhs Map) bool {
|
||||
return MapCompare(lhs, rhs) == -1
|
||||
}
|
||||
|
||||
// MapCompare compares two maps lexically.
|
||||
func MapCompare(lhs, rhs Map) int {
|
||||
return MapCompareUsing(HeapAllocator, lhs, rhs)
|
||||
}
|
||||
|
||||
// MapCompareUsing uses the provided allocator and compares two maps lexically.
|
||||
func MapCompareUsing(a Allocator, lhs, rhs Map) int {
|
||||
c := 0
|
||||
var llength, rlength int
|
||||
if lhs != nil {
|
||||
llength = lhs.Length()
|
||||
}
|
||||
if rhs != nil {
|
||||
rlength = rhs.Length()
|
||||
}
|
||||
if llength == 0 && rlength == 0 {
|
||||
return 0
|
||||
}
|
||||
i := 0
|
||||
MapZipUsing(a, lhs, rhs, LexicalKeyOrder, func(key string, lhs, rhs Value) bool {
|
||||
switch {
|
||||
case i == llength:
|
||||
c = -1
|
||||
case i == rlength:
|
||||
c = 1
|
||||
case lhs == nil:
|
||||
c = 1
|
||||
case rhs == nil:
|
||||
c = -1
|
||||
default:
|
||||
c = CompareUsing(a, lhs, rhs)
|
||||
}
|
||||
i++
|
||||
return c == 0
|
||||
})
|
||||
return c
|
||||
}
|
||||
|
||||
// MapEquals returns true if lhs == rhs, false otherwise. This function
|
||||
// acts on generic types and should not be used by callers, but can help
|
||||
// implement Map.Equals.
|
||||
// WARN: This is a naive implementation, calling lhs.Equals(rhs) is typically the most efficient.
|
||||
func MapEquals(lhs, rhs Map) bool {
|
||||
return MapEqualsUsing(HeapAllocator, lhs, rhs)
|
||||
}
|
||||
|
||||
// MapEqualsUsing uses the provided allocator and returns true if lhs == rhs,
|
||||
// false otherwise. This function acts on generic types and should not be used
|
||||
// by callers, but can help implement Map.Equals.
|
||||
// WARN: This is a naive implementation, calling lhs.EqualsUsing(allocator, rhs) is typically the most efficient.
|
||||
func MapEqualsUsing(a Allocator, lhs, rhs Map) bool {
|
||||
if lhs == nil && rhs == nil {
|
||||
return true
|
||||
}
|
||||
if lhs == nil || rhs == nil {
|
||||
return false
|
||||
}
|
||||
if lhs.Length() != rhs.Length() {
|
||||
return false
|
||||
}
|
||||
return MapZipUsing(a, lhs, rhs, Unordered, func(key string, lhs, rhs Value) bool {
|
||||
if lhs == nil || rhs == nil {
|
||||
return false
|
||||
}
|
||||
return EqualsUsing(a, lhs, rhs)
|
||||
})
|
||||
}
|
||||
209
vendor/sigs.k8s.io/structured-merge-diff/v3/value/mapreflect.go
generated
vendored
Normal file
209
vendor/sigs.k8s.io/structured-merge-diff/v3/value/mapreflect.go
generated
vendored
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type mapReflect struct {
|
||||
valueReflect
|
||||
}
|
||||
|
||||
func (r mapReflect) Length() int {
|
||||
val := r.Value
|
||||
return val.Len()
|
||||
}
|
||||
|
||||
func (r mapReflect) Empty() bool {
|
||||
val := r.Value
|
||||
return val.Len() == 0
|
||||
}
|
||||
|
||||
func (r mapReflect) Get(key string) (Value, bool) {
|
||||
return r.GetUsing(HeapAllocator, key)
|
||||
}
|
||||
|
||||
func (r mapReflect) GetUsing(a Allocator, key string) (Value, bool) {
|
||||
k, v, ok := r.get(key)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return a.allocValueReflect().mustReuse(v, nil, &r.Value, &k), true
|
||||
}
|
||||
|
||||
func (r mapReflect) get(k string) (key, value reflect.Value, ok bool) {
|
||||
mapKey := r.toMapKey(k)
|
||||
val := r.Value.MapIndex(mapKey)
|
||||
return mapKey, val, val.IsValid() && val != reflect.Value{}
|
||||
}
|
||||
|
||||
func (r mapReflect) Has(key string) bool {
|
||||
var val reflect.Value
|
||||
val = r.Value.MapIndex(r.toMapKey(key))
|
||||
if !val.IsValid() {
|
||||
return false
|
||||
}
|
||||
return val != reflect.Value{}
|
||||
}
|
||||
|
||||
func (r mapReflect) Set(key string, val Value) {
|
||||
r.Value.SetMapIndex(r.toMapKey(key), reflect.ValueOf(val.Unstructured()))
|
||||
}
|
||||
|
||||
func (r mapReflect) Delete(key string) {
|
||||
val := r.Value
|
||||
val.SetMapIndex(r.toMapKey(key), reflect.Value{})
|
||||
}
|
||||
|
||||
// TODO: Do we need to support types that implement json.Marshaler and are used as string keys?
|
||||
func (r mapReflect) toMapKey(key string) reflect.Value {
|
||||
val := r.Value
|
||||
return reflect.ValueOf(key).Convert(val.Type().Key())
|
||||
}
|
||||
|
||||
func (r mapReflect) Iterate(fn func(string, Value) bool) bool {
|
||||
return r.IterateUsing(HeapAllocator, fn)
|
||||
}
|
||||
|
||||
func (r mapReflect) IterateUsing(a Allocator, fn func(string, Value) bool) bool {
|
||||
if r.Value.Len() == 0 {
|
||||
return true
|
||||
}
|
||||
v := a.allocValueReflect()
|
||||
defer a.Free(v)
|
||||
return eachMapEntry(r.Value, func(e *TypeReflectCacheEntry, key reflect.Value, value reflect.Value) bool {
|
||||
return fn(key.String(), v.mustReuse(value, e, &r.Value, &key))
|
||||
})
|
||||
}
|
||||
|
||||
func eachMapEntry(val reflect.Value, fn func(*TypeReflectCacheEntry, reflect.Value, reflect.Value) bool) bool {
|
||||
iter := val.MapRange()
|
||||
entry := TypeReflectEntryOf(val.Type().Elem())
|
||||
for iter.Next() {
|
||||
next := iter.Value()
|
||||
if !next.IsValid() {
|
||||
continue
|
||||
}
|
||||
if !fn(entry, iter.Key(), next) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (r mapReflect) Unstructured() interface{} {
|
||||
result := make(map[string]interface{}, r.Length())
|
||||
r.Iterate(func(s string, value Value) bool {
|
||||
result[s] = value.Unstructured()
|
||||
return true
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
func (r mapReflect) Equals(m Map) bool {
|
||||
return r.EqualsUsing(HeapAllocator, m)
|
||||
}
|
||||
|
||||
func (r mapReflect) EqualsUsing(a Allocator, m Map) bool {
|
||||
lhsLength := r.Length()
|
||||
rhsLength := m.Length()
|
||||
if lhsLength != rhsLength {
|
||||
return false
|
||||
}
|
||||
if lhsLength == 0 {
|
||||
return true
|
||||
}
|
||||
vr := a.allocValueReflect()
|
||||
defer a.Free(vr)
|
||||
entry := TypeReflectEntryOf(r.Value.Type().Elem())
|
||||
return m.Iterate(func(key string, value Value) bool {
|
||||
_, lhsVal, ok := r.get(key)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return Equals(vr.mustReuse(lhsVal, entry, nil, nil), value)
|
||||
})
|
||||
}
|
||||
|
||||
func (r mapReflect) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
return r.ZipUsing(HeapAllocator, other, order, fn)
|
||||
}
|
||||
|
||||
func (r mapReflect) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
if otherMapReflect, ok := other.(*mapReflect); ok && order == Unordered {
|
||||
return r.unorderedReflectZip(a, otherMapReflect, fn)
|
||||
}
|
||||
return defaultMapZip(a, &r, other, order, fn)
|
||||
}
|
||||
|
||||
// unorderedReflectZip provides an optimized unordered zip for mapReflect types.
|
||||
func (r mapReflect) unorderedReflectZip(a Allocator, other *mapReflect, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
if r.Empty() && (other == nil || other.Empty()) {
|
||||
return true
|
||||
}
|
||||
|
||||
lhs := r.Value
|
||||
lhsEntry := TypeReflectEntryOf(lhs.Type().Elem())
|
||||
|
||||
// map lookup via reflection is expensive enough that it is better to keep track of visited keys
|
||||
visited := map[string]struct{}{}
|
||||
|
||||
vlhs, vrhs := a.allocValueReflect(), a.allocValueReflect()
|
||||
defer a.Free(vlhs)
|
||||
defer a.Free(vrhs)
|
||||
|
||||
if other != nil {
|
||||
rhs := other.Value
|
||||
rhsEntry := TypeReflectEntryOf(rhs.Type().Elem())
|
||||
iter := rhs.MapRange()
|
||||
|
||||
for iter.Next() {
|
||||
key := iter.Key()
|
||||
keyString := key.String()
|
||||
next := iter.Value()
|
||||
if !next.IsValid() {
|
||||
continue
|
||||
}
|
||||
rhsVal := vrhs.mustReuse(next, rhsEntry, &rhs, &key)
|
||||
visited[keyString] = struct{}{}
|
||||
var lhsVal Value
|
||||
if _, v, ok := r.get(keyString); ok {
|
||||
lhsVal = vlhs.mustReuse(v, lhsEntry, &lhs, &key)
|
||||
}
|
||||
if !fn(keyString, lhsVal, rhsVal) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
iter := lhs.MapRange()
|
||||
for iter.Next() {
|
||||
key := iter.Key()
|
||||
if _, ok := visited[key.String()]; ok {
|
||||
continue
|
||||
}
|
||||
next := iter.Value()
|
||||
if !next.IsValid() {
|
||||
continue
|
||||
}
|
||||
if !fn(key.String(), vlhs.mustReuse(next, lhsEntry, &lhs, &key), nil) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
190
vendor/sigs.k8s.io/structured-merge-diff/v3/value/mapunstructured.go
generated
vendored
Normal file
190
vendor/sigs.k8s.io/structured-merge-diff/v3/value/mapunstructured.go
generated
vendored
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
type mapUnstructuredInterface map[interface{}]interface{}
|
||||
|
||||
func (m mapUnstructuredInterface) Set(key string, val Value) {
|
||||
m[key] = val.Unstructured()
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) Get(key string) (Value, bool) {
|
||||
return m.GetUsing(HeapAllocator, key)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) GetUsing(a Allocator, key string) (Value, bool) {
|
||||
if v, ok := m[key]; !ok {
|
||||
return nil, false
|
||||
} else {
|
||||
return a.allocValueUnstructured().reuse(v), true
|
||||
}
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) Has(key string) bool {
|
||||
_, ok := m[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) Delete(key string) {
|
||||
delete(m, key)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) Iterate(fn func(key string, value Value) bool) bool {
|
||||
return m.IterateUsing(HeapAllocator, fn)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) IterateUsing(a Allocator, fn func(key string, value Value) bool) bool {
|
||||
if len(m) == 0 {
|
||||
return true
|
||||
}
|
||||
vv := a.allocValueUnstructured()
|
||||
defer a.Free(vv)
|
||||
for k, v := range m {
|
||||
if ks, ok := k.(string); !ok {
|
||||
continue
|
||||
} else {
|
||||
if !fn(ks, vv.reuse(v)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) Length() int {
|
||||
return len(m)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) Empty() bool {
|
||||
return len(m) == 0
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) Equals(other Map) bool {
|
||||
return m.EqualsUsing(HeapAllocator, other)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) EqualsUsing(a Allocator, other Map) bool {
|
||||
lhsLength := m.Length()
|
||||
rhsLength := other.Length()
|
||||
if lhsLength != rhsLength {
|
||||
return false
|
||||
}
|
||||
if lhsLength == 0 {
|
||||
return true
|
||||
}
|
||||
vv := a.allocValueUnstructured()
|
||||
defer a.Free(vv)
|
||||
return other.Iterate(func(key string, value Value) bool {
|
||||
lhsVal, ok := m[key]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return Equals(vv.reuse(lhsVal), value)
|
||||
})
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
return m.ZipUsing(HeapAllocator, other, order, fn)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredInterface) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
return defaultMapZip(a, m, other, order, fn)
|
||||
}
|
||||
|
||||
type mapUnstructuredString map[string]interface{}
|
||||
|
||||
func (m mapUnstructuredString) Set(key string, val Value) {
|
||||
m[key] = val.Unstructured()
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) Get(key string) (Value, bool) {
|
||||
return m.GetUsing(HeapAllocator, key)
|
||||
}
|
||||
func (m mapUnstructuredString) GetUsing(a Allocator, key string) (Value, bool) {
|
||||
if v, ok := m[key]; !ok {
|
||||
return nil, false
|
||||
} else {
|
||||
return a.allocValueUnstructured().reuse(v), true
|
||||
}
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) Has(key string) bool {
|
||||
_, ok := m[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) Delete(key string) {
|
||||
delete(m, key)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) Iterate(fn func(key string, value Value) bool) bool {
|
||||
return m.IterateUsing(HeapAllocator, fn)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) IterateUsing(a Allocator, fn func(key string, value Value) bool) bool {
|
||||
if len(m) == 0 {
|
||||
return true
|
||||
}
|
||||
vv := a.allocValueUnstructured()
|
||||
defer a.Free(vv)
|
||||
for k, v := range m {
|
||||
if !fn(k, vv.reuse(v)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) Length() int {
|
||||
return len(m)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) Equals(other Map) bool {
|
||||
return m.EqualsUsing(HeapAllocator, other)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) EqualsUsing(a Allocator, other Map) bool {
|
||||
lhsLength := m.Length()
|
||||
rhsLength := other.Length()
|
||||
if lhsLength != rhsLength {
|
||||
return false
|
||||
}
|
||||
if lhsLength == 0 {
|
||||
return true
|
||||
}
|
||||
vv := a.allocValueUnstructured()
|
||||
defer a.Free(vv)
|
||||
return other.Iterate(func(key string, value Value) bool {
|
||||
lhsVal, ok := m[key]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return Equals(vv.reuse(lhsVal), value)
|
||||
})
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
return m.ZipUsing(HeapAllocator, other, order, fn)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
return defaultMapZip(a, m, other, order, fn)
|
||||
}
|
||||
|
||||
func (m mapUnstructuredString) Empty() bool {
|
||||
return len(m) == 0
|
||||
}
|
||||
463
vendor/sigs.k8s.io/structured-merge-diff/v3/value/reflectcache.go
generated
vendored
Normal file
463
vendor/sigs.k8s.io/structured-merge-diff/v3/value/reflectcache.go
generated
vendored
Normal file
|
|
@ -0,0 +1,463 @@
|
|||
/*
|
||||
Copyright 2020 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// UnstructuredConverter defines how a type can be converted directly to unstructured.
|
||||
// Types that implement json.Marshaler may also optionally implement this interface to provide a more
|
||||
// direct and more efficient conversion. All types that choose to implement this interface must still
|
||||
// implement this same conversion via json.Marshaler.
|
||||
type UnstructuredConverter interface {
|
||||
json.Marshaler // require that json.Marshaler is implemented
|
||||
|
||||
// ToUnstructured returns the unstructured representation.
|
||||
ToUnstructured() interface{}
|
||||
}
|
||||
|
||||
// TypeReflectCacheEntry keeps data gathered using reflection about how a type is converted to/from unstructured.
|
||||
type TypeReflectCacheEntry struct {
|
||||
isJsonMarshaler bool
|
||||
ptrIsJsonMarshaler bool
|
||||
isJsonUnmarshaler bool
|
||||
ptrIsJsonUnmarshaler bool
|
||||
isStringConvertable bool
|
||||
ptrIsStringConvertable bool
|
||||
|
||||
structFields map[string]*FieldCacheEntry
|
||||
orderedStructFields []*FieldCacheEntry
|
||||
}
|
||||
|
||||
// FieldCacheEntry keeps data gathered using reflection about how the field of a struct is converted to/from
|
||||
// unstructured.
|
||||
type FieldCacheEntry struct {
|
||||
// JsonName returns the name of the field according to the json tags on the struct field.
|
||||
JsonName string
|
||||
// isOmitEmpty is true if the field has the json 'omitempty' tag.
|
||||
isOmitEmpty bool
|
||||
// fieldPath is a list of field indices (see FieldByIndex) to lookup the value of
|
||||
// a field in a reflect.Value struct. The field indices in the list form a path used
|
||||
// to traverse through intermediary 'inline' fields.
|
||||
fieldPath [][]int
|
||||
|
||||
fieldType reflect.Type
|
||||
TypeEntry *TypeReflectCacheEntry
|
||||
}
|
||||
|
||||
func (f *FieldCacheEntry) CanOmit(fieldVal reflect.Value) bool {
|
||||
return f.isOmitEmpty && (safeIsNil(fieldVal) || isZero(fieldVal))
|
||||
}
|
||||
|
||||
// GetUsing returns the field identified by this FieldCacheEntry from the provided struct.
|
||||
func (f *FieldCacheEntry) GetFrom(structVal reflect.Value) reflect.Value {
|
||||
// field might be nested within 'inline' structs
|
||||
for _, elem := range f.fieldPath {
|
||||
structVal = structVal.FieldByIndex(elem)
|
||||
}
|
||||
return structVal
|
||||
}
|
||||
|
||||
var marshalerType = reflect.TypeOf(new(json.Marshaler)).Elem()
|
||||
var unmarshalerType = reflect.TypeOf(new(json.Unmarshaler)).Elem()
|
||||
var unstructuredConvertableType = reflect.TypeOf(new(UnstructuredConverter)).Elem()
|
||||
var defaultReflectCache = newReflectCache()
|
||||
|
||||
// TypeReflectEntryOf returns the TypeReflectCacheEntry of the provided reflect.Type.
|
||||
func TypeReflectEntryOf(t reflect.Type) *TypeReflectCacheEntry {
|
||||
cm := defaultReflectCache.get()
|
||||
if record, ok := cm[t]; ok {
|
||||
return record
|
||||
}
|
||||
updates := reflectCacheMap{}
|
||||
result := typeReflectEntryOf(cm, t, updates)
|
||||
if len(updates) > 0 {
|
||||
defaultReflectCache.update(updates)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// TypeReflectEntryOf returns all updates needed to add provided reflect.Type, and the types its fields transitively
|
||||
// depend on, to the cache.
|
||||
func typeReflectEntryOf(cm reflectCacheMap, t reflect.Type, updates reflectCacheMap) *TypeReflectCacheEntry {
|
||||
if record, ok := cm[t]; ok {
|
||||
return record
|
||||
}
|
||||
if record, ok := updates[t]; ok {
|
||||
return record
|
||||
}
|
||||
typeEntry := &TypeReflectCacheEntry{
|
||||
isJsonMarshaler: t.Implements(marshalerType),
|
||||
ptrIsJsonMarshaler: reflect.PtrTo(t).Implements(marshalerType),
|
||||
isJsonUnmarshaler: reflect.PtrTo(t).Implements(unmarshalerType),
|
||||
isStringConvertable: t.Implements(unstructuredConvertableType),
|
||||
ptrIsStringConvertable: reflect.PtrTo(t).Implements(unstructuredConvertableType),
|
||||
}
|
||||
if t.Kind() == reflect.Struct {
|
||||
fieldEntries := map[string]*FieldCacheEntry{}
|
||||
buildStructCacheEntry(t, fieldEntries, nil)
|
||||
typeEntry.structFields = fieldEntries
|
||||
sortedByJsonName := make([]*FieldCacheEntry, len(fieldEntries))
|
||||
i := 0
|
||||
for _, entry := range fieldEntries {
|
||||
sortedByJsonName[i] = entry
|
||||
i++
|
||||
}
|
||||
sort.Slice(sortedByJsonName, func(i, j int) bool {
|
||||
return sortedByJsonName[i].JsonName < sortedByJsonName[j].JsonName
|
||||
})
|
||||
typeEntry.orderedStructFields = sortedByJsonName
|
||||
}
|
||||
|
||||
// cyclic type references are allowed, so we must add the typeEntry to the updates map before resolving
|
||||
// the field.typeEntry references, or creating them if they are not already in the cache
|
||||
updates[t] = typeEntry
|
||||
|
||||
for _, field := range typeEntry.structFields {
|
||||
if field.TypeEntry == nil {
|
||||
field.TypeEntry = typeReflectEntryOf(cm, field.fieldType, updates)
|
||||
}
|
||||
}
|
||||
return typeEntry
|
||||
}
|
||||
|
||||
func buildStructCacheEntry(t reflect.Type, infos map[string]*FieldCacheEntry, fieldPath [][]int) {
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
jsonName, omit, isInline, isOmitempty := lookupJsonTags(field)
|
||||
if omit {
|
||||
continue
|
||||
}
|
||||
if isInline {
|
||||
buildStructCacheEntry(field.Type, infos, append(fieldPath, field.Index))
|
||||
continue
|
||||
}
|
||||
info := &FieldCacheEntry{JsonName: jsonName, isOmitEmpty: isOmitempty, fieldPath: append(fieldPath, field.Index), fieldType: field.Type}
|
||||
infos[jsonName] = info
|
||||
}
|
||||
}
|
||||
|
||||
// Fields returns a map of JSON field name to FieldCacheEntry for structs, or nil for non-structs.
|
||||
func (e TypeReflectCacheEntry) Fields() map[string]*FieldCacheEntry {
|
||||
return e.structFields
|
||||
}
|
||||
|
||||
// Fields returns a map of JSON field name to FieldCacheEntry for structs, or nil for non-structs.
|
||||
func (e TypeReflectCacheEntry) OrderedFields() []*FieldCacheEntry {
|
||||
return e.orderedStructFields
|
||||
}
|
||||
|
||||
// CanConvertToUnstructured returns true if this TypeReflectCacheEntry can convert values of its type to unstructured.
|
||||
func (e TypeReflectCacheEntry) CanConvertToUnstructured() bool {
|
||||
return e.isJsonMarshaler || e.ptrIsJsonMarshaler || e.isStringConvertable || e.ptrIsStringConvertable
|
||||
}
|
||||
|
||||
// ToUnstructured converts the provided value to unstructured and returns it.
|
||||
func (e TypeReflectCacheEntry) ToUnstructured(sv reflect.Value) (interface{}, error) {
|
||||
// This is based on https://github.com/kubernetes/kubernetes/blob/82c9e5c814eb7acc6cc0a090c057294d0667ad66/staging/src/k8s.io/apimachinery/pkg/runtime/converter.go#L505
|
||||
// and is intended to replace it.
|
||||
|
||||
// Check if the object has a custom string converter and use it if available, since it is much more efficient
|
||||
// than round tripping through json.
|
||||
if converter, ok := e.getUnstructuredConverter(sv); ok {
|
||||
return converter.ToUnstructured(), nil
|
||||
}
|
||||
// Check if the object has a custom JSON marshaller/unmarshaller.
|
||||
if marshaler, ok := e.getJsonMarshaler(sv); ok {
|
||||
if sv.Kind() == reflect.Ptr && sv.IsNil() {
|
||||
// We're done - we don't need to store anything.
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
data, err := marshaler.MarshalJSON()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch {
|
||||
case len(data) == 0:
|
||||
return nil, fmt.Errorf("error decoding from json: empty value")
|
||||
|
||||
case bytes.Equal(data, nullBytes):
|
||||
// We're done - we don't need to store anything.
|
||||
return nil, nil
|
||||
|
||||
case bytes.Equal(data, trueBytes):
|
||||
return true, nil
|
||||
|
||||
case bytes.Equal(data, falseBytes):
|
||||
return false, nil
|
||||
|
||||
case data[0] == '"':
|
||||
var result string
|
||||
err := unmarshal(data, &result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error decoding string from json: %v", err)
|
||||
}
|
||||
return result, nil
|
||||
|
||||
case data[0] == '{':
|
||||
result := make(map[string]interface{})
|
||||
err := unmarshal(data, &result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error decoding object from json: %v", err)
|
||||
}
|
||||
return result, nil
|
||||
|
||||
case data[0] == '[':
|
||||
result := make([]interface{}, 0)
|
||||
err := unmarshal(data, &result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error decoding array from json: %v", err)
|
||||
}
|
||||
return result, nil
|
||||
|
||||
default:
|
||||
var (
|
||||
resultInt int64
|
||||
resultFloat float64
|
||||
err error
|
||||
)
|
||||
if err = unmarshal(data, &resultInt); err == nil {
|
||||
return resultInt, nil
|
||||
} else if err = unmarshal(data, &resultFloat); err == nil {
|
||||
return resultFloat, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("error decoding number from json: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("provided type cannot be converted: %v", sv.Type())
|
||||
}
|
||||
|
||||
// CanConvertFromUnstructured returns true if this TypeReflectCacheEntry can convert objects of the type from unstructured.
|
||||
func (e TypeReflectCacheEntry) CanConvertFromUnstructured() bool {
|
||||
return e.isJsonUnmarshaler
|
||||
}
|
||||
|
||||
// FromUnstructured converts the provided source value from unstructured into the provided destination value.
|
||||
func (e TypeReflectCacheEntry) FromUnstructured(sv, dv reflect.Value) error {
|
||||
// TODO: this could be made much more efficient using direct conversions like
|
||||
// UnstructuredConverter.ToUnstructured provides.
|
||||
st := dv.Type()
|
||||
data, err := json.Marshal(sv.Interface())
|
||||
if err != nil {
|
||||
return fmt.Errorf("error encoding %s to json: %v", st.String(), err)
|
||||
}
|
||||
if unmarshaler, ok := e.getJsonUnmarshaler(dv); ok {
|
||||
return unmarshaler.UnmarshalJSON(data)
|
||||
}
|
||||
return fmt.Errorf("unable to unmarshal %v into %v", sv.Type(), dv.Type())
|
||||
}
|
||||
|
||||
var (
|
||||
nullBytes = []byte("null")
|
||||
trueBytes = []byte("true")
|
||||
falseBytes = []byte("false")
|
||||
)
|
||||
|
||||
func (e TypeReflectCacheEntry) getJsonMarshaler(v reflect.Value) (json.Marshaler, bool) {
|
||||
if e.isJsonMarshaler {
|
||||
return v.Interface().(json.Marshaler), true
|
||||
}
|
||||
if e.ptrIsJsonMarshaler {
|
||||
// Check pointer receivers if v is not a pointer
|
||||
if v.Kind() != reflect.Ptr && v.CanAddr() {
|
||||
v = v.Addr()
|
||||
return v.Interface().(json.Marshaler), true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (e TypeReflectCacheEntry) getJsonUnmarshaler(v reflect.Value) (json.Unmarshaler, bool) {
|
||||
if !e.isJsonUnmarshaler {
|
||||
return nil, false
|
||||
}
|
||||
return v.Addr().Interface().(json.Unmarshaler), true
|
||||
}
|
||||
|
||||
func (e TypeReflectCacheEntry) getUnstructuredConverter(v reflect.Value) (UnstructuredConverter, bool) {
|
||||
if e.isStringConvertable {
|
||||
return v.Interface().(UnstructuredConverter), true
|
||||
}
|
||||
if e.ptrIsStringConvertable {
|
||||
// Check pointer receivers if v is not a pointer
|
||||
if v.CanAddr() {
|
||||
v = v.Addr()
|
||||
return v.Interface().(UnstructuredConverter), true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
type typeReflectCache struct {
|
||||
// use an atomic and copy-on-write since there are a fixed (typically very small) number of structs compiled into any
|
||||
// go program using this cache
|
||||
value atomic.Value
|
||||
// mu is held by writers when performing load/modify/store operations on the cache, readers do not need to hold a
|
||||
// read-lock since the atomic value is always read-only
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func newReflectCache() *typeReflectCache {
|
||||
cache := &typeReflectCache{}
|
||||
cache.value.Store(make(reflectCacheMap))
|
||||
return cache
|
||||
}
|
||||
|
||||
type reflectCacheMap map[reflect.Type]*TypeReflectCacheEntry
|
||||
|
||||
// get returns the reflectCacheMap.
|
||||
func (c *typeReflectCache) get() reflectCacheMap {
|
||||
return c.value.Load().(reflectCacheMap)
|
||||
}
|
||||
|
||||
// update merges the provided updates into the cache.
|
||||
func (c *typeReflectCache) update(updates reflectCacheMap) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
currentCacheMap := c.value.Load().(reflectCacheMap)
|
||||
|
||||
hasNewEntries := false
|
||||
for t := range updates {
|
||||
if _, ok := currentCacheMap[t]; !ok {
|
||||
hasNewEntries = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasNewEntries {
|
||||
// Bail if the updates have been set while waiting for lock acquisition.
|
||||
// This is safe since setting entries is idempotent.
|
||||
return
|
||||
}
|
||||
|
||||
newCacheMap := make(reflectCacheMap, len(currentCacheMap)+len(updates))
|
||||
for k, v := range currentCacheMap {
|
||||
newCacheMap[k] = v
|
||||
}
|
||||
for t, update := range updates {
|
||||
newCacheMap[t] = update
|
||||
}
|
||||
c.value.Store(newCacheMap)
|
||||
}
|
||||
|
||||
// Below json Unmarshal is fromk8s.io/apimachinery/pkg/util/json
|
||||
// to handle number conversions as expected by Kubernetes
|
||||
|
||||
// limit recursive depth to prevent stack overflow errors
|
||||
const maxDepth = 10000
|
||||
|
||||
// unmarshal unmarshals the given data
|
||||
// If v is a *map[string]interface{}, numbers are converted to int64 or float64
|
||||
func unmarshal(data []byte, v interface{}) error {
|
||||
switch v := v.(type) {
|
||||
case *map[string]interface{}:
|
||||
// Build a decoder from the given data
|
||||
decoder := json.NewDecoder(bytes.NewBuffer(data))
|
||||
// Preserve numbers, rather than casting to float64 automatically
|
||||
decoder.UseNumber()
|
||||
// Run the decode
|
||||
if err := decoder.Decode(v); err != nil {
|
||||
return err
|
||||
}
|
||||
// If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64
|
||||
return convertMapNumbers(*v, 0)
|
||||
|
||||
case *[]interface{}:
|
||||
// Build a decoder from the given data
|
||||
decoder := json.NewDecoder(bytes.NewBuffer(data))
|
||||
// Preserve numbers, rather than casting to float64 automatically
|
||||
decoder.UseNumber()
|
||||
// Run the decode
|
||||
if err := decoder.Decode(v); err != nil {
|
||||
return err
|
||||
}
|
||||
// If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64
|
||||
return convertSliceNumbers(*v, 0)
|
||||
|
||||
default:
|
||||
return json.Unmarshal(data, v)
|
||||
}
|
||||
}
|
||||
|
||||
// convertMapNumbers traverses the map, converting any json.Number values to int64 or float64.
|
||||
// values which are map[string]interface{} or []interface{} are recursively visited
|
||||
func convertMapNumbers(m map[string]interface{}, depth int) error {
|
||||
if depth > maxDepth {
|
||||
return fmt.Errorf("exceeded max depth of %d", maxDepth)
|
||||
}
|
||||
|
||||
var err error
|
||||
for k, v := range m {
|
||||
switch v := v.(type) {
|
||||
case json.Number:
|
||||
m[k], err = convertNumber(v)
|
||||
case map[string]interface{}:
|
||||
err = convertMapNumbers(v, depth+1)
|
||||
case []interface{}:
|
||||
err = convertSliceNumbers(v, depth+1)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// convertSliceNumbers traverses the slice, converting any json.Number values to int64 or float64.
|
||||
// values which are map[string]interface{} or []interface{} are recursively visited
|
||||
func convertSliceNumbers(s []interface{}, depth int) error {
|
||||
if depth > maxDepth {
|
||||
return fmt.Errorf("exceeded max depth of %d", maxDepth)
|
||||
}
|
||||
|
||||
var err error
|
||||
for i, v := range s {
|
||||
switch v := v.(type) {
|
||||
case json.Number:
|
||||
s[i], err = convertNumber(v)
|
||||
case map[string]interface{}:
|
||||
err = convertMapNumbers(v, depth+1)
|
||||
case []interface{}:
|
||||
err = convertSliceNumbers(v, depth+1)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// convertNumber converts a json.Number to an int64 or float64, or returns an error
|
||||
func convertNumber(n json.Number) (interface{}, error) {
|
||||
// Attempt to convert to an int64 first
|
||||
if i, err := n.Int64(); err == nil {
|
||||
return i, nil
|
||||
}
|
||||
// Return a float64 (default json.Decode() behavior)
|
||||
// An overflow will return an error
|
||||
return n.Float64()
|
||||
}
|
||||
50
vendor/sigs.k8s.io/structured-merge-diff/v3/value/scalar.go
generated
vendored
Normal file
50
vendor/sigs.k8s.io/structured-merge-diff/v3/value/scalar.go
generated
vendored
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
// Compare compares floats. The result will be 0 if lhs==rhs, -1 if f <
|
||||
// rhs, and +1 if f > rhs.
|
||||
func FloatCompare(lhs, rhs float64) int {
|
||||
if lhs > rhs {
|
||||
return 1
|
||||
} else if lhs < rhs {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// IntCompare compares integers. The result will be 0 if i==rhs, -1 if i <
|
||||
// rhs, and +1 if i > rhs.
|
||||
func IntCompare(lhs, rhs int64) int {
|
||||
if lhs > rhs {
|
||||
return 1
|
||||
} else if lhs < rhs {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Compare compares booleans. The result will be 0 if b==rhs, -1 if b <
|
||||
// rhs, and +1 if b > rhs.
|
||||
func BoolCompare(lhs, rhs bool) int {
|
||||
if lhs == rhs {
|
||||
return 0
|
||||
} else if lhs == false {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
208
vendor/sigs.k8s.io/structured-merge-diff/v3/value/structreflect.go
generated
vendored
Normal file
208
vendor/sigs.k8s.io/structured-merge-diff/v3/value/structreflect.go
generated
vendored
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type structReflect struct {
|
||||
valueReflect
|
||||
}
|
||||
|
||||
func (r structReflect) Length() int {
|
||||
i := 0
|
||||
eachStructField(r.Value, func(_ *TypeReflectCacheEntry, s string, value reflect.Value) bool {
|
||||
i++
|
||||
return true
|
||||
})
|
||||
return i
|
||||
}
|
||||
|
||||
func (r structReflect) Empty() bool {
|
||||
return eachStructField(r.Value, func(_ *TypeReflectCacheEntry, s string, value reflect.Value) bool {
|
||||
return false // exit early if the struct is non-empty
|
||||
})
|
||||
}
|
||||
|
||||
func (r structReflect) Get(key string) (Value, bool) {
|
||||
return r.GetUsing(HeapAllocator, key)
|
||||
}
|
||||
|
||||
func (r structReflect) GetUsing(a Allocator, key string) (Value, bool) {
|
||||
if val, ok := r.findJsonNameField(key); ok {
|
||||
return a.allocValueReflect().mustReuse(val, nil, nil, nil), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (r structReflect) Has(key string) bool {
|
||||
_, ok := r.findJsonNameField(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (r structReflect) Set(key string, val Value) {
|
||||
fieldEntry, ok := TypeReflectEntryOf(r.Value.Type()).Fields()[key]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("key %s may not be set on struct %T: field does not exist", key, r.Value.Interface()))
|
||||
}
|
||||
oldVal := fieldEntry.GetFrom(r.Value)
|
||||
newVal := reflect.ValueOf(val.Unstructured())
|
||||
r.update(fieldEntry, key, oldVal, newVal)
|
||||
}
|
||||
|
||||
func (r structReflect) Delete(key string) {
|
||||
fieldEntry, ok := TypeReflectEntryOf(r.Value.Type()).Fields()[key]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("key %s may not be deleted on struct %T: field does not exist", key, r.Value.Interface()))
|
||||
}
|
||||
oldVal := fieldEntry.GetFrom(r.Value)
|
||||
if oldVal.Kind() != reflect.Ptr && !fieldEntry.isOmitEmpty {
|
||||
panic(fmt.Sprintf("key %s may not be deleted on struct: %T: value is neither a pointer nor an omitempty field", key, r.Value.Interface()))
|
||||
}
|
||||
r.update(fieldEntry, key, oldVal, reflect.Zero(oldVal.Type()))
|
||||
}
|
||||
|
||||
func (r structReflect) update(fieldEntry *FieldCacheEntry, key string, oldVal, newVal reflect.Value) {
|
||||
if oldVal.CanSet() {
|
||||
oldVal.Set(newVal)
|
||||
return
|
||||
}
|
||||
|
||||
// map items are not addressable, so if a struct is contained in a map, the only way to modify it is
|
||||
// to write a replacement fieldEntry into the map.
|
||||
if r.ParentMap != nil {
|
||||
if r.ParentMapKey == nil {
|
||||
panic("ParentMapKey must not be nil if ParentMap is not nil")
|
||||
}
|
||||
replacement := reflect.New(r.Value.Type()).Elem()
|
||||
fieldEntry.GetFrom(replacement).Set(newVal)
|
||||
r.ParentMap.SetMapIndex(*r.ParentMapKey, replacement)
|
||||
return
|
||||
}
|
||||
|
||||
// This should never happen since NewValueReflect ensures that the root object reflected on is a pointer and map
|
||||
// item replacement is handled above.
|
||||
panic(fmt.Sprintf("key %s may not be modified on struct: %T: struct is not settable", key, r.Value.Interface()))
|
||||
}
|
||||
|
||||
func (r structReflect) Iterate(fn func(string, Value) bool) bool {
|
||||
return r.IterateUsing(HeapAllocator, fn)
|
||||
}
|
||||
|
||||
func (r structReflect) IterateUsing(a Allocator, fn func(string, Value) bool) bool {
|
||||
vr := a.allocValueReflect()
|
||||
defer a.Free(vr)
|
||||
return eachStructField(r.Value, func(e *TypeReflectCacheEntry, s string, value reflect.Value) bool {
|
||||
return fn(s, vr.mustReuse(value, e, nil, nil))
|
||||
})
|
||||
}
|
||||
|
||||
func eachStructField(structVal reflect.Value, fn func(*TypeReflectCacheEntry, string, reflect.Value) bool) bool {
|
||||
for _, fieldCacheEntry := range TypeReflectEntryOf(structVal.Type()).OrderedFields() {
|
||||
fieldVal := fieldCacheEntry.GetFrom(structVal)
|
||||
if fieldCacheEntry.CanOmit(fieldVal) {
|
||||
// omit it
|
||||
continue
|
||||
}
|
||||
ok := fn(fieldCacheEntry.TypeEntry, fieldCacheEntry.JsonName, fieldVal)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (r structReflect) Unstructured() interface{} {
|
||||
// Use number of struct fields as a cheap way to rough estimate map size
|
||||
result := make(map[string]interface{}, r.Value.NumField())
|
||||
r.Iterate(func(s string, value Value) bool {
|
||||
result[s] = value.Unstructured()
|
||||
return true
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
func (r structReflect) Equals(m Map) bool {
|
||||
return r.EqualsUsing(HeapAllocator, m)
|
||||
}
|
||||
|
||||
func (r structReflect) EqualsUsing(a Allocator, m Map) bool {
|
||||
// MapEquals uses zip and is fairly efficient for structReflect
|
||||
return MapEqualsUsing(a, &r, m)
|
||||
}
|
||||
|
||||
func (r structReflect) findJsonNameFieldAndNotEmpty(jsonName string) (reflect.Value, bool) {
|
||||
structCacheEntry, ok := TypeReflectEntryOf(r.Value.Type()).Fields()[jsonName]
|
||||
if !ok {
|
||||
return reflect.Value{}, false
|
||||
}
|
||||
fieldVal := structCacheEntry.GetFrom(r.Value)
|
||||
return fieldVal, !structCacheEntry.CanOmit(fieldVal)
|
||||
}
|
||||
|
||||
func (r structReflect) findJsonNameField(jsonName string) (val reflect.Value, ok bool) {
|
||||
structCacheEntry, ok := TypeReflectEntryOf(r.Value.Type()).Fields()[jsonName]
|
||||
if !ok {
|
||||
return reflect.Value{}, false
|
||||
}
|
||||
fieldVal := structCacheEntry.GetFrom(r.Value)
|
||||
return fieldVal, !structCacheEntry.CanOmit(fieldVal)
|
||||
}
|
||||
|
||||
func (r structReflect) Zip(other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
return r.ZipUsing(HeapAllocator, other, order, fn)
|
||||
}
|
||||
|
||||
func (r structReflect) ZipUsing(a Allocator, other Map, order MapTraverseOrder, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
if otherStruct, ok := other.(*structReflect); ok && r.Value.Type() == otherStruct.Value.Type() {
|
||||
lhsvr, rhsvr := a.allocValueReflect(), a.allocValueReflect()
|
||||
defer a.Free(lhsvr)
|
||||
defer a.Free(rhsvr)
|
||||
return r.structZip(otherStruct, lhsvr, rhsvr, fn)
|
||||
}
|
||||
return defaultMapZip(a, &r, other, order, fn)
|
||||
}
|
||||
|
||||
// structZip provides an optimized zip for structReflect types. The zip is always lexical key ordered since there is
|
||||
// no additional cost to ordering the zip for structured types.
|
||||
func (r structReflect) structZip(other *structReflect, lhsvr, rhsvr *valueReflect, fn func(key string, lhs, rhs Value) bool) bool {
|
||||
lhsVal := r.Value
|
||||
rhsVal := other.Value
|
||||
|
||||
for _, fieldCacheEntry := range TypeReflectEntryOf(lhsVal.Type()).OrderedFields() {
|
||||
lhsFieldVal := fieldCacheEntry.GetFrom(lhsVal)
|
||||
rhsFieldVal := fieldCacheEntry.GetFrom(rhsVal)
|
||||
lhsOmit := fieldCacheEntry.CanOmit(lhsFieldVal)
|
||||
rhsOmit := fieldCacheEntry.CanOmit(rhsFieldVal)
|
||||
if lhsOmit && rhsOmit {
|
||||
continue
|
||||
}
|
||||
var lhsVal, rhsVal Value
|
||||
if !lhsOmit {
|
||||
lhsVal = lhsvr.mustReuse(lhsFieldVal, fieldCacheEntry.TypeEntry, nil, nil)
|
||||
}
|
||||
if !rhsOmit {
|
||||
rhsVal = rhsvr.mustReuse(rhsFieldVal, fieldCacheEntry.TypeEntry, nil, nil)
|
||||
}
|
||||
if !fn(fieldCacheEntry.JsonName, lhsVal, rhsVal) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
347
vendor/sigs.k8s.io/structured-merge-diff/v3/value/value.go
generated
vendored
Normal file
347
vendor/sigs.k8s.io/structured-merge-diff/v3/value/value.go
generated
vendored
Normal file
|
|
@ -0,0 +1,347 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
readPool = jsoniter.NewIterator(jsoniter.ConfigCompatibleWithStandardLibrary).Pool()
|
||||
writePool = jsoniter.NewStream(jsoniter.ConfigCompatibleWithStandardLibrary, nil, 1024).Pool()
|
||||
)
|
||||
|
||||
// A Value corresponds to an 'atom' in the schema. It should return true
|
||||
// for at least one of the IsXXX methods below, or the value is
|
||||
// considered "invalid"
|
||||
type Value interface {
|
||||
// IsMap returns true if the Value is a Map, false otherwise.
|
||||
IsMap() bool
|
||||
// IsList returns true if the Value is a List, false otherwise.
|
||||
IsList() bool
|
||||
// IsBool returns true if the Value is a bool, false otherwise.
|
||||
IsBool() bool
|
||||
// IsInt returns true if the Value is a int64, false otherwise.
|
||||
IsInt() bool
|
||||
// IsFloat returns true if the Value is a float64, false
|
||||
// otherwise.
|
||||
IsFloat() bool
|
||||
// IsString returns true if the Value is a string, false
|
||||
// otherwise.
|
||||
IsString() bool
|
||||
// IsMap returns true if the Value is null, false otherwise.
|
||||
IsNull() bool
|
||||
|
||||
// AsMap converts the Value into a Map (or panic if the type
|
||||
// doesn't allow it).
|
||||
AsMap() Map
|
||||
// AsMapUsing uses the provided allocator and converts the Value
|
||||
// into a Map (or panic if the type doesn't allow it).
|
||||
AsMapUsing(Allocator) Map
|
||||
// AsList converts the Value into a List (or panic if the type
|
||||
// doesn't allow it).
|
||||
AsList() List
|
||||
// AsListUsing uses the provided allocator and converts the Value
|
||||
// into a List (or panic if the type doesn't allow it).
|
||||
AsListUsing(Allocator) List
|
||||
// AsBool converts the Value into a bool (or panic if the type
|
||||
// doesn't allow it).
|
||||
AsBool() bool
|
||||
// AsInt converts the Value into an int64 (or panic if the type
|
||||
// doesn't allow it).
|
||||
AsInt() int64
|
||||
// AsFloat converts the Value into a float64 (or panic if the type
|
||||
// doesn't allow it).
|
||||
AsFloat() float64
|
||||
// AsString converts the Value into a string (or panic if the type
|
||||
// doesn't allow it).
|
||||
AsString() string
|
||||
|
||||
// Unstructured converts the Value into an Unstructured interface{}.
|
||||
Unstructured() interface{}
|
||||
}
|
||||
|
||||
// FromJSON is a helper function for reading a JSON document.
|
||||
func FromJSON(input []byte) (Value, error) {
|
||||
return FromJSONFast(input)
|
||||
}
|
||||
|
||||
// FromJSONFast is a helper function for reading a JSON document.
|
||||
func FromJSONFast(input []byte) (Value, error) {
|
||||
iter := readPool.BorrowIterator(input)
|
||||
defer readPool.ReturnIterator(iter)
|
||||
return ReadJSONIter(iter)
|
||||
}
|
||||
|
||||
// ToJSON is a helper function for producing a JSon document.
|
||||
func ToJSON(v Value) ([]byte, error) {
|
||||
buf := bytes.Buffer{}
|
||||
stream := writePool.BorrowStream(&buf)
|
||||
defer writePool.ReturnStream(stream)
|
||||
WriteJSONStream(v, stream)
|
||||
b := stream.Buffer()
|
||||
err := stream.Flush()
|
||||
// Help jsoniter manage its buffers--without this, the next
|
||||
// use of the stream is likely to require an allocation. Look
|
||||
// at the jsoniter stream code to understand why. They were probably
|
||||
// optimizing for folks using the buffer directly.
|
||||
stream.SetBuffer(b[:0])
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
// ReadJSONIter reads a Value from a JSON iterator.
|
||||
func ReadJSONIter(iter *jsoniter.Iterator) (Value, error) {
|
||||
v := iter.Read()
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
return nil, iter.Error
|
||||
}
|
||||
return NewValueInterface(v), nil
|
||||
}
|
||||
|
||||
// WriteJSONStream writes a value into a JSON stream.
|
||||
func WriteJSONStream(v Value, stream *jsoniter.Stream) {
|
||||
stream.WriteVal(v.Unstructured())
|
||||
}
|
||||
|
||||
// ToYAML marshals a value as YAML.
|
||||
func ToYAML(v Value) ([]byte, error) {
|
||||
return yaml.Marshal(v.Unstructured())
|
||||
}
|
||||
|
||||
// Equals returns true iff the two values are equal.
|
||||
func Equals(lhs, rhs Value) bool {
|
||||
return EqualsUsing(HeapAllocator, lhs, rhs)
|
||||
}
|
||||
|
||||
// EqualsUsing uses the provided allocator and returns true iff the two values are equal.
|
||||
func EqualsUsing(a Allocator, lhs, rhs Value) bool {
|
||||
if lhs.IsFloat() || rhs.IsFloat() {
|
||||
var lf float64
|
||||
if lhs.IsFloat() {
|
||||
lf = lhs.AsFloat()
|
||||
} else if lhs.IsInt() {
|
||||
lf = float64(lhs.AsInt())
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
var rf float64
|
||||
if rhs.IsFloat() {
|
||||
rf = rhs.AsFloat()
|
||||
} else if rhs.IsInt() {
|
||||
rf = float64(rhs.AsInt())
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
return lf == rf
|
||||
}
|
||||
if lhs.IsInt() {
|
||||
if rhs.IsInt() {
|
||||
return lhs.AsInt() == rhs.AsInt()
|
||||
}
|
||||
return false
|
||||
} else if rhs.IsInt() {
|
||||
return false
|
||||
}
|
||||
if lhs.IsString() {
|
||||
if rhs.IsString() {
|
||||
return lhs.AsString() == rhs.AsString()
|
||||
}
|
||||
return false
|
||||
} else if rhs.IsString() {
|
||||
return false
|
||||
}
|
||||
if lhs.IsBool() {
|
||||
if rhs.IsBool() {
|
||||
return lhs.AsBool() == rhs.AsBool()
|
||||
}
|
||||
return false
|
||||
} else if rhs.IsBool() {
|
||||
return false
|
||||
}
|
||||
if lhs.IsList() {
|
||||
if rhs.IsList() {
|
||||
lhsList := lhs.AsListUsing(a)
|
||||
defer a.Free(lhsList)
|
||||
rhsList := rhs.AsListUsing(a)
|
||||
defer a.Free(rhsList)
|
||||
return lhsList.EqualsUsing(a, rhsList)
|
||||
}
|
||||
return false
|
||||
} else if rhs.IsList() {
|
||||
return false
|
||||
}
|
||||
if lhs.IsMap() {
|
||||
if rhs.IsMap() {
|
||||
lhsList := lhs.AsMapUsing(a)
|
||||
defer a.Free(lhsList)
|
||||
rhsList := rhs.AsMapUsing(a)
|
||||
defer a.Free(rhsList)
|
||||
return lhsList.EqualsUsing(a, rhsList)
|
||||
}
|
||||
return false
|
||||
} else if rhs.IsMap() {
|
||||
return false
|
||||
}
|
||||
if lhs.IsNull() {
|
||||
if rhs.IsNull() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
} else if rhs.IsNull() {
|
||||
return false
|
||||
}
|
||||
// No field is set, on either objects.
|
||||
return true
|
||||
}
|
||||
|
||||
// ToString returns a human-readable representation of the value.
|
||||
func ToString(v Value) string {
|
||||
if v.IsNull() {
|
||||
return "null"
|
||||
}
|
||||
switch {
|
||||
case v.IsFloat():
|
||||
return fmt.Sprintf("%v", v.AsFloat())
|
||||
case v.IsInt():
|
||||
return fmt.Sprintf("%v", v.AsInt())
|
||||
case v.IsString():
|
||||
return fmt.Sprintf("%q", v.AsString())
|
||||
case v.IsBool():
|
||||
return fmt.Sprintf("%v", v.AsBool())
|
||||
case v.IsList():
|
||||
strs := []string{}
|
||||
list := v.AsList()
|
||||
for i := 0; i < list.Length(); i++ {
|
||||
strs = append(strs, ToString(list.At(i)))
|
||||
}
|
||||
return "[" + strings.Join(strs, ",") + "]"
|
||||
case v.IsMap():
|
||||
strs := []string{}
|
||||
v.AsMap().Iterate(func(k string, v Value) bool {
|
||||
strs = append(strs, fmt.Sprintf("%v=%v", k, ToString(v)))
|
||||
return true
|
||||
})
|
||||
return strings.Join(strs, "")
|
||||
}
|
||||
// No field is set, on either objects.
|
||||
return "{{undefined}}"
|
||||
}
|
||||
|
||||
// Less provides a total ordering for Value (so that they can be sorted, even
|
||||
// if they are of different types).
|
||||
func Less(lhs, rhs Value) bool {
|
||||
return Compare(lhs, rhs) == -1
|
||||
}
|
||||
|
||||
// Compare provides a total ordering for Value (so that they can be
|
||||
// sorted, even if they are of different types). The result will be 0 if
|
||||
// v==rhs, -1 if v < rhs, and +1 if v > rhs.
|
||||
func Compare(lhs, rhs Value) int {
|
||||
return CompareUsing(HeapAllocator, lhs, rhs)
|
||||
}
|
||||
|
||||
// CompareUsing uses the provided allocator and provides a total
|
||||
// ordering for Value (so that they can be sorted, even if they
|
||||
// are of different types). The result will be 0 if v==rhs, -1
|
||||
// if v < rhs, and +1 if v > rhs.
|
||||
func CompareUsing(a Allocator, lhs, rhs Value) int {
|
||||
if lhs.IsFloat() {
|
||||
if !rhs.IsFloat() {
|
||||
// Extra: compare floats and ints numerically.
|
||||
if rhs.IsInt() {
|
||||
return FloatCompare(lhs.AsFloat(), float64(rhs.AsInt()))
|
||||
}
|
||||
return -1
|
||||
}
|
||||
return FloatCompare(lhs.AsFloat(), rhs.AsFloat())
|
||||
} else if rhs.IsFloat() {
|
||||
// Extra: compare floats and ints numerically.
|
||||
if lhs.IsInt() {
|
||||
return FloatCompare(float64(lhs.AsInt()), rhs.AsFloat())
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
if lhs.IsInt() {
|
||||
if !rhs.IsInt() {
|
||||
return -1
|
||||
}
|
||||
return IntCompare(lhs.AsInt(), rhs.AsInt())
|
||||
} else if rhs.IsInt() {
|
||||
return 1
|
||||
}
|
||||
|
||||
if lhs.IsString() {
|
||||
if !rhs.IsString() {
|
||||
return -1
|
||||
}
|
||||
return strings.Compare(lhs.AsString(), rhs.AsString())
|
||||
} else if rhs.IsString() {
|
||||
return 1
|
||||
}
|
||||
|
||||
if lhs.IsBool() {
|
||||
if !rhs.IsBool() {
|
||||
return -1
|
||||
}
|
||||
return BoolCompare(lhs.AsBool(), rhs.AsBool())
|
||||
} else if rhs.IsBool() {
|
||||
return 1
|
||||
}
|
||||
|
||||
if lhs.IsList() {
|
||||
if !rhs.IsList() {
|
||||
return -1
|
||||
}
|
||||
lhsList := lhs.AsListUsing(a)
|
||||
defer a.Free(lhsList)
|
||||
rhsList := rhs.AsListUsing(a)
|
||||
defer a.Free(rhsList)
|
||||
return ListCompareUsing(a, lhsList, rhsList)
|
||||
} else if rhs.IsList() {
|
||||
return 1
|
||||
}
|
||||
if lhs.IsMap() {
|
||||
if !rhs.IsMap() {
|
||||
return -1
|
||||
}
|
||||
lhsMap := lhs.AsMapUsing(a)
|
||||
defer a.Free(lhsMap)
|
||||
rhsMap := rhs.AsMapUsing(a)
|
||||
defer a.Free(rhsMap)
|
||||
return MapCompareUsing(a, lhsMap, rhsMap)
|
||||
} else if rhs.IsMap() {
|
||||
return 1
|
||||
}
|
||||
if lhs.IsNull() {
|
||||
if !rhs.IsNull() {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
} else if rhs.IsNull() {
|
||||
return 1
|
||||
}
|
||||
|
||||
// Invalid Value-- nothing is set.
|
||||
return 0
|
||||
}
|
||||
294
vendor/sigs.k8s.io/structured-merge-diff/v3/value/valuereflect.go
generated
vendored
Normal file
294
vendor/sigs.k8s.io/structured-merge-diff/v3/value/valuereflect.go
generated
vendored
Normal file
|
|
@ -0,0 +1,294 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// NewValueReflect creates a Value backed by an "interface{}" type,
|
||||
// typically an structured object in Kubernetes world that is uses reflection to expose.
|
||||
// The provided "interface{}" value must be a pointer so that the value can be modified via reflection.
|
||||
// The provided "interface{}" may contain structs and types that are converted to Values
|
||||
// by the jsonMarshaler interface.
|
||||
func NewValueReflect(value interface{}) (Value, error) {
|
||||
if value == nil {
|
||||
return NewValueInterface(nil), nil
|
||||
}
|
||||
v := reflect.ValueOf(value)
|
||||
if v.Kind() != reflect.Ptr {
|
||||
// The root value to reflect on must be a pointer so that map.Set() and map.Delete() operations are possible.
|
||||
return nil, fmt.Errorf("value provided to NewValueReflect must be a pointer")
|
||||
}
|
||||
return wrapValueReflect(v, nil, nil)
|
||||
}
|
||||
|
||||
// wrapValueReflect wraps the provide reflect.Value as a value. If parent in the data tree is a map, parentMap
|
||||
// and parentMapKey must be provided so that the returned value may be set and deleted.
|
||||
func wrapValueReflect(value reflect.Value, parentMap, parentMapKey *reflect.Value) (Value, error) {
|
||||
val := HeapAllocator.allocValueReflect()
|
||||
return val.reuse(value, nil, parentMap, parentMapKey)
|
||||
}
|
||||
|
||||
// wrapValueReflect wraps the provide reflect.Value as a value, and panics if there is an error. If parent in the data
|
||||
// tree is a map, parentMap and parentMapKey must be provided so that the returned value may be set and deleted.
|
||||
func mustWrapValueReflect(value reflect.Value, parentMap, parentMapKey *reflect.Value) Value {
|
||||
v, err := wrapValueReflect(value, parentMap, parentMapKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// the value interface doesn't care about the type for value.IsNull, so we can use a constant
|
||||
var nilType = reflect.TypeOf(&struct{}{})
|
||||
|
||||
// reuse replaces the value of the valueReflect. If parent in the data tree is a map, parentMap and parentMapKey
|
||||
// must be provided so that the returned value may be set and deleted.
|
||||
func (r *valueReflect) reuse(value reflect.Value, cacheEntry *TypeReflectCacheEntry, parentMap, parentMapKey *reflect.Value) (Value, error) {
|
||||
if cacheEntry == nil {
|
||||
cacheEntry = TypeReflectEntryOf(value.Type())
|
||||
}
|
||||
if cacheEntry.CanConvertToUnstructured() {
|
||||
u, err := cacheEntry.ToUnstructured(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if u == nil {
|
||||
value = reflect.Zero(nilType)
|
||||
} else {
|
||||
value = reflect.ValueOf(u)
|
||||
}
|
||||
}
|
||||
r.Value = dereference(value)
|
||||
r.ParentMap = parentMap
|
||||
r.ParentMapKey = parentMapKey
|
||||
r.kind = kind(r.Value)
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// mustReuse replaces the value of the valueReflect and panics if there is an error. If parent in the data tree is a
|
||||
// map, parentMap and parentMapKey must be provided so that the returned value may be set and deleted.
|
||||
func (r *valueReflect) mustReuse(value reflect.Value, cacheEntry *TypeReflectCacheEntry, parentMap, parentMapKey *reflect.Value) Value {
|
||||
v, err := r.reuse(value, cacheEntry, parentMap, parentMapKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func dereference(val reflect.Value) reflect.Value {
|
||||
kind := val.Kind()
|
||||
if (kind == reflect.Interface || kind == reflect.Ptr) && !safeIsNil(val) {
|
||||
return val.Elem()
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
type valueReflect struct {
|
||||
ParentMap *reflect.Value
|
||||
ParentMapKey *reflect.Value
|
||||
Value reflect.Value
|
||||
kind reflectType
|
||||
}
|
||||
|
||||
func (r valueReflect) IsMap() bool {
|
||||
return r.kind == mapType || r.kind == structMapType
|
||||
}
|
||||
|
||||
func (r valueReflect) IsList() bool {
|
||||
return r.kind == listType
|
||||
}
|
||||
|
||||
func (r valueReflect) IsBool() bool {
|
||||
return r.kind == boolType
|
||||
}
|
||||
|
||||
func (r valueReflect) IsInt() bool {
|
||||
return r.kind == intType || r.kind == uintType
|
||||
}
|
||||
|
||||
func (r valueReflect) IsFloat() bool {
|
||||
return r.kind == floatType
|
||||
}
|
||||
|
||||
func (r valueReflect) IsString() bool {
|
||||
return r.kind == stringType || r.kind == byteStringType
|
||||
}
|
||||
|
||||
func (r valueReflect) IsNull() bool {
|
||||
return r.kind == nullType
|
||||
}
|
||||
|
||||
type reflectType = int
|
||||
|
||||
const (
|
||||
mapType = iota
|
||||
structMapType
|
||||
listType
|
||||
intType
|
||||
uintType
|
||||
floatType
|
||||
stringType
|
||||
byteStringType
|
||||
boolType
|
||||
nullType
|
||||
)
|
||||
|
||||
func kind(v reflect.Value) reflectType {
|
||||
typ := v.Type()
|
||||
rk := typ.Kind()
|
||||
switch rk {
|
||||
case reflect.Map:
|
||||
if v.IsNil() {
|
||||
return nullType
|
||||
}
|
||||
return mapType
|
||||
case reflect.Struct:
|
||||
return structMapType
|
||||
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
|
||||
return intType
|
||||
case reflect.Uint, reflect.Uint32, reflect.Uint16, reflect.Uint8:
|
||||
// Uint64 deliberately excluded, see valueUnstructured.Int.
|
||||
return uintType
|
||||
case reflect.Float64, reflect.Float32:
|
||||
return floatType
|
||||
case reflect.String:
|
||||
return stringType
|
||||
case reflect.Bool:
|
||||
return boolType
|
||||
case reflect.Slice:
|
||||
if v.IsNil() {
|
||||
return nullType
|
||||
}
|
||||
elemKind := typ.Elem().Kind()
|
||||
if elemKind == reflect.Uint8 {
|
||||
return byteStringType
|
||||
}
|
||||
return listType
|
||||
case reflect.Chan, reflect.Func, reflect.Ptr, reflect.UnsafePointer, reflect.Interface:
|
||||
if v.IsNil() {
|
||||
return nullType
|
||||
}
|
||||
panic(fmt.Sprintf("unsupported type: %v", v.Type()))
|
||||
default:
|
||||
panic(fmt.Sprintf("unsupported type: %v", v.Type()))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO find a cleaner way to avoid panics from reflect.IsNil()
|
||||
func safeIsNil(v reflect.Value) bool {
|
||||
k := v.Kind()
|
||||
switch k {
|
||||
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
|
||||
return v.IsNil()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r valueReflect) AsMap() Map {
|
||||
return r.AsMapUsing(HeapAllocator)
|
||||
}
|
||||
|
||||
func (r valueReflect) AsMapUsing(a Allocator) Map {
|
||||
switch r.kind {
|
||||
case structMapType:
|
||||
v := a.allocStructReflect()
|
||||
v.valueReflect = r
|
||||
return v
|
||||
case mapType:
|
||||
v := a.allocMapReflect()
|
||||
v.valueReflect = r
|
||||
return v
|
||||
default:
|
||||
panic("value is not a map or struct")
|
||||
}
|
||||
}
|
||||
|
||||
func (r valueReflect) AsList() List {
|
||||
return r.AsListUsing(HeapAllocator)
|
||||
}
|
||||
|
||||
func (r valueReflect) AsListUsing(a Allocator) List {
|
||||
if r.IsList() {
|
||||
v := a.allocListReflect()
|
||||
v.Value = r.Value
|
||||
return v
|
||||
}
|
||||
panic("value is not a list")
|
||||
}
|
||||
|
||||
func (r valueReflect) AsBool() bool {
|
||||
if r.IsBool() {
|
||||
return r.Value.Bool()
|
||||
}
|
||||
panic("value is not a bool")
|
||||
}
|
||||
|
||||
func (r valueReflect) AsInt() int64 {
|
||||
if r.kind == intType {
|
||||
return r.Value.Int()
|
||||
}
|
||||
if r.kind == uintType {
|
||||
return int64(r.Value.Uint())
|
||||
}
|
||||
|
||||
panic("value is not an int")
|
||||
}
|
||||
|
||||
func (r valueReflect) AsFloat() float64 {
|
||||
if r.IsFloat() {
|
||||
return r.Value.Float()
|
||||
}
|
||||
panic("value is not a float")
|
||||
}
|
||||
|
||||
func (r valueReflect) AsString() string {
|
||||
switch r.kind {
|
||||
case stringType:
|
||||
return r.Value.String()
|
||||
case byteStringType:
|
||||
return base64.StdEncoding.EncodeToString(r.Value.Bytes())
|
||||
}
|
||||
panic("value is not a string")
|
||||
}
|
||||
|
||||
func (r valueReflect) Unstructured() interface{} {
|
||||
val := r.Value
|
||||
switch {
|
||||
case r.IsNull():
|
||||
return nil
|
||||
case val.Kind() == reflect.Struct:
|
||||
return structReflect{r}.Unstructured()
|
||||
case val.Kind() == reflect.Map:
|
||||
return mapReflect{valueReflect: r}.Unstructured()
|
||||
case r.IsList():
|
||||
return listReflect{r.Value}.Unstructured()
|
||||
case r.IsString():
|
||||
return r.AsString()
|
||||
case r.IsInt():
|
||||
return r.AsInt()
|
||||
case r.IsBool():
|
||||
return r.AsBool()
|
||||
case r.IsFloat():
|
||||
return r.AsFloat()
|
||||
default:
|
||||
panic(fmt.Sprintf("value of type %s is not a supported by value reflector", val.Type()))
|
||||
}
|
||||
}
|
||||
178
vendor/sigs.k8s.io/structured-merge-diff/v3/value/valueunstructured.go
generated
vendored
Normal file
178
vendor/sigs.k8s.io/structured-merge-diff/v3/value/valueunstructured.go
generated
vendored
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// NewValueInterface creates a Value backed by an "interface{}" type,
|
||||
// typically an unstructured object in Kubernetes world.
|
||||
// interface{} must be one of: map[string]interface{}, map[interface{}]interface{}, []interface{}, int types, float types,
|
||||
// string or boolean. Nested interface{} must also be one of these types.
|
||||
func NewValueInterface(v interface{}) Value {
|
||||
return Value(HeapAllocator.allocValueUnstructured().reuse(v))
|
||||
}
|
||||
|
||||
type valueUnstructured struct {
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
// reuse replaces the value of the valueUnstructured.
|
||||
func (vi *valueUnstructured) reuse(value interface{}) Value {
|
||||
vi.Value = value
|
||||
return vi
|
||||
}
|
||||
|
||||
func (v valueUnstructured) IsMap() bool {
|
||||
if _, ok := v.Value.(map[string]interface{}); ok {
|
||||
return true
|
||||
}
|
||||
if _, ok := v.Value.(map[interface{}]interface{}); ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (v valueUnstructured) AsMap() Map {
|
||||
return v.AsMapUsing(HeapAllocator)
|
||||
}
|
||||
|
||||
func (v valueUnstructured) AsMapUsing(_ Allocator) Map {
|
||||
if v.Value == nil {
|
||||
panic("invalid nil")
|
||||
}
|
||||
switch t := v.Value.(type) {
|
||||
case map[string]interface{}:
|
||||
return mapUnstructuredString(t)
|
||||
case map[interface{}]interface{}:
|
||||
return mapUnstructuredInterface(t)
|
||||
}
|
||||
panic(fmt.Errorf("not a map: %#v", v))
|
||||
}
|
||||
|
||||
func (v valueUnstructured) IsList() bool {
|
||||
if v.Value == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := v.Value.([]interface{})
|
||||
return ok
|
||||
}
|
||||
|
||||
func (v valueUnstructured) AsList() List {
|
||||
return v.AsListUsing(HeapAllocator)
|
||||
}
|
||||
|
||||
func (v valueUnstructured) AsListUsing(_ Allocator) List {
|
||||
return listUnstructured(v.Value.([]interface{}))
|
||||
}
|
||||
|
||||
func (v valueUnstructured) IsFloat() bool {
|
||||
if v.Value == nil {
|
||||
return false
|
||||
} else if _, ok := v.Value.(float64); ok {
|
||||
return true
|
||||
} else if _, ok := v.Value.(float32); ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (v valueUnstructured) AsFloat() float64 {
|
||||
if f, ok := v.Value.(float32); ok {
|
||||
return float64(f)
|
||||
}
|
||||
return v.Value.(float64)
|
||||
}
|
||||
|
||||
func (v valueUnstructured) IsInt() bool {
|
||||
if v.Value == nil {
|
||||
return false
|
||||
} else if _, ok := v.Value.(int); ok {
|
||||
return true
|
||||
} else if _, ok := v.Value.(int8); ok {
|
||||
return true
|
||||
} else if _, ok := v.Value.(int16); ok {
|
||||
return true
|
||||
} else if _, ok := v.Value.(int32); ok {
|
||||
return true
|
||||
} else if _, ok := v.Value.(int64); ok {
|
||||
return true
|
||||
} else if _, ok := v.Value.(uint); ok {
|
||||
return true
|
||||
} else if _, ok := v.Value.(uint8); ok {
|
||||
return true
|
||||
} else if _, ok := v.Value.(uint16); ok {
|
||||
return true
|
||||
} else if _, ok := v.Value.(uint32); ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (v valueUnstructured) AsInt() int64 {
|
||||
if i, ok := v.Value.(int); ok {
|
||||
return int64(i)
|
||||
} else if i, ok := v.Value.(int8); ok {
|
||||
return int64(i)
|
||||
} else if i, ok := v.Value.(int16); ok {
|
||||
return int64(i)
|
||||
} else if i, ok := v.Value.(int32); ok {
|
||||
return int64(i)
|
||||
} else if i, ok := v.Value.(uint); ok {
|
||||
return int64(i)
|
||||
} else if i, ok := v.Value.(uint8); ok {
|
||||
return int64(i)
|
||||
} else if i, ok := v.Value.(uint16); ok {
|
||||
return int64(i)
|
||||
} else if i, ok := v.Value.(uint32); ok {
|
||||
return int64(i)
|
||||
}
|
||||
return v.Value.(int64)
|
||||
}
|
||||
|
||||
func (v valueUnstructured) IsString() bool {
|
||||
if v.Value == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := v.Value.(string)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (v valueUnstructured) AsString() string {
|
||||
return v.Value.(string)
|
||||
}
|
||||
|
||||
func (v valueUnstructured) IsBool() bool {
|
||||
if v.Value == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := v.Value.(bool)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (v valueUnstructured) AsBool() bool {
|
||||
return v.Value.(bool)
|
||||
}
|
||||
|
||||
func (v valueUnstructured) IsNull() bool {
|
||||
return v.Value == nil
|
||||
}
|
||||
|
||||
func (v valueUnstructured) Unstructured() interface{} {
|
||||
return v.Value
|
||||
}
|
||||
149
vendor/sigs.k8s.io/structured-merge-diff/value/fastjson.go
generated
vendored
149
vendor/sigs.k8s.io/structured-merge-diff/value/fastjson.go
generated
vendored
|
|
@ -1,149 +0,0 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var (
|
||||
readPool = jsoniter.NewIterator(jsoniter.ConfigCompatibleWithStandardLibrary).Pool()
|
||||
writePool = jsoniter.NewStream(jsoniter.ConfigCompatibleWithStandardLibrary, nil, 1024).Pool()
|
||||
)
|
||||
|
||||
// FromJSONFast is a helper function for reading a JSON document
|
||||
func FromJSONFast(input []byte) (Value, error) {
|
||||
iter := readPool.BorrowIterator(input)
|
||||
defer readPool.ReturnIterator(iter)
|
||||
return ReadJSONIter(iter)
|
||||
}
|
||||
|
||||
func ReadJSONIter(iter *jsoniter.Iterator) (Value, error) {
|
||||
next := iter.WhatIsNext()
|
||||
switch next {
|
||||
case jsoniter.InvalidValue:
|
||||
iter.ReportError("reading an object", "got invalid token")
|
||||
return Value{}, iter.Error
|
||||
case jsoniter.StringValue:
|
||||
str := String(iter.ReadString())
|
||||
return Value{StringValue: &str}, nil
|
||||
case jsoniter.NumberValue:
|
||||
number := iter.ReadNumber()
|
||||
isFloat := false
|
||||
for _, c := range number {
|
||||
if c == 'e' || c == 'E' || c == '.' {
|
||||
isFloat = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if isFloat {
|
||||
f, err := number.Float64()
|
||||
if err != nil {
|
||||
iter.ReportError("parsing as float", err.Error())
|
||||
return Value{}, err
|
||||
}
|
||||
return Value{FloatValue: (*Float)(&f)}, nil
|
||||
}
|
||||
i, err := number.Int64()
|
||||
if err != nil {
|
||||
iter.ReportError("parsing as float", err.Error())
|
||||
return Value{}, err
|
||||
}
|
||||
return Value{IntValue: (*Int)(&i)}, nil
|
||||
case jsoniter.NilValue:
|
||||
iter.ReadNil()
|
||||
return Value{Null: true}, nil
|
||||
case jsoniter.BoolValue:
|
||||
b := Boolean(iter.ReadBool())
|
||||
return Value{BooleanValue: &b}, nil
|
||||
case jsoniter.ArrayValue:
|
||||
list := &List{}
|
||||
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool {
|
||||
v, err := ReadJSONIter(iter)
|
||||
if err != nil {
|
||||
iter.Error = err
|
||||
return false
|
||||
}
|
||||
list.Items = append(list.Items, v)
|
||||
return true
|
||||
})
|
||||
return Value{ListValue: list}, iter.Error
|
||||
case jsoniter.ObjectValue:
|
||||
m := &Map{}
|
||||
iter.ReadObjectCB(func(iter *jsoniter.Iterator, key string) bool {
|
||||
v, err := ReadJSONIter(iter)
|
||||
if err != nil {
|
||||
iter.Error = err
|
||||
return false
|
||||
}
|
||||
m.Items = append(m.Items, Field{Name: key, Value: v})
|
||||
return true
|
||||
})
|
||||
return Value{MapValue: m}, iter.Error
|
||||
default:
|
||||
return Value{}, fmt.Errorf("unexpected object type %v", next)
|
||||
}
|
||||
}
|
||||
|
||||
// ToJSONFast is a helper function for producing a JSon document.
|
||||
func (v *Value) ToJSONFast() ([]byte, error) {
|
||||
buf := bytes.Buffer{}
|
||||
stream := writePool.BorrowStream(&buf)
|
||||
defer writePool.ReturnStream(stream)
|
||||
v.WriteJSONStream(stream)
|
||||
err := stream.Flush()
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
func (v *Value) WriteJSONStream(stream *jsoniter.Stream) {
|
||||
switch {
|
||||
case v.Null:
|
||||
stream.WriteNil()
|
||||
case v.FloatValue != nil:
|
||||
stream.WriteFloat64(float64(*v.FloatValue))
|
||||
case v.IntValue != nil:
|
||||
stream.WriteInt64(int64(*v.IntValue))
|
||||
case v.BooleanValue != nil:
|
||||
stream.WriteBool(bool(*v.BooleanValue))
|
||||
case v.StringValue != nil:
|
||||
stream.WriteString(string(*v.StringValue))
|
||||
case v.ListValue != nil:
|
||||
stream.WriteArrayStart()
|
||||
for i := range v.ListValue.Items {
|
||||
if i > 0 {
|
||||
stream.WriteMore()
|
||||
}
|
||||
v.ListValue.Items[i].WriteJSONStream(stream)
|
||||
}
|
||||
stream.WriteArrayEnd()
|
||||
case v.MapValue != nil:
|
||||
stream.WriteObjectStart()
|
||||
for i := range v.MapValue.Items {
|
||||
if i > 0 {
|
||||
stream.WriteMore()
|
||||
}
|
||||
stream.WriteObjectField(v.MapValue.Items[i].Name)
|
||||
v.MapValue.Items[i].Value.WriteJSONStream(stream)
|
||||
}
|
||||
stream.WriteObjectEnd()
|
||||
default:
|
||||
stream.Write([]byte("invalid_value"))
|
||||
}
|
||||
}
|
||||
234
vendor/sigs.k8s.io/structured-merge-diff/value/unstructured.go
generated
vendored
234
vendor/sigs.k8s.io/structured-merge-diff/value/unstructured.go
generated
vendored
|
|
@ -1,234 +0,0 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// FromYAML is a helper function for reading a YAML document; it attempts to
|
||||
// preserve order of keys within maps/structs. This is as a convenience to
|
||||
// humans keeping YAML documents, not because there is a behavior difference.
|
||||
//
|
||||
// Known bug: objects with top-level arrays don't parse correctly.
|
||||
func FromYAML(input []byte) (Value, error) {
|
||||
var decoded interface{}
|
||||
|
||||
if len(input) == 4 && string(input) == "null" {
|
||||
// Special case since the yaml package doesn't accurately
|
||||
// preserve this.
|
||||
return Value{Null: true}, nil
|
||||
}
|
||||
|
||||
// This attempts to enable order sensitivity; note the yaml package is
|
||||
// broken for documents that have root-level arrays, hence the two-step
|
||||
// approach. TODO: This is a horrific hack. Is it worth it?
|
||||
var ms yaml.MapSlice
|
||||
if err := yaml.Unmarshal(input, &ms); err == nil {
|
||||
decoded = ms
|
||||
} else if err := yaml.Unmarshal(input, &decoded); err != nil {
|
||||
return Value{}, err
|
||||
}
|
||||
|
||||
v, err := FromUnstructured(decoded)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("failed to interpret (%v):\n%s", err, input)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// FromJSON is a helper function for reading a JSON document
|
||||
func FromJSON(input []byte) (Value, error) {
|
||||
var decoded interface{}
|
||||
|
||||
if err := json.Unmarshal(input, &decoded); err != nil {
|
||||
return Value{}, err
|
||||
}
|
||||
|
||||
v, err := FromUnstructured(decoded)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("failed to interpret (%v):\n%s", err, input)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// FromUnstructured will convert a go interface to a Value.
|
||||
// It's most commonly expected to be used with map[string]interface{} as the
|
||||
// input. `in` must not have any structures with cycles in them.
|
||||
// yaml.MapSlice may be used for order-preservation.
|
||||
func FromUnstructured(in interface{}) (Value, error) {
|
||||
if in == nil {
|
||||
return Value{Null: true}, nil
|
||||
}
|
||||
switch t := in.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
m := Map{}
|
||||
for rawKey, rawVal := range t {
|
||||
k, ok := rawKey.(string)
|
||||
if !ok {
|
||||
return Value{}, fmt.Errorf("key %#v: not a string", k)
|
||||
}
|
||||
v, err := FromUnstructured(rawVal)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("key %v: %v", k, err)
|
||||
}
|
||||
m.Set(k, v)
|
||||
}
|
||||
return Value{MapValue: &m}, nil
|
||||
case map[string]interface{}:
|
||||
m := Map{}
|
||||
for k, rawVal := range t {
|
||||
v, err := FromUnstructured(rawVal)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("key %v: %v", k, err)
|
||||
}
|
||||
m.Set(k, v)
|
||||
}
|
||||
return Value{MapValue: &m}, nil
|
||||
case yaml.MapSlice:
|
||||
m := Map{}
|
||||
for _, item := range t {
|
||||
k, ok := item.Key.(string)
|
||||
if !ok {
|
||||
return Value{}, fmt.Errorf("key %#v is not a string", item.Key)
|
||||
}
|
||||
v, err := FromUnstructured(item.Value)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("key %v: %v", k, err)
|
||||
}
|
||||
m.Set(k, v)
|
||||
}
|
||||
return Value{MapValue: &m}, nil
|
||||
case []interface{}:
|
||||
l := List{}
|
||||
for i, rawVal := range t {
|
||||
v, err := FromUnstructured(rawVal)
|
||||
if err != nil {
|
||||
return Value{}, fmt.Errorf("index %v: %v", i, err)
|
||||
}
|
||||
l.Items = append(l.Items, v)
|
||||
}
|
||||
return Value{ListValue: &l}, nil
|
||||
case int:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case int8:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case int16:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case int32:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case int64:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case uint:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case uint8:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case uint16:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case uint32:
|
||||
n := Int(t)
|
||||
return Value{IntValue: &n}, nil
|
||||
case float32:
|
||||
f := Float(t)
|
||||
return Value{FloatValue: &f}, nil
|
||||
case float64:
|
||||
f := Float(t)
|
||||
return Value{FloatValue: &f}, nil
|
||||
case string:
|
||||
return StringValue(t), nil
|
||||
case bool:
|
||||
return BooleanValue(t), nil
|
||||
default:
|
||||
return Value{}, fmt.Errorf("type unimplemented: %t", in)
|
||||
}
|
||||
}
|
||||
|
||||
// ToYAML is a helper function for producing a YAML document; it attempts to
|
||||
// preserve order of keys within maps/structs. This is as a convenience to
|
||||
// humans keeping YAML documents, not because there is a behavior difference.
|
||||
func (v *Value) ToYAML() ([]byte, error) {
|
||||
return yaml.Marshal(v.ToUnstructured(true))
|
||||
}
|
||||
|
||||
// ToJSON is a helper function for producing a JSon document.
|
||||
func (v *Value) ToJSON() ([]byte, error) {
|
||||
return json.Marshal(v.ToUnstructured(false))
|
||||
}
|
||||
|
||||
// ToUnstructured will convert the Value into a go-typed object.
|
||||
// If preserveOrder is true, then maps will be converted to the yaml.MapSlice
|
||||
// type. Otherwise, map[string]interface{} must be used-- this destroys
|
||||
// ordering information and is not recommended if the result of this will be
|
||||
// serialized. Other types:
|
||||
// * list -> []interface{}
|
||||
// * others -> corresponding go type, wrapped in an interface{}
|
||||
//
|
||||
// Of note, floats and ints will always come out as float64 and int64,
|
||||
// respectively.
|
||||
func (v *Value) ToUnstructured(preserveOrder bool) interface{} {
|
||||
switch {
|
||||
case v.FloatValue != nil:
|
||||
f := float64(*v.FloatValue)
|
||||
return f
|
||||
case v.IntValue != nil:
|
||||
i := int64(*v.IntValue)
|
||||
return i
|
||||
case v.StringValue != nil:
|
||||
return string(*v.StringValue)
|
||||
case v.BooleanValue != nil:
|
||||
return bool(*v.BooleanValue)
|
||||
case v.ListValue != nil:
|
||||
out := []interface{}{}
|
||||
for _, item := range v.ListValue.Items {
|
||||
out = append(out, item.ToUnstructured(preserveOrder))
|
||||
}
|
||||
return out
|
||||
case v.MapValue != nil:
|
||||
m := v.MapValue
|
||||
if preserveOrder {
|
||||
ms := make(yaml.MapSlice, len(m.Items))
|
||||
for i := range m.Items {
|
||||
ms[i] = yaml.MapItem{
|
||||
Key: m.Items[i].Name,
|
||||
Value: m.Items[i].Value.ToUnstructured(preserveOrder),
|
||||
}
|
||||
}
|
||||
return ms
|
||||
}
|
||||
// This case is unavoidably lossy.
|
||||
out := map[string]interface{}{}
|
||||
for i := range m.Items {
|
||||
out[m.Items[i].Name] = m.Items[i].Value.ToUnstructured(preserveOrder)
|
||||
}
|
||||
return out
|
||||
default:
|
||||
fallthrough
|
||||
case v.Null == true:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
538
vendor/sigs.k8s.io/structured-merge-diff/value/value.go
generated
vendored
538
vendor/sigs.k8s.io/structured-merge-diff/value/value.go
generated
vendored
|
|
@ -1,538 +0,0 @@
|
|||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package value
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A Value is an object; it corresponds to an 'atom' in the schema.
|
||||
type Value struct {
|
||||
// Exactly one of the below must be set.
|
||||
FloatValue *Float
|
||||
IntValue *Int
|
||||
StringValue *String
|
||||
BooleanValue *Boolean
|
||||
ListValue *List
|
||||
MapValue *Map
|
||||
Null bool // represents an explicit `"foo" = null`
|
||||
}
|
||||
|
||||
// Equals returns true iff the two values are equal.
|
||||
func (v Value) Equals(rhs Value) bool {
|
||||
if v.FloatValue != nil || rhs.FloatValue != nil {
|
||||
var lf float64
|
||||
if v.FloatValue != nil {
|
||||
lf = float64(*v.FloatValue)
|
||||
} else if v.IntValue != nil {
|
||||
lf = float64(*v.IntValue)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
var rf float64
|
||||
if rhs.FloatValue != nil {
|
||||
rf = float64(*rhs.FloatValue)
|
||||
} else if rhs.IntValue != nil {
|
||||
rf = float64(*rhs.IntValue)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
return lf == rf
|
||||
}
|
||||
if v.IntValue != nil {
|
||||
if rhs.IntValue != nil {
|
||||
return *v.IntValue == *rhs.IntValue
|
||||
}
|
||||
return false
|
||||
}
|
||||
if v.StringValue != nil {
|
||||
if rhs.StringValue != nil {
|
||||
return *v.StringValue == *rhs.StringValue
|
||||
}
|
||||
return false
|
||||
}
|
||||
if v.BooleanValue != nil {
|
||||
if rhs.BooleanValue != nil {
|
||||
return *v.BooleanValue == *rhs.BooleanValue
|
||||
}
|
||||
return false
|
||||
}
|
||||
if v.ListValue != nil {
|
||||
if rhs.ListValue != nil {
|
||||
return v.ListValue.Equals(rhs.ListValue)
|
||||
}
|
||||
return false
|
||||
}
|
||||
if v.MapValue != nil {
|
||||
if rhs.MapValue != nil {
|
||||
return v.MapValue.Equals(rhs.MapValue)
|
||||
}
|
||||
return false
|
||||
}
|
||||
if v.Null {
|
||||
if rhs.Null {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
// No field is set, on either objects.
|
||||
return true
|
||||
}
|
||||
|
||||
// Less provides a total ordering for Value (so that they can be sorted, even
|
||||
// if they are of different types).
|
||||
func (v Value) Less(rhs Value) bool {
|
||||
return v.Compare(rhs) == -1
|
||||
}
|
||||
|
||||
// Compare provides a total ordering for Value (so that they can be
|
||||
// sorted, even if they are of different types). The result will be 0 if
|
||||
// v==rhs, -1 if v < rhs, and +1 if v > rhs.
|
||||
func (v Value) Compare(rhs Value) int {
|
||||
if v.FloatValue != nil {
|
||||
if rhs.FloatValue == nil {
|
||||
// Extra: compare floats and ints numerically.
|
||||
if rhs.IntValue != nil {
|
||||
return v.FloatValue.Compare(Float(*rhs.IntValue))
|
||||
}
|
||||
return -1
|
||||
}
|
||||
return v.FloatValue.Compare(*rhs.FloatValue)
|
||||
} else if rhs.FloatValue != nil {
|
||||
// Extra: compare floats and ints numerically.
|
||||
if v.IntValue != nil {
|
||||
return Float(*v.IntValue).Compare(*rhs.FloatValue)
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
if v.IntValue != nil {
|
||||
if rhs.IntValue == nil {
|
||||
return -1
|
||||
}
|
||||
return v.IntValue.Compare(*rhs.IntValue)
|
||||
} else if rhs.IntValue != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
if v.StringValue != nil {
|
||||
if rhs.StringValue == nil {
|
||||
return -1
|
||||
}
|
||||
return strings.Compare(string(*v.StringValue), string(*rhs.StringValue))
|
||||
} else if rhs.StringValue != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
if v.BooleanValue != nil {
|
||||
if rhs.BooleanValue == nil {
|
||||
return -1
|
||||
}
|
||||
return v.BooleanValue.Compare(*rhs.BooleanValue)
|
||||
} else if rhs.BooleanValue != nil {
|
||||
return 1
|
||||
}
|
||||
|
||||
if v.ListValue != nil {
|
||||
if rhs.ListValue == nil {
|
||||
return -1
|
||||
}
|
||||
return v.ListValue.Compare(rhs.ListValue)
|
||||
} else if rhs.ListValue != nil {
|
||||
return 1
|
||||
}
|
||||
if v.MapValue != nil {
|
||||
if rhs.MapValue == nil {
|
||||
return -1
|
||||
}
|
||||
return v.MapValue.Compare(rhs.MapValue)
|
||||
} else if rhs.MapValue != nil {
|
||||
return 1
|
||||
}
|
||||
if v.Null {
|
||||
if !rhs.Null {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
} else if rhs.Null {
|
||||
return 1
|
||||
}
|
||||
|
||||
// Invalid Value-- nothing is set.
|
||||
return 0
|
||||
}
|
||||
|
||||
type Int int64
|
||||
type Float float64
|
||||
type String string
|
||||
type Boolean bool
|
||||
|
||||
// Compare compares integers. The result will be 0 if i==rhs, -1 if i <
|
||||
// rhs, and +1 if i > rhs.
|
||||
func (i Int) Compare(rhs Int) int {
|
||||
if i > rhs {
|
||||
return 1
|
||||
} else if i < rhs {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Compare compares floats. The result will be 0 if f==rhs, -1 if f <
|
||||
// rhs, and +1 if f > rhs.
|
||||
func (f Float) Compare(rhs Float) int {
|
||||
if f > rhs {
|
||||
return 1
|
||||
} else if f < rhs {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Compare compares booleans. The result will be 0 if b==rhs, -1 if b <
|
||||
// rhs, and +1 if b > rhs.
|
||||
func (b Boolean) Compare(rhs Boolean) int {
|
||||
if b == rhs {
|
||||
return 0
|
||||
} else if b == false {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// Field is an individual key-value pair.
|
||||
type Field struct {
|
||||
Name string
|
||||
Value Value
|
||||
}
|
||||
|
||||
// FieldList is a list of key-value pairs. Each field is expected to
|
||||
// have a different name.
|
||||
type FieldList []Field
|
||||
|
||||
// Sort sorts the field list by Name.
|
||||
func (f FieldList) Sort() {
|
||||
if len(f) < 2 {
|
||||
return
|
||||
}
|
||||
if len(f) == 2 {
|
||||
if f[1].Name < f[0].Name {
|
||||
f[0], f[1] = f[1], f[0]
|
||||
}
|
||||
return
|
||||
}
|
||||
sort.SliceStable(f, func(i, j int) bool {
|
||||
return f[i].Name < f[j].Name
|
||||
})
|
||||
}
|
||||
|
||||
// Less compares two lists lexically.
|
||||
func (f FieldList) Less(rhs FieldList) bool {
|
||||
return f.Compare(rhs) == -1
|
||||
}
|
||||
|
||||
// Less compares two lists lexically. The result will be 0 if f==rhs, -1
|
||||
// if f < rhs, and +1 if f > rhs.
|
||||
func (f FieldList) Compare(rhs FieldList) int {
|
||||
i := 0
|
||||
for {
|
||||
if i >= len(f) && i >= len(rhs) {
|
||||
// Maps are the same length and all items are equal.
|
||||
return 0
|
||||
}
|
||||
if i >= len(f) {
|
||||
// F is shorter.
|
||||
return -1
|
||||
}
|
||||
if i >= len(rhs) {
|
||||
// RHS is shorter.
|
||||
return 1
|
||||
}
|
||||
if c := strings.Compare(f[i].Name, rhs[i].Name); c != 0 {
|
||||
return c
|
||||
}
|
||||
if c := f[i].Value.Compare(rhs[i].Value); c != 0 {
|
||||
return c
|
||||
}
|
||||
// The items are equal; continue.
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
// List is a list of items.
|
||||
type List struct {
|
||||
Items []Value
|
||||
}
|
||||
|
||||
// Equals compares two lists lexically.
|
||||
func (l *List) Equals(rhs *List) bool {
|
||||
if len(l.Items) != len(rhs.Items) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i, lv := range l.Items {
|
||||
if !lv.Equals(rhs.Items[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Less compares two lists lexically.
|
||||
func (l *List) Less(rhs *List) bool {
|
||||
return l.Compare(rhs) == -1
|
||||
}
|
||||
|
||||
// Compare compares two lists lexically. The result will be 0 if l==rhs, -1
|
||||
// if l < rhs, and +1 if l > rhs.
|
||||
func (l *List) Compare(rhs *List) int {
|
||||
i := 0
|
||||
for {
|
||||
if i >= len(l.Items) && i >= len(rhs.Items) {
|
||||
// Lists are the same length and all items are equal.
|
||||
return 0
|
||||
}
|
||||
if i >= len(l.Items) {
|
||||
// LHS is shorter.
|
||||
return -1
|
||||
}
|
||||
if i >= len(rhs.Items) {
|
||||
// RHS is shorter.
|
||||
return 1
|
||||
}
|
||||
if c := l.Items[i].Compare(rhs.Items[i]); c != 0 {
|
||||
return c
|
||||
}
|
||||
// The items are equal; continue.
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
// Map is a map of key-value pairs. It represents both structs and maps. We use
|
||||
// a list and a go-language map to preserve order.
|
||||
//
|
||||
// Set and Get helpers are provided.
|
||||
type Map struct {
|
||||
Items []Field
|
||||
|
||||
// may be nil; lazily constructed.
|
||||
// TODO: Direct modifications to Items above will cause serious problems.
|
||||
index map[string]int
|
||||
// may be empty; lazily constructed.
|
||||
// TODO: Direct modifications to Items above will cause serious problems.
|
||||
order []int
|
||||
}
|
||||
|
||||
func (m *Map) computeOrder() []int {
|
||||
if len(m.order) != len(m.Items) {
|
||||
m.order = make([]int, len(m.Items))
|
||||
for i := range m.order {
|
||||
m.order[i] = i
|
||||
}
|
||||
sort.SliceStable(m.order, func(i, j int) bool {
|
||||
return m.Items[m.order[i]].Name < m.Items[m.order[j]].Name
|
||||
})
|
||||
}
|
||||
return m.order
|
||||
}
|
||||
|
||||
// Equals compares two maps lexically.
|
||||
func (m *Map) Equals(rhs *Map) bool {
|
||||
if len(m.Items) != len(rhs.Items) {
|
||||
return false
|
||||
}
|
||||
for _, lfield := range m.Items {
|
||||
rfield, ok := rhs.Get(lfield.Name)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if !lfield.Value.Equals(rfield.Value) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Less compares two maps lexically.
|
||||
func (m *Map) Less(rhs *Map) bool {
|
||||
return m.Compare(rhs) == -1
|
||||
}
|
||||
|
||||
// Compare compares two maps lexically.
|
||||
func (m *Map) Compare(rhs *Map) int {
|
||||
var noAllocL, noAllocR [2]int
|
||||
var morder, rorder []int
|
||||
|
||||
// For very short maps (<2 elements) this permits us to avoid
|
||||
// allocating the order array. We could make this accomodate larger
|
||||
// maps, but 2 items should be enough to cover most path element
|
||||
// comparisons, and at some point there will be diminishing returns.
|
||||
// This has a large effect on the path element deserialization test,
|
||||
// because everything is sorted / compared, but only once.
|
||||
switch len(m.Items) {
|
||||
case 0:
|
||||
morder = noAllocL[0:0]
|
||||
case 1:
|
||||
morder = noAllocL[0:1]
|
||||
case 2:
|
||||
morder = noAllocL[0:2]
|
||||
if m.Items[0].Name > m.Items[1].Name {
|
||||
morder[0] = 1
|
||||
} else {
|
||||
morder[1] = 1
|
||||
}
|
||||
default:
|
||||
morder = m.computeOrder()
|
||||
}
|
||||
|
||||
switch len(rhs.Items) {
|
||||
case 0:
|
||||
rorder = noAllocR[0:0]
|
||||
case 1:
|
||||
rorder = noAllocR[0:1]
|
||||
case 2:
|
||||
rorder = noAllocR[0:2]
|
||||
if rhs.Items[0].Name > rhs.Items[1].Name {
|
||||
rorder[0] = 1
|
||||
} else {
|
||||
rorder[1] = 1
|
||||
}
|
||||
default:
|
||||
rorder = rhs.computeOrder()
|
||||
}
|
||||
|
||||
i := 0
|
||||
for {
|
||||
if i >= len(morder) && i >= len(rorder) {
|
||||
// Maps are the same length and all items are equal.
|
||||
return 0
|
||||
}
|
||||
if i >= len(morder) {
|
||||
// LHS is shorter.
|
||||
return -1
|
||||
}
|
||||
if i >= len(rorder) {
|
||||
// RHS is shorter.
|
||||
return 1
|
||||
}
|
||||
fa, fb := &m.Items[morder[i]], &rhs.Items[rorder[i]]
|
||||
if c := strings.Compare(fa.Name, fb.Name); c != 0 {
|
||||
return c
|
||||
}
|
||||
if c := fa.Value.Compare(fb.Value); c != 0 {
|
||||
return c
|
||||
}
|
||||
// The items are equal; continue.
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the (Field, true) or (nil, false) if it is not present
|
||||
func (m *Map) Get(key string) (*Field, bool) {
|
||||
if m.index == nil {
|
||||
m.index = map[string]int{}
|
||||
for i := range m.Items {
|
||||
m.index[m.Items[i].Name] = i
|
||||
}
|
||||
}
|
||||
f, ok := m.index[key]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return &m.Items[f], true
|
||||
}
|
||||
|
||||
// Set inserts or updates the given item.
|
||||
func (m *Map) Set(key string, value Value) {
|
||||
if f, ok := m.Get(key); ok {
|
||||
f.Value = value
|
||||
return
|
||||
}
|
||||
m.Items = append(m.Items, Field{Name: key, Value: value})
|
||||
i := len(m.Items) - 1
|
||||
m.index[key] = i
|
||||
m.order = nil
|
||||
}
|
||||
|
||||
// Delete removes the key from the set.
|
||||
func (m *Map) Delete(key string) {
|
||||
items := []Field{}
|
||||
for i := range m.Items {
|
||||
if m.Items[i].Name != key {
|
||||
items = append(items, m.Items[i])
|
||||
}
|
||||
}
|
||||
m.Items = items
|
||||
m.index = nil // Since the list has changed
|
||||
m.order = nil
|
||||
}
|
||||
|
||||
// StringValue returns s as a scalar string Value.
|
||||
func StringValue(s string) Value {
|
||||
s2 := String(s)
|
||||
return Value{StringValue: &s2}
|
||||
}
|
||||
|
||||
// IntValue returns i as a scalar numeric (integer) Value.
|
||||
func IntValue(i int) Value {
|
||||
i2 := Int(i)
|
||||
return Value{IntValue: &i2}
|
||||
}
|
||||
|
||||
// FloatValue returns f as a scalar numeric (float) Value.
|
||||
func FloatValue(f float64) Value {
|
||||
f2 := Float(f)
|
||||
return Value{FloatValue: &f2}
|
||||
}
|
||||
|
||||
// BooleanValue returns b as a scalar boolean Value.
|
||||
func BooleanValue(b bool) Value {
|
||||
b2 := Boolean(b)
|
||||
return Value{BooleanValue: &b2}
|
||||
}
|
||||
|
||||
// String returns a human-readable representation of the value.
|
||||
func (v Value) String() string {
|
||||
switch {
|
||||
case v.FloatValue != nil:
|
||||
return fmt.Sprintf("%v", *v.FloatValue)
|
||||
case v.IntValue != nil:
|
||||
return fmt.Sprintf("%v", *v.IntValue)
|
||||
case v.StringValue != nil:
|
||||
return fmt.Sprintf("%q", *v.StringValue)
|
||||
case v.BooleanValue != nil:
|
||||
return fmt.Sprintf("%v", *v.BooleanValue)
|
||||
case v.ListValue != nil:
|
||||
strs := []string{}
|
||||
for _, item := range v.ListValue.Items {
|
||||
strs = append(strs, item.String())
|
||||
}
|
||||
return "[" + strings.Join(strs, ",") + "]"
|
||||
case v.MapValue != nil:
|
||||
strs := []string{}
|
||||
for _, i := range v.MapValue.Items {
|
||||
strs = append(strs, fmt.Sprintf("%v=%v", i.Name, i.Value))
|
||||
}
|
||||
return "{" + strings.Join(strs, ";") + "}"
|
||||
default:
|
||||
fallthrough
|
||||
case v.Null == true:
|
||||
return "null"
|
||||
}
|
||||
}
|
||||
15
vendor/sigs.k8s.io/yaml/.travis.yml
generated
vendored
15
vendor/sigs.k8s.io/yaml/.travis.yml
generated
vendored
|
|
@ -1,14 +1,13 @@
|
|||
language: go
|
||||
dist: xenial
|
||||
go:
|
||||
- 1.9.x
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
- 1.12.x
|
||||
- 1.13.x
|
||||
script:
|
||||
- go get -t -v ./...
|
||||
- diff -u <(echo -n) <(gofmt -d .)
|
||||
- diff -u <(echo -n) <(gofmt -d *.go)
|
||||
- diff -u <(echo -n) <(golint $(go list -e ./...) | grep -v YAMLToJSON)
|
||||
- go tool vet .
|
||||
- go test -v -race ./...
|
||||
- GO111MODULE=on go vet .
|
||||
- GO111MODULE=on go test -v -race ./...
|
||||
- git diff --exit-code
|
||||
install:
|
||||
- go get golang.org/x/lint/golint
|
||||
- GO111MODULE=off go get golang.org/x/lint/golint
|
||||
|
|
|
|||
2
vendor/sigs.k8s.io/yaml/OWNERS
generated
vendored
2
vendor/sigs.k8s.io/yaml/OWNERS
generated
vendored
|
|
@ -1,3 +1,5 @@
|
|||
# See the OWNERS docs at https://go.k8s.io/owners
|
||||
|
||||
approvers:
|
||||
- dims
|
||||
- lavalamp
|
||||
|
|
|
|||
14
vendor/sigs.k8s.io/yaml/README.md
generated
vendored
14
vendor/sigs.k8s.io/yaml/README.md
generated
vendored
|
|
@ -1,12 +1,14 @@
|
|||
# YAML marshaling and unmarshaling support for Go
|
||||
|
||||
[](https://travis-ci.org/ghodss/yaml)
|
||||
[](https://travis-ci.org/kubernetes-sigs/yaml)
|
||||
|
||||
kubernetes-sigs/yaml is a permanent fork of [ghodss/yaml](https://github.com/ghodss/yaml).
|
||||
|
||||
## Introduction
|
||||
|
||||
A wrapper around [go-yaml](https://github.com/go-yaml/yaml) designed to enable a better way of handling YAML when marshaling to and from structs.
|
||||
|
||||
In short, this library first converts YAML to JSON using go-yaml and then uses `json.Marshal` and `json.Unmarshal` to convert to or from the struct. This means that it effectively reuses the JSON struct tags as well as the custom JSON methods `MarshalJSON` and `UnmarshalJSON` unlike go-yaml. For a detailed overview of the rationale behind this method, [see this blog post](http://ghodss.com/2014/the-right-way-to-handle-yaml-in-golang/).
|
||||
In short, this library first converts YAML to JSON using go-yaml and then uses `json.Marshal` and `json.Unmarshal` to convert to or from the struct. This means that it effectively reuses the JSON struct tags as well as the custom JSON methods `MarshalJSON` and `UnmarshalJSON` unlike go-yaml. For a detailed overview of the rationale behind this method, [see this blog post](http://web.archive.org/web/20190603050330/http://ghodss.com/2014/the-right-way-to-handle-yaml-in-golang/).
|
||||
|
||||
## Compatibility
|
||||
|
||||
|
|
@ -32,13 +34,13 @@ GOOD:
|
|||
To install, run:
|
||||
|
||||
```
|
||||
$ go get github.com/ghodss/yaml
|
||||
$ go get sigs.k8s.io/yaml
|
||||
```
|
||||
|
||||
And import using:
|
||||
|
||||
```
|
||||
import "github.com/ghodss/yaml"
|
||||
import "sigs.k8s.io/yaml"
|
||||
```
|
||||
|
||||
Usage is very similar to the JSON library:
|
||||
|
|
@ -49,7 +51,7 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
|
|
@ -93,7 +95,7 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
|
|||
8
vendor/sigs.k8s.io/yaml/go.mod
generated
vendored
Normal file
8
vendor/sigs.k8s.io/yaml/go.mod
generated
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
module sigs.k8s.io/yaml
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
gopkg.in/yaml.v2 v2.2.8
|
||||
)
|
||||
9
vendor/sigs.k8s.io/yaml/go.sum
generated
vendored
Normal file
9
vendor/sigs.k8s.io/yaml/go.sum
generated
vendored
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
|
||||
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
61
vendor/sigs.k8s.io/yaml/yaml.go
generated
vendored
61
vendor/sigs.k8s.io/yaml/yaml.go
generated
vendored
|
|
@ -317,3 +317,64 @@ func convertToJSONableObject(yamlObj interface{}, jsonTarget *reflect.Value) (in
|
|||
return yamlObj, nil
|
||||
}
|
||||
}
|
||||
|
||||
// JSONObjectToYAMLObject converts an in-memory JSON object into a YAML in-memory MapSlice,
|
||||
// without going through a byte representation. A nil or empty map[string]interface{} input is
|
||||
// converted to an empty map, i.e. yaml.MapSlice(nil).
|
||||
//
|
||||
// interface{} slices stay interface{} slices. map[string]interface{} becomes yaml.MapSlice.
|
||||
//
|
||||
// int64 and float64 are down casted following the logic of github.com/go-yaml/yaml:
|
||||
// - float64s are down-casted as far as possible without data-loss to int, int64, uint64.
|
||||
// - int64s are down-casted to int if possible without data-loss.
|
||||
//
|
||||
// Big int/int64/uint64 do not lose precision as in the json-yaml roundtripping case.
|
||||
//
|
||||
// string, bool and any other types are unchanged.
|
||||
func JSONObjectToYAMLObject(j map[string]interface{}) yaml.MapSlice {
|
||||
if len(j) == 0 {
|
||||
return nil
|
||||
}
|
||||
ret := make(yaml.MapSlice, 0, len(j))
|
||||
for k, v := range j {
|
||||
ret = append(ret, yaml.MapItem{Key: k, Value: jsonToYAMLValue(v)})
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func jsonToYAMLValue(j interface{}) interface{} {
|
||||
switch j := j.(type) {
|
||||
case map[string]interface{}:
|
||||
if j == nil {
|
||||
return interface{}(nil)
|
||||
}
|
||||
return JSONObjectToYAMLObject(j)
|
||||
case []interface{}:
|
||||
if j == nil {
|
||||
return interface{}(nil)
|
||||
}
|
||||
ret := make([]interface{}, len(j))
|
||||
for i := range j {
|
||||
ret[i] = jsonToYAMLValue(j[i])
|
||||
}
|
||||
return ret
|
||||
case float64:
|
||||
// replicate the logic in https://github.com/go-yaml/yaml/blob/51d6538a90f86fe93ac480b35f37b2be17fef232/resolve.go#L151
|
||||
if i64 := int64(j); j == float64(i64) {
|
||||
if i := int(i64); i64 == int64(i) {
|
||||
return i
|
||||
}
|
||||
return i64
|
||||
}
|
||||
if ui64 := uint64(j); j == float64(ui64) {
|
||||
return ui64
|
||||
}
|
||||
return j
|
||||
case int64:
|
||||
if i := int(j); j == int64(i) {
|
||||
return i
|
||||
}
|
||||
return j
|
||||
}
|
||||
return j
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue