Merge pull request #1466 from travisturner/fix-schema-output-again

ensure /schema excludes views and includes all field options (like "keys")
This commit is contained in:
Travis Turner 2018-07-05 20:44:18 -05:00 committed by GitHub
commit 7bde403faa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 14 deletions

6
api.go
View file

@ -482,9 +482,9 @@ func (api *API) ClusterMessage(ctx context.Context, reqBody io.Reader) error {
}
// Schema returns information about each index in Pilosa including which fields
// and views they contain.
func (api *API) Schema(ctx context.Context) []*Index {
return api.holder.Indexes()
// they contain.
func (api *API) Schema(ctx context.Context) []*IndexInfo {
return api.holder.limitedSchema()
}
// Views returns the views in the given field.

View file

@ -1094,9 +1094,9 @@ func (f *Field) ImportValue(columnIDs []uint64, values []int64) error {
func (f *Field) MarshalJSON() ([]byte, error) {
thing := struct {
Name string
Options FieldOptions
Views []*ViewInfo
Name string `json:"name"`
Options FieldOptions `json:"options"`
Views []*ViewInfo `json:"views"`
}{
Name: f.Name(),
Options: f.Options(),
@ -1134,7 +1134,7 @@ type FieldOptions struct {
Min int64 `json:"min,omitempty"`
Max int64 `json:"max,omitempty"`
TimeQuantum TimeQuantum `json:"timeQuantum,omitempty"`
Keys bool `json:"keys,omitempty"`
Keys bool `json:"keys"`
}
// applyDefaultOptions returns a new FieldOptions object
@ -1177,28 +1177,34 @@ func (o *FieldOptions) MarshalJSON() ([]byte, error) {
Type string `json:"type"`
CacheType string `json:"cacheType"`
CacheSize uint32 `json:"cacheSize"`
Keys bool `json:"keys"`
}{
o.Type,
o.CacheType,
o.CacheSize,
o.Keys,
})
case FieldTypeInt:
return json.Marshal(struct {
Type string `json:"type"`
Min int64 `json:"min"`
Max int64 `json:"max"`
Keys bool `json:"keys"`
}{
o.Type,
o.Min,
o.Max,
o.Keys,
})
case FieldTypeTime:
return json.Marshal(struct {
Type string `json:"type"`
TimeQuantum TimeQuantum `json:"timeQuantum"`
Keys bool `json:"keys"`
}{
o.Type,
o.TimeQuantum,
o.Keys,
})
}
return nil, errors.New("invalid field type")

View file

@ -228,6 +228,22 @@ func (h *Holder) Schema() []*IndexInfo {
return a
}
// limitedSchema returns schema information for all indexes and fields.
func (h *Holder) limitedSchema() []*IndexInfo {
var a []*IndexInfo
for _, index := range h.Indexes() {
di := &IndexInfo{Name: index.Name()}
for _, field := range index.Fields() {
fi := &FieldInfo{Name: field.Name(), Options: field.Options()}
di.Fields = append(di.Fields, fi)
}
sort.Sort(fieldInfoSlice(di.Fields))
a = append(a, di)
}
sort.Sort(indexInfoSlice(a))
return a
}
// applySchema applies an internal Schema to Holder.
func (h *Holder) applySchema(schema *Schema) error {
// Create indexes that don't exist.

View file

@ -463,7 +463,7 @@ func (h *Handler) handleGetIndex(w http.ResponseWriter, r *http.Request) {
}
indexName := mux.Vars(r)["index"]
for _, idx := range h.API.Schema(r.Context()) {
if idx.Name() == indexName {
if idx.Name == indexName {
if err := json.NewEncoder(w).Encode(idx); err != nil {
h.Logger.Printf("write response error: %s", err)
}

View file

@ -83,11 +83,13 @@ func (i *Index) MarshalJSON() ([]byte, error) {
fields = append(fields, f)
}
thing := struct {
Name string
Fields []*Field
Name string `json:"name"`
Options IndexOptions `json:"options"`
Fields []*Field `json:"fields"`
}{
Name: i.name,
Fields: fields,
Name: i.name,
Options: i.Options(),
Fields: fields,
}
return json.Marshal(thing)
}
@ -422,8 +424,9 @@ func (p indexSlice) Less(i, j int) bool { return p[i].Name() < p[j].Name() }
// IndexInfo represents schema information for an index.
type IndexInfo struct {
Name string `json:"name"`
Fields []*FieldInfo `json:"fields"`
Name string `json:"name"`
Options IndexOptions `json:"options"`
Fields []*FieldInfo `json:"fields"`
}
type indexInfoSlice []*IndexInfo