Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 569638914
  • Loading branch information
Notebook Kernels Mixer Team authored and copybara-github committed Sep 30, 2023
1 parent 857bd05 commit 94e1d31
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 46 deletions.
46 changes: 21 additions & 25 deletions resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,17 @@ func (ks *KernelSpecs) UnmarshalJSON(b []byte) error {
if !ok {
return fmt.Errorf("invalid value for the field 'kernelspecs': %+v: %w", specs, util.HTTPError(http.StatusBadRequest))
}
if len(ksMap) > 0 {
ks.KernelSpecs = make(map[string]*KernelSpec)
for name, specObj := range ksMap {
specBytes, err := json.Marshal(specObj)
if err != nil {
return fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
var spec KernelSpec
if err := json.Unmarshal(specBytes, &spec); err != nil {
return fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
ks.KernelSpecs[name] = &spec
ks.KernelSpecs = make(map[string]*KernelSpec)
for name, specObj := range ksMap {
specBytes, err := json.Marshal(specObj)
if err != nil {
return fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
var spec KernelSpec
if err := json.Unmarshal(specBytes, &spec); err != nil {
return fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
ks.KernelSpecs[name] = &spec
}
ks.rawFields = rawFields
return nil
Expand All @@ -85,20 +83,18 @@ func (ks KernelSpecs) MarshalJSON() ([]byte, error) {
if len(ks.Default) > 0 {
rawFields["default"] = ks.Default
}
if len(ks.KernelSpecs) > 0 {
specMap := make(map[string]any)
for name, spec := range ks.KernelSpecs {
specBytes, err := json.Marshal(spec)
if err != nil {
return nil, fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
spec := make(map[string]any)
if err := json.Unmarshal(specBytes, &spec); err != nil {
return nil, fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
specMap[name] = spec
specMap := make(map[string]any)
rawFields["kernelspecs"] = specMap
for name, spec := range ks.KernelSpecs {
specBytes, err := json.Marshal(spec)
if err != nil {
return nil, fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
spec := make(map[string]any)
if err := json.Unmarshal(specBytes, &spec); err != nil {
return nil, fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
}
rawFields["kernelspecs"] = specMap
specMap[name] = spec
}
return json.Marshal(rawFields)
}
Expand Down
52 changes: 31 additions & 21 deletions resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ import (

func TestUnmarshalAndMarshalRoundtrip(t *testing.T) {
testCases := []struct {
Description string
Source string
Got any
Want any
Description string
Source string
Got any
Want any
WantMarshalled string
}{
{
Description: "Empty KernelSpecs",
Source: "{}",
Got: &KernelSpecs{},
Want: &KernelSpecs{},
Description: "Empty KernelSpecs",
Source: "{}",
Got: &KernelSpecs{},
Want: &KernelSpecs{},
WantMarshalled: "{\"kernelspecs\":{}}",
},
{
Description: "Empty KernelSpecs with raw fields",
Expand All @@ -46,6 +48,7 @@ func TestUnmarshalAndMarshalRoundtrip(t *testing.T) {
"baz": "bat",
},
},
WantMarshalled: "{\"baz\":\"bat\",\"foo\":\"bar\",\"kernelspecs\":{}}",
},
{
Description: "Simple KernelSpecs",
Expand Down Expand Up @@ -295,20 +298,27 @@ func TestUnmarshalAndMarshalRoundtrip(t *testing.T) {
t.Errorf("Unexpected diff when unmarshalling the source for %q:\n\t %v", testCase.Description, diff)
} else if output, err := json.Marshal(testCase.Got); err != nil {
t.Errorf("Failure marshalling the unmarshalled resource for %q: %v", testCase.Description, err)
} else if err := json.Unmarshal(output, testCase.Got); err != nil {
t.Errorf("Failure unmarshalling the marshalled resource for %q: %v", testCase.Description, err)
} else if diff := cmp.Diff(testCase.Got, testCase.Want, cmpopts.EquateEmpty(), cmpopts.IgnoreUnexported(KernelSpecs{}, KernelSpec{}, Kernel{}, Session{}, Terminal{})); len(diff) > 0 {
t.Errorf("Unexpected diff when unmarshalling the marshalled resource for %q:\n\t %v", testCase.Description, diff)
} else {
sourceRawFields := make(map[string]any)
outputRawFields := make(map[string]any)
if err := json.Unmarshal([]byte(testCase.Source), &sourceRawFields); err != nil {
t.Errorf("Failure unmarshalling the resource for %q as raw fields: %v", testCase.Description, err)
} else if err := json.Unmarshal(output, &outputRawFields); err != nil {
t.Errorf("Failure unmarshalling the result for %q as raw fields: %v", testCase.Description, err)
} else if rawFieldsDiff := cmp.Diff(outputRawFields, sourceRawFields, cmpopts.EquateEmpty(), cmpopts.SortMaps(func(a, b string) bool { return a < b })); len(rawFieldsDiff) > 0 {
t.Logf("Output raw: %v", string(output))
t.Errorf("Unexpected raw fields diff for the marshalled value for %q:\n\t %v", testCase.Description, rawFieldsDiff)
if len(testCase.WantMarshalled) > 0 {
if diff := cmp.Diff(string(output), testCase.WantMarshalled); len(diff) > 0 {
t.Errorf("Unexpected diff when marshalling the unmarshalled resource for %q:\n\t %v", testCase.Description, diff)
}
}
if err := json.Unmarshal(output, testCase.Got); err != nil {
t.Errorf("Failure unmarshalling the marshalled resource for %q: %v", testCase.Description, err)
} else if diff := cmp.Diff(testCase.Got, testCase.Want, cmpopts.EquateEmpty(), cmpopts.IgnoreUnexported(KernelSpecs{}, KernelSpec{}, Kernel{}, Session{}, Terminal{})); len(diff) > 0 {
t.Errorf("Unexpected diff when unmarshalling the marshalled resource for %q:\n\t %v", testCase.Description, diff)
} else if len(testCase.WantMarshalled) == 0 {
sourceRawFields := make(map[string]any)
outputRawFields := make(map[string]any)
if err := json.Unmarshal([]byte(testCase.Source), &sourceRawFields); err != nil {
t.Errorf("Failure unmarshalling the resource for %q as raw fields: %v", testCase.Description, err)
} else if err := json.Unmarshal(output, &outputRawFields); err != nil {
t.Errorf("Failure unmarshalling the result for %q as raw fields: %v", testCase.Description, err)
} else if rawFieldsDiff := cmp.Diff(outputRawFields, sourceRawFields, cmpopts.EquateEmpty(), cmpopts.SortMaps(func(a, b string) bool { return a < b })); len(rawFieldsDiff) > 0 {
t.Logf("Output raw: %v", string(output))
t.Errorf("Unexpected raw fields diff for the marshalled value for %q:\n\t %v", testCase.Description, rawFieldsDiff)
}
}
}
}
Expand Down

0 comments on commit 94e1d31

Please sign in to comment.