Esempio n. 1
0
func (this *User) Validate() (errors *model.ValidationErrors) {
	errors = model.NoErrors()

	other := U.UserFromClashTag(this.ClashTag)
	if other != nil && other.ID != this.ID {
		errors.Add("ClashTag", "should be unique")
	}

	if this.ClashTag == "new" {
		errors.Add("ClashTag", "the word 'new' is reserved")
	}

	return
}
Esempio n. 2
0
// AddError add error to the db
func (s *DB) AddError(err error) error {
	if err != nil {
		if err != ErrRecordNotFound {
			if s.logMode == 0 {
				go s.print(fileWithLineNum(), err)
			} else {
				s.log(err)
			}

			errors := Errors(s.GetErrors())
			errors = errors.Add(err)
			if len(errors) > 1 {
				err = errors
			}
		}

		s.Error = err
	}
	return err
}
Esempio n. 3
0
func main() {
	flags := getflags()

	log.Println("Working on files:")
	for _, file := range flags.Files {
		log.Println("    " + file)
	}

	ln, _ := net.Listen("tcp", flags.Listen)
	conn, _ := ln.Accept()

	for {
		query, _ := bufio.NewReader(conn).ReadString('\n')

		log.Printf("Message Received: %s\n", query)

		// result sets
		results := set.New()
		errors := set.New()

		// limited concurrency, http://jmoiron.net/blog/limiting-concurrency-in-go/
		concurrency := runtime.NumCPU()
		sem := make(chan bool, concurrency)

		// spawn workers
		for _, file := range flags.Files {
			sem <- true
			go func(this_file string) {
				defer func() { <-sem }()

				matches, err := clangQuery(this_file, query, flags.ClangArgs)

				if err != nil {
					log.Fatal(err)
				}

				for _, match := range matches {
					results.Add(match.String())
				}

				if err != nil {
					errors.Add(clangError{this_file, err.Error()})
					log.Println(err)
				}

			}(file)
		}
		// join workers
		for i := 0; i < cap(sem); i++ {
			sem <- true
		}

		response := &response{
			Matches: []string{}}

		// accumulate results
		for _, match := range set.StringSlice(results) {
			response.Matches = append(response.Matches, match)
		}

		jsonResponse, _ := json.Marshal(response)

		conn.Write([]byte(string(jsonResponse) + "\n"))

		log.Printf("Found %d matches", results.Size())

		conn.Close()
		conn, _ = ln.Accept()
	}
}