apiserver builds again

This commit is contained in:
Daniel Smith 2014-06-16 18:03:44 -07:00
parent 1c6342a794
commit 154ec0db1e
4 changed files with 51 additions and 98 deletions

View File

@ -24,11 +24,13 @@ import (
"net/http"
"net/url"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)
// RESTStorage is a generic interface for RESTful storage services
type RESTStorage interface {
List(LabelQuery) (interface{}, error)
List(labels.Query) (interface{}, error)
Get(id string) (interface{}, error)
Delete(id string) error
Extract(body string) (interface{}, error)
@ -146,7 +148,7 @@ func (server *ApiServer) handleREST(parts []string, requestUrl *url.URL, req *ht
case "GET":
switch len(parts) {
case 1:
query, err := ParseLabelQuery(requestUrl.Query().Get("labels"))
query, err := labels.ParseQuery(requestUrl.Query().Get("labels"))
if err != nil {
server.error(err, w)
return

View File

@ -24,6 +24,8 @@ import (
"net/http/httptest"
"reflect"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)
// TODO: This doesn't reduce typing enough to make it worth the less readable errors. Remove.
@ -49,7 +51,7 @@ type SimpleRESTStorage struct {
updated Simple
}
func (storage *SimpleRESTStorage) List(LabelQuery) (interface{}, error) {
func (storage *SimpleRESTStorage) List(labels.Query) (interface{}, error) {
result := SimpleList{
Items: storage.list,
}

44
pkg/labels/labels.go Normal file
View File

@ -0,0 +1,44 @@
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package labels
import (
"strings"
)
// Labels allows you to present labels independently from their storage.
type Labels interface {
Get(label string) (value string)
}
// A map of label:value. Implements Labels.
type LabelSet map[string]string
// All labels listed as a human readable string. Conveiently, exactly the format
// that ParseQuery takes.
func (ls LabelSet) String() string {
query := make([]string, 0, len(ls))
for key, value := range ls {
query = append(query, key+"="+value)
}
return strings.Join(query, ",")
}
// Implement Labels interface.
func (ls LabelSet) Get(label string) string {
return ls[label]
}

View File

@ -21,29 +21,6 @@ import (
"strings"
)
// Labels allows you to present labels.
type Labels interface {
Get(label string) (value string)
}
// A map of label:value. Implements Labels.
type LabelSet map[string]string
// All labels listed as a human readable string. Conveiently, exactly the format
// that ParseQuery takes.
func (ls LabelSet) String() string {
query := make([]string, 0, len(ls))
for key, value := range ls {
query = append(query, key+"="+value)
}
return strings.Join(query, ",")
}
// Implement Labels interface.
func (ls LabelSet) Get(label string) string {
return ls[label]
}
// Represents a label query.
type Query interface {
// Returns true if this query matches the given set of labels.
@ -150,75 +127,3 @@ func (l *queryTerm) String() (out string) {
}
return ""
}
/*
type parseErr struct {
Reason string
Pos token.Pos
}
func (p parseErr) Error() string {
return fmt.Sprintf("%v: %v", p.Reason, p.Pos)
}
func fromLiteral(expr *ast.BinaryExpr) (*queryTerm, error) {
lhs, ok := expr.X.(*ast.Ident)
if !ok {
return nil, parseErr{"expected literal", expr.X.Pos()}
}
}
func fromBinaryExpr(expr *ast.BinaryExpr) (*queryTerm, error) {
switch expr.Op {
case token.EQL, token.NEQ:
return fromLiteral(expr)
}
lhs, err := fromExpr(expr.X)
if err != nil {
return nil, err
}
rhs, err := fromExpr(expr.Y)
if err != nil {
return nil, err
}
switch expr.Op {
case token.AND, token.LAND:
return &queryTerm{And: []LabelQuery{lhs, rhs}}
case token.OR, token.LOR:
return &queryTerm{Or: []LabelQuery{lhs, rhs}}
}
}
func fromUnaryExpr(expr *ast.UnaryExpr) (*queryTerm, error) {
if expr.Op == token.NOT {
lqt, err := fromExpr(expr.X)
if err != nil {
return nil, err
}
lqt.not = !lqt.not
return lqt, nil
}
return nil, parseErr{"unrecognized unary expression", expr.OpPos}
}
func fromExpr(expr ast.Expr) (*queryTerm, error) {
switch v := expr.(type) {
case *ast.UnaryExpr:
return fromUnaryExpr(v)
case *ast.BinaryExpr:
return fromBinaryExpr(v)
}
return nil, parseErr{"unrecognized expression type", expr.Pos()}
}
// Takes a string repsenting a label query and returns an object suitable for matching, or an error.
func ParseLabelQuery(query string) (LabelQuery, error) {
expr, err := parser.ParseExpr(query)
if err != nil {
return nil, err
}
log.Printf("%v: %v (%#v)", query, expr, expr)
return fromExpr(expr)
}
*/