func (ep endpoint) Validate() error { if ep.Name == "" { return fmt.Errorf("missing name") } if ep.URI == "" && ep.Abstract == false { return fmt.Errorf("missing URI") } exclusiveLocations := gotility.NewStringSet("json", "postField") foundExclusiveLocations := gotility.StringSet{} for _, p := range ep.Parameters { err := p.Validate() if err != nil { return fmt.Errorf("invalid parameter %q: %s", p.Name, err) } if exclusiveLocations.Contains(p.Location) { if len(foundExclusiveLocations) > 0 && !foundExclusiveLocations.Contains(p.Location) { return fmt.Errorf("incompatible parameter locations: can not mix %q and %q parameters", foundExclusiveLocations.All()[0], p.Location) } foundExclusiveLocations.Set(p.Location) } } return nil }
func generateImports(c *client) { stdImportsSet := gotility.NewStringSet(`"fmt"`, `"net/http"`, `"net/url"`) otherImports := []string{`"github.com/fgrosse/tigs/tigshttp"`} if c.containsJSONEndpoints() { stdImportsSet.Set(`"encoding/json"`) stdImportsSet.Set(`"bytes"`) stdImportsSet.Set(`"io/ioutil"`) } if c.containsPostfieldEndpoints() { stdImportsSet.Set(`"strings"`) stdImportsSet.Set(`"io/ioutil"`) } stdImports := stdImportsSet.All() sort.Strings(stdImports) sort.Strings(otherImports) c.Imports = strings.Join(stdImports, "\n\t") + "\n\n\t" + strings.Join(otherImports, "\n\t") for i := range c.Endpoints { c.Endpoints[i].ClientName = c.Name } }
package gotility_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/fgrosse/gotility" ) var _ = Describe("StringSet", func() { Describe("NewStringSet", func() { It("should initialize a new StringSet", func() { s := gotility.NewStringSet("foo", "bar", "baz") Expect(s.All()).To(Equal([]string{"foo", "bar", "baz"})) }) }) It("should have a simple method to check if a key is set", func() { s := gotility.StringSet{} key := "test" Expect(s.Contains(key)).To(BeFalse()) s.Set(key) Expect(s.Contains(key)).To(BeTrue()) }) })