2021-10-27 10:00:15 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2022-04-11 16:25:14 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-10-27 10:00:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ContainerContext represents NeoFS API v2-compatible
|
|
|
|
// context of the container session.
|
|
|
|
//
|
|
|
|
// It is a wrapper over session.ContainerSessionContext
|
|
|
|
// which allows to abstract from details of the message
|
|
|
|
// structure.
|
|
|
|
type ContainerContext session.ContainerSessionContext
|
|
|
|
|
|
|
|
// NewContainerContext creates and returns blank ContainerSessionContext.
|
|
|
|
//
|
|
|
|
// Defaults:
|
|
|
|
// - not bound to any operation;
|
|
|
|
// - applied to all containers.
|
|
|
|
func NewContainerContext() *ContainerContext {
|
|
|
|
v2 := new(session.ContainerSessionContext)
|
|
|
|
v2.SetWildcard(true)
|
|
|
|
|
2022-02-01 10:21:25 +00:00
|
|
|
return NewContainerContextFromV2(v2)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 10:21:25 +00:00
|
|
|
// NewContainerContextFromV2 wraps session.ContainerSessionContext
|
2021-10-27 10:00:15 +00:00
|
|
|
// into ContainerContext.
|
2022-02-01 10:21:25 +00:00
|
|
|
func NewContainerContextFromV2(v *session.ContainerSessionContext) *ContainerContext {
|
2021-10-27 10:00:15 +00:00
|
|
|
return (*ContainerContext)(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts ContainerContext to session.ContainerSessionContext
|
|
|
|
// message structure.
|
|
|
|
func (x *ContainerContext) ToV2() *session.ContainerSessionContext {
|
|
|
|
return (*session.ContainerSessionContext)(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyTo specifies which container the ContainerContext applies to.
|
|
|
|
//
|
|
|
|
// If id is nil, ContainerContext is applied to all containers.
|
|
|
|
func (x *ContainerContext) ApplyTo(id *cid.ID) {
|
|
|
|
v2 := (*session.ContainerSessionContext)(x)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var cidV2 *refs.ContainerID
|
|
|
|
|
|
|
|
if id != nil {
|
|
|
|
var c refs.ContainerID
|
|
|
|
id.WriteToV2(&c)
|
|
|
|
|
|
|
|
cidV2 = &c
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:00:15 +00:00
|
|
|
v2.SetWildcard(id == nil)
|
2022-04-11 16:25:14 +00:00
|
|
|
v2.SetContainerID(cidV2)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 10:21:25 +00:00
|
|
|
// ApplyToAllContainers is a helper function that conveniently
|
2021-10-27 10:00:15 +00:00
|
|
|
// applies ContainerContext to all containers.
|
|
|
|
func ApplyToAllContainers(c *ContainerContext) {
|
|
|
|
c.ApplyTo(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Container returns identifier of the container
|
|
|
|
// to which the ContainerContext applies.
|
|
|
|
//
|
|
|
|
// Returns nil if ContainerContext is applied to
|
|
|
|
// all containers.
|
|
|
|
func (x *ContainerContext) Container() *cid.ID {
|
|
|
|
v2 := (*session.ContainerSessionContext)(x)
|
|
|
|
|
|
|
|
if v2.Wildcard() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
cidV2 := v2.ContainerID()
|
|
|
|
if cidV2 == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var cID cid.ID
|
|
|
|
_ = cID.ReadFromV2(*cidV2)
|
|
|
|
|
|
|
|
return &cID
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ContainerContext) forVerb(v session.ContainerSessionVerb) {
|
|
|
|
(*session.ContainerSessionContext)(x).
|
|
|
|
SetVerb(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ContainerContext) isForVerb(v session.ContainerSessionVerb) bool {
|
|
|
|
return (*session.ContainerSessionContext)(x).
|
|
|
|
Verb() == v
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForPut binds the ContainerContext to
|
|
|
|
// PUT operation.
|
|
|
|
func (x *ContainerContext) ForPut() {
|
|
|
|
x.forVerb(session.ContainerVerbPut)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsForPut checks if ContainerContext is bound to
|
|
|
|
// PUT operation.
|
|
|
|
func (x *ContainerContext) IsForPut() bool {
|
|
|
|
return x.isForVerb(session.ContainerVerbPut)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForDelete binds the ContainerContext to
|
|
|
|
// DELETE operation.
|
|
|
|
func (x *ContainerContext) ForDelete() {
|
|
|
|
x.forVerb(session.ContainerVerbDelete)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsForDelete checks if ContainerContext is bound to
|
|
|
|
// DELETE operation.
|
|
|
|
func (x *ContainerContext) IsForDelete() bool {
|
|
|
|
return x.isForVerb(session.ContainerVerbDelete)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForSetEACL binds the ContainerContext to
|
|
|
|
// SETEACL operation.
|
|
|
|
func (x *ContainerContext) ForSetEACL() {
|
|
|
|
x.forVerb(session.ContainerVerbSetEACL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsForSetEACL checks if ContainerContext is bound to
|
|
|
|
// SETEACL operation.
|
|
|
|
func (x *ContainerContext) IsForSetEACL() bool {
|
|
|
|
return x.isForVerb(session.ContainerVerbSetEACL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals ContainerContext into a protobuf binary form.
|
2021-11-08 12:25:13 +00:00
|
|
|
func (x *ContainerContext) Marshal() ([]byte, error) {
|
|
|
|
return x.ToV2().StableMarshal(nil)
|
2021-10-27 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of ContainerContext.
|
|
|
|
func (x *ContainerContext) Unmarshal(data []byte) error {
|
|
|
|
return x.ToV2().Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes ContainerContext to protobuf JSON format.
|
|
|
|
func (x *ContainerContext) MarshalJSON() ([]byte, error) {
|
|
|
|
return x.ToV2().MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes ContainerContext from protobuf JSON format.
|
|
|
|
func (x *ContainerContext) UnmarshalJSON(data []byte) error {
|
|
|
|
return x.ToV2().UnmarshalJSON(data)
|
|
|
|
}
|