1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-12 13:26:13 +00:00
Files
norman/store/schema/schema_store.go

52 lines
1.2 KiB
Go
Raw Normal View History

2017-11-10 21:44:02 -07:00
package schema
import (
"encoding/json"
"strings"
"github.com/rancher/norman/store/empty"
"github.com/rancher/norman/types"
)
type Store struct {
empty.Store
}
func NewSchemaStore() types.Store {
return &Store{}
}
func (s *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
2017-11-21 13:46:30 -07:00
for _, schema := range apiContext.Schemas.SchemasForVersion(*apiContext.Version) {
2017-11-10 21:44:02 -07:00
if strings.EqualFold(schema.ID, id) {
schemaData := map[string]interface{}{}
data, err := json.Marshal(schema)
if err != nil {
return nil, err
}
return schemaData, json.Unmarshal(data, &schemaData)
}
}
return nil, nil
}
2017-11-21 13:46:30 -07:00
func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt types.QueryOptions) ([]map[string]interface{}, error) {
schemaMap := apiContext.Schemas.SchemasForVersion(*apiContext.Version)
schemas := make([]*types.Schema, 0, len(schemaMap))
schemaData := make([]map[string]interface{}, 0, len(schemaMap))
2017-11-10 21:44:02 -07:00
2017-11-21 13:46:30 -07:00
for _, schema := range schemaMap {
schemas = append(schemas, schema)
2017-11-10 21:44:02 -07:00
}
2017-11-21 13:46:30 -07:00
data, err := json.Marshal(schemas)
if err != nil {
2017-11-10 21:44:02 -07:00
return nil, err
}
2017-11-21 13:46:30 -07:00
return schemaData, json.Unmarshal(data, &schemaData)
2017-11-10 21:44:02 -07:00
}