2024-05-28 09:05:02 +00:00
|
|
|
package ape
|
2024-05-06 13:26:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
apeV2 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/ape"
|
2024-05-06 13:26:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidChainRepresentation = errors.New("invalid chain representation")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChainID is Chain's identifier.
|
|
|
|
type ChainID []byte
|
|
|
|
|
|
|
|
// Chain is an SDK representation for v2's Chain.
|
|
|
|
//
|
|
|
|
// Note that Chain (as well as v2's Chain) and all related entities
|
|
|
|
// are NOT operated by Access-Policy-Engine (APE). The client is responsible
|
|
|
|
// to convert these types to policy-engine entities.
|
|
|
|
type Chain struct {
|
|
|
|
// Raw is the encoded chain kind.
|
|
|
|
// It assumes that Raw's bytes are the result of encoding provided by
|
|
|
|
// policy-engine package.
|
|
|
|
Raw []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts Chain to v2.
|
2024-05-28 09:05:02 +00:00
|
|
|
func (c *Chain) ToV2() *apeV2.Chain {
|
|
|
|
v2ct := new(apeV2.Chain)
|
2024-05-06 13:26:51 +00:00
|
|
|
|
|
|
|
if c.Raw != nil {
|
2024-05-28 09:05:02 +00:00
|
|
|
v2Raw := new(apeV2.ChainRaw)
|
2024-05-06 13:26:51 +00:00
|
|
|
v2Raw.SetRaw(c.Raw)
|
|
|
|
v2ct.SetKind(v2Raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v2ct
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFromV2 fills Chain from v2.
|
2024-05-28 09:05:02 +00:00
|
|
|
func (c *Chain) ReadFromV2(v2ct *apeV2.Chain) error {
|
2024-05-06 13:26:51 +00:00
|
|
|
switch v := v2ct.GetKind().(type) {
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported chain kind: %T", v)
|
2024-05-28 09:05:02 +00:00
|
|
|
case *apeV2.ChainRaw:
|
2024-05-06 13:26:51 +00:00
|
|
|
raw := v.GetRaw()
|
|
|
|
c.Raw = raw
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|