func()

in resources/resources.go [128:170]


func (ks *KernelSpec) 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 name, ok := rawFields["name"]; ok {
		idString, ok := name.(string)
		if !ok {
			return fmt.Errorf("invalid value for the field 'name': %+v: %w", name, util.HTTPError(http.StatusBadRequest))
		}
		ks.ID = idString
	}
	if resources, ok := rawFields["resources"]; ok {
		resourcesMap, ok := resources.(map[string]any)
		if !ok {
			return fmt.Errorf("invalid value for the field 'resources': %+v: %w", resources, util.HTTPError(http.StatusBadRequest))
		}
		ks.Resources = make(map[string]string)
		for name, val := range resourcesMap {
			if resource, ok := val.(string); ok {
				ks.Resources[name] = resource
			}
		}
	}
	spec, ok := rawFields["spec"]
	if !ok {
		ks.rawFields = rawFields
		return nil
	}
	specBytes, err := json.Marshal(spec)
	if err != nil {
		return fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
	}
	if err := json.Unmarshal(specBytes, &ks.Spec); err != nil {
		return fmt.Errorf("failure unmarshalling a nested `spec` field: %w", err)
	}
	ks.rawFields = rawFields
	return nil
}