in resources/resources.go [335:397]
func (s *Session) UnmarshalJSON(b []byte) error {
rawFields := make(map[string]any)
if err := json.Unmarshal(b, &rawFields); err != nil {
return err
}
if len(rawFields) == 0 {
// The JSON object was empty; leave the structured object empty too.
return nil
}
if idVal, ok := rawFields["id"]; ok {
idString, ok := idVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'id': %+v: %w", idVal, util.HTTPError(http.StatusBadRequest))
}
s.ID = idString
}
if pathVal, ok := rawFields["path"]; ok {
pathString, ok := pathVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'path': %+v: %w", pathVal, util.HTTPError(http.StatusBadRequest))
}
s.Path = pathString
}
if nameVal, ok := rawFields["name"]; ok {
nameString, ok := nameVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'name': %+v: %w", nameVal, util.HTTPError(http.StatusBadRequest))
}
s.Name = nameString
}
if typeVal, ok := rawFields["type"]; ok {
typeString, ok := typeVal.(string)
if !ok {
return fmt.Errorf("invalid value for the field 'type': %+v: %w", typeVal, util.HTTPError(http.StatusBadRequest))
}
s.Type = typeString
}
if notebookVal, ok := rawFields["notebook"]; ok {
notebookMap, ok := notebookVal.(map[string]any)
if !ok {
return fmt.Errorf("invalid value for the field 'notebook': %+v: %w", notebookVal, util.HTTPError(http.StatusBadRequest))
}
s.Notebook = make(map[string]string)
for name, val := range notebookMap {
if valStr, ok := val.(string); ok {
s.Notebook[name] = valStr
}
}
}
k, ok := rawFields["kernel"]
if !ok {
return nil
}
kernelBytes, err := json.Marshal(k)
if err != nil {
return fmt.Errorf("failure unmarshalling a nested `kernel` field: %w", err)
}
if err := json.Unmarshal(kernelBytes, &s.Kernel); err != nil {
return fmt.Errorf("failure unmarshalling a nested `kernel` field: %w", err)
}
s.rawFields = rawFields
return nil
}