1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00

[Bugfix] Enable LazyLoader for CRD & CRD Schemas (#1582)

This commit is contained in:
Adam Janikowski 2024-01-17 22:41:16 +01:00 committed by GitHub
parent ffd9d5256c
commit 82a87225c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 168 additions and 109 deletions

View file

@ -9,6 +9,7 @@
- (Documentation) Use relative links for generated docs
- (Improvement) Change default logging level to info. Add --log.sampling (default true). Adjust log levels.
- (Maintenance) Bump Go to 1.21.6
- (Bugfix) Enable LazyLoader for CRD & CRD Schemas
## [1.2.36](https://github.com/arangodb/kube-arangodb/tree/1.2.36) (2024-01-08)
- (Documentation) Improvements and fixes for rendered documentation (GH pages)

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
AppsJobVersion = driver.Version("1.0.1")
)
func init() {
mustLoadCRD(appsJobs, appsJobsSchemaRaw, &appsJobsCRD, &appsJobsCRDSchemas)
}
// Deprecated: use AppsJobWithOptions instead
func AppsJob() *apiextensions.CustomResourceDefinition {
return AppsJobWithOptions()
@ -57,8 +55,8 @@ func AppsJobDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
}
}
var appsJobsCRD apiextensions.CustomResourceDefinition
var appsJobsCRDSchemas crdSchemas
var appsJobsCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](appsJobs)
var appsJobsCRDSchemas = util.NewYamlLoader[crdSchemas](appsJobsSchemaRaw)
//go:embed apps-job.yaml
var appsJobs []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
BackupsBackupVersion = driver.Version("1.0.1")
)
func init() {
mustLoadCRD(backupsBackup, backupsBackupSchemaRaw, &backupsBackupCRD, &backupsBackupCRDSchemas)
}
// Deprecated: use BackupsBackupWithOptions instead
func BackupsBackup() *apiextensions.CustomResourceDefinition {
return BackupsBackupWithOptions()
@ -57,8 +55,8 @@ func BackupsBackupDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
}
}
var backupsBackupCRD apiextensions.CustomResourceDefinition
var backupsBackupCRDSchemas crdSchemas
var backupsBackupCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](backupsBackup)
var backupsBackupCRDSchemas = util.NewYamlLoader[crdSchemas](backupsBackupSchemaRaw)
//go:embed backups-backup.yaml
var backupsBackup []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
BackupsBackupPolicyPolicyVersion = driver.Version("1.0.1")
)
func init() {
mustLoadCRD(backupsBackupPolicy, backupsBackupPolicySchemaRaw, &backupsBackupPolicyCRD, &backupsBackupPolicyCRDSchemas)
}
// Deprecated: use BackupsBackupPolicyPolicyWithOptions instead
func BackupsBackupPolicyPolicy() *apiextensions.CustomResourceDefinition {
return BackupsBackupPolicyPolicyWithOptions()
@ -57,8 +55,8 @@ func BackupsBackupPolicyDefinitionWithOptions(opts ...func(*CRDOptions)) Definit
}
}
var backupsBackupPolicyCRD apiextensions.CustomResourceDefinition
var backupsBackupPolicyCRDSchemas crdSchemas
var backupsBackupPolicyCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](backupsBackupPolicy)
var backupsBackupPolicyCRDSchemas = util.NewYamlLoader[crdSchemas](backupsBackupPolicySchemaRaw)
//go:embed backups-backuppolicy.yaml
var backupsBackupPolicy []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -24,9 +24,10 @@ import (
"fmt"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
type Definition struct {
@ -65,16 +66,6 @@ func AllDefinitions() []Definition {
}
}
func mustLoadCRD(crdRaw, crdSchemasRaw []byte, crd *apiextensions.CustomResourceDefinition, schemas *crdSchemas) {
if err := yaml.Unmarshal(crdRaw, crd); err != nil {
panic(err)
}
if err := yaml.Unmarshal(crdSchemasRaw, schemas); err != nil {
panic(err)
}
}
type crdSchemas map[string]apiextensions.CustomResourceValidation
type CRDOptions struct {
@ -97,13 +88,19 @@ func WithSchema() func(*CRDOptions) {
}
}
func getCRD(crd apiextensions.CustomResourceDefinition, schemas crdSchemas, opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition {
func getCRD(crdLoader util.Loader[apiextensions.CustomResourceDefinition], schemasLoader util.Loader[crdSchemas], opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition {
o := &CRDOptions{}
for _, fn := range opts {
fn(o)
}
crd := crdLoader.MustGet()
if o.WithSchema {
crdWithSchema := crd.DeepCopy()
schemas := schemasLoader.MustGet()
for i, v := range crdWithSchema.Spec.Versions {
schema, ok := schemas[v.Name]
if !ok {

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
DatabaseClusterSynchronizationVersion = driver.Version("1.0.1")
)
func init() {
mustLoadCRD(databaseClusterSynchronization, databaseClusterSynchronizationSchemaRaw, &databaseClusterSynchronizationCRD, &databaseClusterSynchronizationCRDSchemas)
}
// Deprecated: use DatabaseClusterSynchronizationWithOptions instead
func DatabaseClusterSynchronization() *apiextensions.CustomResourceDefinition {
return DatabaseClusterSynchronizationWithOptions()
@ -57,8 +55,8 @@ func DatabaseClusterSynchronizationDefinitionWithOptions(opts ...func(*CRDOption
}
}
var databaseClusterSynchronizationCRD apiextensions.CustomResourceDefinition
var databaseClusterSynchronizationCRDSchemas crdSchemas
var databaseClusterSynchronizationCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](databaseClusterSynchronization)
var databaseClusterSynchronizationCRDSchemas = util.NewYamlLoader[crdSchemas](databaseClusterSynchronizationSchemaRaw)
//go:embed database-clustersynchronization.yaml
var databaseClusterSynchronization []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
DatabaseDeploymentVersion = driver.Version("1.0.1")
)
func init() {
mustLoadCRD(databaseDeployment, databaseDeploymentSchemaRaw, &databaseDeploymentCRD, &databaseDeploymentCRDSchemas)
}
// Deprecated: use DatabaseDeploymentWithOptions instead
func DatabaseDeployment() *apiextensions.CustomResourceDefinition {
return DatabaseDeploymentWithOptions()
@ -57,8 +55,8 @@ func DatabaseDeploymentDefinitionWithOptions(opts ...func(*CRDOptions)) Definiti
}
}
var databaseDeploymentCRD apiextensions.CustomResourceDefinition
var databaseDeploymentCRDSchemas crdSchemas
var databaseDeploymentCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](databaseDeployment)
var databaseDeploymentCRDSchemas = util.NewYamlLoader[crdSchemas](databaseDeploymentSchemaRaw)
//go:embed database-deployment.yaml
var databaseDeployment []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
DatabaseMemberVersion = driver.Version("1.0.1")
)
func init() {
mustLoadCRD(databaseMember, databaseMemberSchemaRaw, &databaseMemberCRD, &databaseMemberCRDSchemas)
}
// Deprecated: use DatabaseMemberWithOptions instead
func DatabaseMember() *apiextensions.CustomResourceDefinition {
return DatabaseMemberWithOptions()
@ -57,8 +55,8 @@ func DatabaseMemberDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
}
}
var databaseMemberCRD apiextensions.CustomResourceDefinition
var databaseMemberCRDSchemas crdSchemas
var databaseMemberCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](databaseMember)
var databaseMemberCRDSchemas = util.NewYamlLoader[crdSchemas](databaseMemberSchemaRaw)
//go:embed database-member.yaml
var databaseMember []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
DatabaseTaskVersion = driver.Version("1.0.1")
)
func init() {
mustLoadCRD(databaseTask, databaseTaskSchemaRaw, &databaseTaskCRD, &databaseTaskCRDSchemas)
}
// Deprecated: use DatabaseTaskWithOptions instead
func DatabaseTask() *apiextensions.CustomResourceDefinition {
return DatabaseTaskWithOptions()
@ -57,8 +55,8 @@ func DatabaseTaskDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
}
}
var databaseTaskCRD apiextensions.CustomResourceDefinition
var databaseTaskCRDSchemas crdSchemas
var databaseTaskCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](databaseTask)
var databaseTaskCRDSchemas = util.NewYamlLoader[crdSchemas](databaseTaskSchemaRaw)
//go:embed database-task.yaml
var databaseTask []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
MLExtensionVersion = driver.Version("1.0.0")
)
func init() {
mustLoadCRD(mlExtension, mlExtensionSchemaRaw, &mlExtensionCRD, &mlExtensionCRDSchemas)
}
func MLExtensionWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition {
return getCRD(mlExtensionCRD, mlExtensionCRDSchemas, opts...)
}
@ -47,8 +45,8 @@ func MLExtensionDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
}
}
var mlExtensionCRD apiextensions.CustomResourceDefinition
var mlExtensionCRDSchemas crdSchemas
var mlExtensionCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](mlExtension)
var mlExtensionCRDSchemas = util.NewYamlLoader[crdSchemas](mlExtensionSchemaRaw)
//go:embed ml-extension.yaml
var mlExtension []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -24,22 +24,16 @@ import (
_ "embed"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
MLBatchJobVersion = driver.Version("1.0.0")
)
func init() {
if err := yaml.Unmarshal(mlBatchJob, &mlBatchJobCRD); err != nil {
panic(err)
}
mustLoadCRD(mlBatchJob, mlBatchJobSchemaRow, &mlBatchJobCRD, &mlBatchJobCRDSchemas)
}
func MLBatchJobWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition {
return getCRD(mlBatchJobCRD, mlBatchJobCRDSchemas, opts...)
}
@ -51,11 +45,11 @@ func MLBatchJobDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
}
}
var mlBatchJobCRD apiextensions.CustomResourceDefinition
var mlBatchJobCRDSchemas crdSchemas
var mlBatchJobCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](mlBatchJob)
var mlBatchJobCRDSchemas = util.NewYamlLoader[crdSchemas](mlBatchJobSchemaRaw)
//go:embed ml-job-batch.yaml
var mlBatchJob []byte
//go:embed ml-job-batch.schema.generated.yaml
var mlBatchJobSchemaRow []byte
var mlBatchJobSchemaRaw []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
MLCronJobVersion = driver.Version("1.0.0")
)
func init() {
mustLoadCRD(mlCronJob, mlCronJobSchemaRaw, &mlCronJobCRD, &mlCronJobCRDSchemas)
}
func MLCronJobWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition {
return getCRD(mlCronJobCRD, mlCronJobCRDSchemas, opts...)
}
@ -47,8 +45,8 @@ func MLCronJobDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
}
}
var mlCronJobCRD apiextensions.CustomResourceDefinition
var mlCronJobCRDSchemas crdSchemas
var mlCronJobCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](mlCronJob)
var mlCronJobCRDSchemas = util.NewYamlLoader[crdSchemas](mlCronJobSchemaRaw)
//go:embed ml-job-cron.yaml
var mlCronJob []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
MLStorageVersion = driver.Version("1.0.0")
)
func init() {
mustLoadCRD(mlStorage, mlStorageSchemaRaw, &mlStorageCRD, &mlStorageCRDSchemas)
}
func MLStorageWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition {
return getCRD(mlStorageCRD, mlStorageCRDSchemas, opts...)
}
@ -47,8 +45,8 @@ func MLStorageDefinitionWithOptions(opts ...func(*CRDOptions)) Definition {
}
}
var mlStorageCRD apiextensions.CustomResourceDefinition
var mlStorageCRDSchemas crdSchemas
var mlStorageCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](mlStorage)
var mlStorageCRDSchemas = util.NewYamlLoader[crdSchemas](mlStorageSchemaRaw)
//go:embed ml-storage.yaml
var mlStorage []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
ReplicationDeploymentReplicationVersion = driver.Version("1.0.1")
)
func init() {
mustLoadCRD(replicationDeploymentReplication, replicationDeploymentReplicationSchemaRaw, &replicationDeploymentReplicationCRD, &replicationDeploymentReplicationCRDSchemas)
}
// Deprecated: use ReplicationDeploymentReplicationWithOptions instead
func ReplicationDeploymentReplication() *apiextensions.CustomResourceDefinition {
return ReplicationDeploymentReplicationWithOptions()
@ -57,8 +55,8 @@ func ReplicationDeploymentReplicationDefinitionWithOptions(opts ...func(*CRDOpti
}
}
var replicationDeploymentReplicationCRD apiextensions.CustomResourceDefinition
var replicationDeploymentReplicationCRDSchemas crdSchemas
var replicationDeploymentReplicationCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](replicationDeploymentReplication)
var replicationDeploymentReplicationCRDSchemas = util.NewYamlLoader[crdSchemas](replicationDeploymentReplicationSchemaRaw)
//go:embed replication-deploymentreplication.yaml
var replicationDeploymentReplication []byte

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -26,16 +26,14 @@ import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util"
)
const (
StorageLocalStorageVersion = driver.Version("1.0.1")
)
func init() {
mustLoadCRD(storageLocalStorage, storageLocalStorageSchemaRaw, &storageLocalStorageCRD, &storageLocalStorageCRDSchemas)
}
// Deprecated: use StorageLocalStorageWithOptions instead
func StorageLocalStorage() *apiextensions.CustomResourceDefinition {
return StorageLocalStorageWithOptions()
@ -57,8 +55,8 @@ func StorageLocalStorageDefinitionWithOptions(opts ...func(*CRDOptions)) Definit
}
}
var storageLocalStorageCRD apiextensions.CustomResourceDefinition
var storageLocalStorageCRDSchemas crdSchemas
var storageLocalStorageCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](storageLocalStorage)
var storageLocalStorageCRDSchemas = util.NewYamlLoader[crdSchemas](storageLocalStorageSchemaRaw)
//go:embed storage-localstorage.yaml
var storageLocalStorage []byte

91
pkg/util/loader.go Normal file
View file

@ -0,0 +1,91 @@
//
// DISCLAIMER
//
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package util
import (
"sync"
"k8s.io/apimachinery/pkg/util/yaml"
)
type Loader[T any] interface {
MustGet() T
Get() (T, error)
}
type LoaderGen[T any] func() (T, error)
type loader[T any] struct {
lock sync.Mutex
gen LoaderGen[T]
object T
generated bool
}
func (l *loader[T]) MustGet() T {
obj, err := l.Get()
if err != nil {
panic(err.Error())
}
return obj
}
func (l *loader[T]) Get() (T, error) {
l.lock.Lock()
defer l.lock.Unlock()
if l.generated {
return l.object, nil
}
obj, err := l.gen()
if err != nil {
return Default[T](), err
}
l.generated = true
l.object = obj
return l.object, nil
}
func NewLoader[T any](gen LoaderGen[T]) Loader[T] {
return &loader[T]{
gen: gen,
}
}
func NewYamlLoader[T any](data []byte) Loader[T] {
return NewLoader[T](func() (T, error) {
var obj T
if err := yaml.Unmarshal(data, &obj); err != nil {
return Default[T](), err
}
return obj, nil
})
}