git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/ape/chain.go (about)

     1  package ape
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	apeV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape"
     8  )
     9  
    10  var (
    11  	ErrInvalidChainRepresentation = errors.New("invalid chain representation")
    12  )
    13  
    14  // ChainID is Chain's identifier.
    15  type ChainID []byte
    16  
    17  // Chain is an SDK representation for v2's Chain.
    18  //
    19  // Note that Chain (as well as v2's Chain) and all related entities
    20  // are NOT operated by Access-Policy-Engine (APE). The client is responsible
    21  // to convert these types to policy-engine entities.
    22  type Chain struct {
    23  	// Raw is the encoded chain kind.
    24  	// It assumes that Raw's bytes are the result of encoding provided by
    25  	// policy-engine package.
    26  	Raw []byte
    27  }
    28  
    29  // ToV2 converts Chain to v2.
    30  func (c *Chain) ToV2() *apeV2.Chain {
    31  	v2ct := new(apeV2.Chain)
    32  
    33  	if c.Raw != nil {
    34  		v2Raw := new(apeV2.ChainRaw)
    35  		v2Raw.SetRaw(c.Raw)
    36  		v2ct.SetKind(v2Raw)
    37  	}
    38  
    39  	return v2ct
    40  }
    41  
    42  // ReadFromV2 fills Chain from v2.
    43  func (c *Chain) ReadFromV2(v2ct *apeV2.Chain) error {
    44  	switch v := v2ct.GetKind().(type) {
    45  	default:
    46  		return fmt.Errorf("unsupported chain kind: %T", v)
    47  	case *apeV2.ChainRaw:
    48  		raw := v.GetRaw()
    49  		c.Raw = raw
    50  	}
    51  	return nil
    52  }