jx/internal/resource/declaration.go

130 lines
3.0 KiB
Go
Raw Normal View History

2024-03-20 19:23:31 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
2024-03-22 17:39:06 +00:00
2024-03-20 19:23:31 +00:00
package resource
import (
2024-03-25 20:31:06 +00:00
"context"
"encoding/json"
2024-03-25 20:31:06 +00:00
"fmt"
"gopkg.in/yaml.v3"
"log/slog"
2024-03-20 19:23:31 +00:00
)
type DeclarationType struct {
Type TypeName `json:"type" yaml:"type"`
}
2024-03-20 19:23:31 +00:00
type Declaration struct {
Type TypeName `json:"type" yaml:"type"`
Attributes Resource `json:"attributes" yaml:"attributes"`
2024-03-20 19:23:31 +00:00
}
type ResourceLoader interface {
2024-03-25 20:31:06 +00:00
LoadDecl(string) error
2024-03-20 19:23:31 +00:00
}
type StateTransformer interface {
2024-03-25 20:31:06 +00:00
Apply() error
2024-03-20 19:23:31 +00:00
}
type YamlLoader func(string, any) error
func YamlLoadDecl(yamlFileResourceDeclaration string, resource any) error {
2024-03-25 20:31:06 +00:00
if err := yaml.Unmarshal([]byte(yamlFileResourceDeclaration), resource); err != nil {
return err
}
return nil
2024-03-20 19:23:31 +00:00
}
func NewDeclaration() *Declaration {
2024-03-25 20:31:06 +00:00
return &Declaration{}
2024-03-20 19:23:31 +00:00
}
func (d *Declaration) LoadDecl(yamlResourceDeclaration string) error {
2024-03-25 20:31:06 +00:00
return YamlLoadDecl(yamlResourceDeclaration, d)
2024-03-20 19:23:31 +00:00
}
func (d *Declaration) NewResource() error {
2024-03-25 20:31:06 +00:00
uri := fmt.Sprintf("%s://", d.Type)
newResource, err := ResourceTypes.New(uri)
d.Attributes = newResource
2024-03-25 20:31:06 +00:00
return err
2024-03-20 19:23:31 +00:00
}
func (d *Declaration) Resource() Resource {
return d.Attributes
2024-03-20 19:23:31 +00:00
}
func (d *Declaration) SetURI(uri string) error {
slog.Info("SetURI()", "uri", uri)
d.Attributes = NewResource(uri)
if d.Attributes == nil {
panic("unknown resource")
2024-03-25 20:31:06 +00:00
}
d.Type = TypeName(d.Attributes.Type())
d.Attributes.Read(context.Background()) // fix context
2024-03-25 20:31:06 +00:00
return nil
2024-03-20 19:23:31 +00:00
}
func (d *Declaration) UnmarshalYAML(value *yaml.Node) error {
t := &DeclarationType{}
if unmarshalResourceTypeErr := value.Decode(t); unmarshalResourceTypeErr != nil {
return unmarshalResourceTypeErr
}
d.Type = t.Type
newResource, resourceErr := ResourceTypes.New(fmt.Sprintf("%s://", d.Type))
if resourceErr != nil {
return resourceErr
}
d.Attributes = newResource
resourceAttrs := struct {
Attributes yaml.Node `json:"attributes"`
}{}
if unmarshalAttributesErr := value.Decode(&resourceAttrs); unmarshalAttributesErr != nil {
return unmarshalAttributesErr
}
if unmarshalResourceErr := resourceAttrs.Attributes.Decode(d.Attributes); unmarshalResourceErr != nil {
return unmarshalResourceErr
}
return nil
2024-03-20 19:23:31 +00:00
}
func (d *Declaration) UnmarshalJSON(data []byte) error {
t := &DeclarationType{}
if unmarshalResourceTypeErr := json.Unmarshal(data, t); unmarshalResourceTypeErr != nil {
return unmarshalResourceTypeErr
2024-03-25 20:31:06 +00:00
}
d.Type = t.Type
newResource, resourceErr := ResourceTypes.New(fmt.Sprintf("%s://", d.Type))
if resourceErr != nil {
return resourceErr
}
d.Attributes = newResource
resourceAttrs := struct {
Attributes Resource `json:"attributes"`
}{Attributes: newResource}
if unmarshalAttributesErr := json.Unmarshal(data, &resourceAttrs); unmarshalAttributesErr != nil {
return unmarshalAttributesErr
}
2024-03-25 20:31:06 +00:00
return nil
2024-03-20 19:23:31 +00:00
}
/*
func (d *Declaration) MarshalJSON() ([]byte, error) {
buf := new(bytes.Buffer)
buf.WriteByte('"')
buf.WriteString("value"))
buf.WriteByte('"')
return buf.Bytes(), nil
}
*/
func (d *Declaration) MarshalYAML() (any, error) {
return d, nil
}