Files
de-golang-sdk/compatibility.go
T

79 lines
2.0 KiB
Go
Raw Permalink Normal View History

2026-09-28 11:14:36 +03:00
// Code generated by internal codegen utility; DO NOT EDIT.
package desdk
import (
"context"
"fmt"
"strconv"
"strings"
)
// Supported DE version is selected during SDK generation.
const (
supportedDEMajor = 1
supportedDEMinor = 8
)
// CheckCompatibility verifies access to DE with the configured token and
// checks that this SDK version supports the connected DE version.
//
// An explicit check is performed even when SkipCompatibilityCheck is enabled.
func (c *Client) CheckCompatibility(ctx context.Context) error {
if ctx == nil {
return fmt.Errorf("nil context")
}
if c == nil {
return fmt.Errorf("nil client")
}
if c.cfg == nil {
return fmt.Errorf("nil client configuration")
}
if c.DeDeployment == nil {
return fmt.Errorf("de_deployment service is nil; call NewClient or NewClientWithConfig")
}
authorization := strings.TrimSpace(c.cfg.DefaultHeaders()["Authorization"])
if !strings.HasPrefix(authorization, "Bearer ") || strings.TrimSpace(strings.TrimPrefix(authorization, "Bearer ")) == "" {
return fmt.Errorf("token is not set")
}
deployment, err := c.DeDeployment.Get(ctx)
if err != nil {
return fmt.Errorf("get DE version: %w", err)
}
deMajor, deMinor, ok := versionMajorMinor(deployment.ReleaseVersion)
if !ok || deMajor != supportedDEMajor || deMinor != supportedDEMinor {
return fmt.Errorf("incompatible DE version %q: expected %d.%d.x", deployment.ReleaseVersion, supportedDEMajor, supportedDEMinor)
}
return nil
}
func versionMajorMinor(version string) (int, int, bool) {
version = strings.TrimPrefix(version, "v")
if suffix := strings.IndexAny(version, "-+"); suffix >= 0 {
version = version[:suffix]
}
parts := strings.Split(version, ".")
if len(parts) != 3 {
return 0, 0, false
}
major, err := strconv.Atoi(parts[0])
if err != nil || major < 0 {
return 0, 0, false
}
minor, err := strconv.Atoi(parts[1])
if err != nil || minor < 0 {
return 0, 0, false
}
patch, err := strconv.Atoi(parts[2])
if err != nil || patch < 0 {
return 0, 0, false
}
return major, minor, true
}