opentofu/internal/command/views/json/output.go
Martin Atkins f40800b3a4 Move states/ to internal/states/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

56 lines
1.3 KiB
Go

package json
import (
"encoding/json"
"fmt"
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/tfdiags"
)
type Output struct {
Sensitive bool `json:"sensitive"`
Type json.RawMessage `json:"type"`
Value json.RawMessage `json:"value"`
}
type Outputs map[string]Output
func OutputsFromMap(outputValues map[string]*states.OutputValue) (Outputs, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
outputs := make(map[string]Output, len(outputValues))
for name, ov := range outputValues {
unmarked, _ := ov.Value.UnmarkDeep()
value, err := ctyjson.Marshal(unmarked, unmarked.Type())
if err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
fmt.Sprintf("Error serializing output %q", name),
fmt.Sprintf("Error: %s", err),
))
return nil, diags
}
valueType, err := ctyjson.MarshalType(unmarked.Type())
if err != nil {
diags = diags.Append(err)
return nil, diags
}
outputs[name] = Output{
Sensitive: ov.Sensitive,
Type: json.RawMessage(valueType),
Value: json.RawMessage(value),
}
}
return outputs, nil
}
func (o Outputs) String() string {
return fmt.Sprintf("Outputs: %d", len(o))
}