{{/*
Copyright 2019-present Facebook Inc. All rights reserved.
This source code is licensed under the Apache 2.0 license found
in the LICENSE file in the root directory of this source tree.
*/}}

{{ define "predicate" }}

{{- with extend $ "Package" "predicate" -}}
	{{ template "header" . }}
{{ end }}

import (
	"fmt"

	"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl"
	"github.com/facebookincubator/ent/dialect/sql"
)

{{ range $_, $n := $.Nodes -}}
// {{ $n.Name }} is the predicate function for {{ $n.Package }} builders.
type {{ $n.Name }} func({{ if $.MultiStorage }}interface{}{{ else }}{{ (index $.Storage 0).Builder }}{{ end }})

{{ if $.MultiStorage }}
// {{ $n.Name }}PerDialect construct a predicate for graph traversals based on dialect type.
func {{ $n.Name }}PerDialect({{ range $i, $storage := $.Storage }}{{ if $i }},{{ end }}f{{ $i }} func({{ $storage.Builder }}){{ end }}) {{ $n.Name }} {
	return {{ $n.Name }}(func (v interface{}) {
		switch v := v.(type) {
		{{- range $i, $storage := $.Storage }}
		case {{ $storage.Builder }}:
			f{{ $i }}(v)
		{{- end }}
		default:
			panic(fmt.Sprintf("unknown type for predicate: %T", v))
		}
	})
}
{{ end }}
{{ end }}
{{ end }}