Skip to content

Commit

Permalink
#1 Simplify file layout and merge functions in existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
ppxl committed Jan 21, 2025
1 parent b571be3 commit 237d438
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 107 deletions.
82 changes: 82 additions & 0 deletions json/blueprint.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,87 @@
package json

import (
"bytes"
"encoding/json"
"fmt"
)

// BlueprintApi is a string that contains a Blueprint API version identifier.
type BlueprintApi string

const (
// V1 is the classic version 1 API identifier of Cloudogu EcoSystem blueprint mechanism inside VMs.
V1 BlueprintApi = "v1"
// TestEmpty is a non-production, test-only API identifier of Cloudogu EcoSystem blueprint mechanism.
TestEmpty BlueprintApi = "test/empty"
)

// GeneralBlueprint defines the minimum set to parse the blueprint API version string in order to select the right
// blueprint handling strategy. This is necessary in order to accommodate maximal changes in different blueprint API
// versions.
type GeneralBlueprint struct {
// API is used to distinguish between different versions of the used API and impacts directly the interpretation of
// this blueprint. Must not be empty.
//
// This field MUST NOT be MODIFIED or REMOVED because the API is paramount for distinguishing between different
// blueprint version implementations.
API BlueprintApi `json:"blueprintApi"`
}

// TargetState defines an enum of values that determines a state of installation.
type TargetState int

const (
// TargetStatePresent is the default state. If selected the chosen item must be present after the blueprint was
// applied.
TargetStatePresent TargetState = iota
// TargetStateAbsent sets the state of the item to absent. If selected the chosen item must be absent after the
// blueprint was applied.
TargetStateAbsent
// TargetStateIgnore is currently only internally used to mark items that are present in the CES instance at hand
// but not mentioned in the blueprint.
TargetStateIgnore
)

// String returns a string representation of the given TargetState enum value.
func (state TargetState) String() string {
return toString[state]
}

var toString = map[TargetState]string{
TargetStatePresent: "present",
TargetStateAbsent: "absent",
}

var toID = map[string]TargetState{
"present": TargetStatePresent,
"absent": TargetStateAbsent,
}

// MarshalJSON marshals the enum as a quoted json string
func (state TargetState) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(toString[state])
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}

// UnmarshalJSON unmarshals a quoted json string to the enum value. Use it with usual json unmarshalling:
//
// jsonBlob := []byte("\"present\"")
// var state TargetState
// err := json.Unmarshal(jsonBlob, &state)
func (state *TargetState) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return fmt.Errorf("cannot unmarshal value %s to a TargetState: %w", string(b), err)
}
// Note that if the string cannot be found then it will be set to the zero value, 'Created' in this case.
*state = toID[j]
return nil
}

// BlueprintV1 describes an abstraction of Cloudogu EcoSystem (CES) parts that should be absent or present within one or
// more CES instances. When the same Blueprint is applied to two different CES instances it is required to leave two
// equal instances in terms of the components.
Expand Down
2 changes: 1 addition & 1 deletion json/blueprintMask.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type BlueprintMaskApi string

const (
// BlueprintMaskAPIV1 contains the API version number of the Blueprint Mask mechanism.
BlueprintMaskAPIV1 BlueprintApi = "v1"
BlueprintMaskAPIV1 BlueprintMaskApi = "v1"
)

// GeneralBlueprintMask defines the minimum set to parse the blueprint mask API version string in order to select the
Expand Down
2 changes: 1 addition & 1 deletion json/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package json

import (
"encoding/json"
"github.com/stretchr/testify/require"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTargetState_String(t *testing.T) {
Expand Down
105 changes: 0 additions & 105 deletions json/core.go

This file was deleted.

0 comments on commit 237d438

Please sign in to comment.