-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add unmarshal functions for task config policies (#9896)
- Loading branch information
Showing
8 changed files
with
444 additions
and
97 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package configpolicy | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ghodss/yaml" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
|
||
"github.com/determined-ai/determined/master/pkg/model" | ||
"github.com/determined-ai/determined/master/pkg/protoutils" | ||
) | ||
|
||
// ValidWorkloadType checks if the string is an accepted WorkloadType. | ||
func ValidWorkloadType(val string) bool { | ||
switch val { | ||
case string(model.ExperimentType), string(model.NTSCType): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
// UnmarshalExperimentConfigPolicy unpacks a string into ExperimentConfigPolicy struct. | ||
func UnmarshalExperimentConfigPolicy(str string) (*ExperimentConfigPolicy, error) { | ||
var expConfigPolicy ExperimentConfigPolicy | ||
var err error | ||
|
||
dec := json.NewDecoder(bytes.NewReader([]byte(str))) | ||
dec.DisallowUnknownFields() | ||
if err = dec.Decode(&expConfigPolicy); err == nil { | ||
// valid JSON input | ||
return &expConfigPolicy, nil | ||
} | ||
|
||
if err = yaml.Unmarshal([]byte(str), &expConfigPolicy, yaml.DisallowUnknownFields); err == nil { | ||
// valid Yaml input | ||
return &expConfigPolicy, nil | ||
} | ||
// invalid JSON & invalid Yaml input | ||
return nil, fmt.Errorf("invalid ExperimentConfigPolicy input: %w", err) | ||
} | ||
|
||
// UnmarshalNTSCConfigPolicy unpacks a string into NTSCConfigPolicy struct. | ||
func UnmarshalNTSCConfigPolicy(str string) (*NTSCConfigPolicy, error) { | ||
var ntscConfigPolicy NTSCConfigPolicy | ||
var err error | ||
|
||
dec := json.NewDecoder(bytes.NewReader([]byte(str))) | ||
dec.DisallowUnknownFields() | ||
if err = dec.Decode(&ntscConfigPolicy); err == nil { | ||
// valid JSON input | ||
return &ntscConfigPolicy, nil | ||
} | ||
|
||
if err = yaml.Unmarshal([]byte(str), &ntscConfigPolicy, yaml.DisallowUnknownFields); err == nil { | ||
// valid Yaml input | ||
return &ntscConfigPolicy, nil | ||
} | ||
// invalid JSON & Yaml input | ||
return nil, fmt.Errorf("invalid ExperimentConfigPolicy input: %w", err) | ||
} | ||
|
||
// MarshalConfigPolicy packs a config policy into a proto struct. | ||
func MarshalConfigPolicy(configPolicy interface{}) *structpb.Struct { | ||
return protoutils.ToStruct(configPolicy) | ||
} |
Oops, something went wrong.