<commit_before>//    Copyright 2017 drillbits
//
//    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 srgen

import (
	"bufio"
	"bytes"
	"fmt"
	"go/ast"
	"go/format"
	"go/parser"
	"go/token"
	"os"
	"sort"
	"strings"
	"text/template"
)

var servicesTmpl = `package {{.Package}}

import (
	"fmt"
	"strings"
	{{- range .Imports}}
	{{if .Using}}{{.Name}} {{.Value}}{{end}}
	{{- end}}
)

var reg = &ServiceRegistry{}

// ServiceRegistry is an registry for services.
type ServiceRegistry struct {
	valid bool

{{- range .Services}}
	{{.Name}} {{.Name}}
{{- end}}
}

// Validate reports whether all services are fulfilled.
func (reg *ServiceRegistry) Validate() error {
	if reg.valid {
		return nil
	}

	var errs []string

{{- range .Services}}
	if reg.{{.Name}} == nil {
		errs = append(errs, "{{.Name}}")
	}
{{- end}}

	if len(errs) > 0 {
		return fmt.Errorf("nil service(s): %s", strings.Join(errs, ", "))
	}

	return nil
}

// InitServiceRegistry initializes the ServiceRegistry.
func InitServiceRegistry(
{{- range .Services}}
	{{.Name}} {{.Name}},
{{- end}}
) {
{{- range .Services}}
	reg.{{.Name}} = {{.Name}}
{{- end}}
}

// Services returns the ServiceRegistry.
func Services() *ServiceRegistry {
	return reg
}

// MustServices is like Services but panics if all services are not fulfilled.
func MustServices() *ServiceRegistry {
	if err := reg.Validate(); err != nil {
		panic(err)
	}
	return reg
}

{{- range $_, $s := .Services}}
// {{$s.Name}}Mock implements {{$s.Name}} for mocking.
type {{$s.Name}}Mock struct {
	{{- range $_, $m := $s.Methods}}
	{{- range $i, $r := $m.Results}}
	{{$m.Name}}Ret{{$i}} {{$r}}
	{{- end}}
	{{- end}}
}

{{- range $_, $m := $s.Methods}}
func (s *{{$s.Name}}Mock) {{$m.Name}}(
	{{- range $i, $p := $m.Params}}{{if $i}}, {{end}}{{$p}}{{end -}}
) (
	{{- range $i, $r := $m.Results}}{{if $i}}, {{end}}{{$r}}{{end -}}
) {
	return {{range $i, $r := $m.Results -}}
	{{- if $i}}, {{end -}}
	s.{{$m.Name}}Ret{{$i}}
	{{- end}}
}
{{- end}}

{{end}}
`

type Import struct {<commit_msg>Generate comment for methods of mocks<commit_after>//    Copyright 2017 drillbits
//
//    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 srgen

import (
	"bufio"
	"bytes"
	"fmt"
	"go/ast"
	"go/format"
	"go/parser"
	"go/token"
	"os"
	"sort"
	"strings"
	"text/template"
)

var servicesTmpl = `package {{.Package}}

import (
	"fmt"
	"strings"
	{{- range .Imports}}
	{{if .Using}}{{.Name}} {{.Value}}{{end}}
	{{- end}}
)

var reg = &ServiceRegistry{}

// ServiceRegistry is an registry for services.
type ServiceRegistry struct {
	valid bool

{{- range .Services}}
	{{.Name}} {{.Name}}
{{- end}}
}

// Validate reports whether all services are fulfilled.
func (reg *ServiceRegistry) Validate() error {
	if reg.valid {
		return nil
	}

	var errs []string

{{- range .Services}}
	if reg.{{.Name}} == nil {
		errs = append(errs, "{{.Name}}")
	}
{{- end}}

	if len(errs) > 0 {
		return fmt.Errorf("nil service(s): %s", strings.Join(errs, ", "))
	}

	return nil
}

// InitServiceRegistry initializes the ServiceRegistry.
func InitServiceRegistry(
{{- range .Services}}
	{{.Name}} {{.Name}},
{{- end}}
) {
{{- range .Services}}
	reg.{{.Name}} = {{.Name}}
{{- end}}
}

// Services returns the ServiceRegistry.
func Services() *ServiceRegistry {
	return reg
}

// MustServices is like Services but panics if all services are not fulfilled.
func MustServices() *ServiceRegistry {
	if err := reg.Validate(); err != nil {
		panic(err)
	}
	return reg
}

{{- range $_, $s := .Services}}
// {{$s.Name}}Mock implements {{$s.Name}} for mocking.
type {{$s.Name}}Mock struct {
	{{- range $_, $m := $s.Methods}}
	{{- range $i, $r := $m.Results}}
	{{$m.Name}}Ret{{$i}} {{$r}}
	{{- end}}
	{{- end}}
}

{{- range $_, $m := $s.Methods}}
// {{$m.Name}} implements for mocking.
func (s *{{$s.Name}}Mock) {{$m.Name}}(
	{{- range $i, $p := $m.Params}}{{if $i}}, {{end}}{{$p}}{{end -}}
) (
	{{- range $i, $r := $m.Results}}{{if $i}}, {{end}}{{$r}}{{end -}}
) {
	return {{range $i, $r := $m.Results -}}
	{{- if $i}}, {{end -}}
	s.{{$m.Name}}Ret{{$i}}
	{{- end}}
}
{{- end}}

{{end}}
`

type Import struct {
{'fraction_non_alphanumeric': 0.20984305367176903, 'fraction_numerical': 0.0034876961829102887, 'mean_word_length': 3.305254378648874, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3879805', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1838, 'n_words': 549}