diff --git a/templates/php_yaml/message.go b/templates/php_yaml/message.go index 6bbde4de7..9516da32f 100644 --- a/templates/php_yaml/message.go +++ b/templates/php_yaml/message.go @@ -5,7 +5,7 @@ const messageTpl = `{{ $f := .Field }}{{ $r := .Rules }} # Skipping validation for {{ $f.Name }} {{- else }} {{- if (isOfMessageType $f) }} - # Validate {{ $f.Name }} + # Validate {{ fieldQualifiedClassName $f }} {{- end -}} {{- template "required" . }} {{- end -}} diff --git a/templates/php_yaml/msg.go b/templates/php_yaml/msg.go index c251fe90d..ac47f2f53 100644 --- a/templates/php_yaml/msg.go +++ b/templates/php_yaml/msg.go @@ -1,11 +1,13 @@ package php_yaml const msgTpl = ` +{{- if (hasDefinedRules .) }} {{ if not (ignored .) -}} {{ qualifiedName . }}: properties: {{- template "msgInner" . }} {{- end -}} +{{- end -}} ` const msgInnerTpl = ` @@ -17,7 +19,15 @@ const msgInnerTpl = ` {{- render (context .) -}} {{- end -}} {{- range .RealOneOfs }} - {{- render (context .) -}} + {{ .Name }}: + - Valid: ~ + {{- if required . }} + - NotBlank: ~ + {{- end -}} + {{- range .Fields }} + {{ .Name }}: + {{- render (context .) }} + {{- end -}} {{- end -}} {{- range .SyntheticOneOfFields }} {{ .Name }}: diff --git a/templates/php_yaml/oneof.go b/templates/php_yaml/oneof.go index 8b8f12b5e..35591cc95 100644 --- a/templates/php_yaml/oneof.go +++ b/templates/php_yaml/oneof.go @@ -1,9 +1,8 @@ package php_yaml -//const oneOfConstTpl = ` -//{{- range .Fields }}{{ renderConstants (context .) }}{{ end -}} -//` - const oneOfTpl = ` - - TODOOneOf: ~ +{{ range .Fields }} + {{ .Name }}: + {{ render (context .) }}: +{{- end -}} ` diff --git a/templates/php_yaml/register.go b/templates/php_yaml/register.go index f9720bdaf..d65eabb8c 100644 --- a/templates/php_yaml/register.go +++ b/templates/php_yaml/register.go @@ -3,6 +3,7 @@ package php_yaml import ( "bytes" "fmt" + "github.com/envoyproxy/protoc-gen-validate/validate" "strconv" "strings" "text/template" @@ -25,6 +26,8 @@ func Register(tpl *template.Template, params pgs.Parameters) { "classNameMessage": classNameMessage, "simpleName": fns.Name, "qualifiedName": fns.qualifiedName, + "fieldQualifiedClassName": fns.fieldQualifiedClassName, + "hasDefinedRules": fns.hasDefinedRules, "camelCase": fns.camelCase, "fieldName": fns.fieldName, "phpStringEscape": fns.phpStringEscape, @@ -186,6 +189,53 @@ func (fns phpFuncs) qualifiedName(entity pgs.Entity) string { return entity.Name().String() } +func (fns phpFuncs) fieldQualifiedClassName(f pgs.Field) string { + fieldTyp := f.Type() + if fieldTyp.IsEmbed() { + return fns.qualifiedName(fieldTyp.Embed()) + } + + if fieldTyp.IsEnum() { + return fns.qualifiedName(fieldTyp.Enum()) + } + + return f.Type().ProtoType().String() +} + +func (fns phpFuncs) hasDefinedRules(msg pgs.Message) bool { + for _, oneOf := range msg.RealOneOfs() { + if required, _ := shared.RequiredOneOf(oneOf); required { + return true + } + + if hasDefinedRulesInFields(oneOf.Fields()) { + return true + } + } + + if hasDefinedRulesInFields(msg.NonOneOfFields()) { + return true + } + + if hasDefinedRulesInFields(msg.SyntheticOneOfFields()) { + return true + } + + return false +} + +func hasDefinedRulesInFields(fields []pgs.Field) bool { + for _, field := range fields { + var rules validate.FieldRules + ok, _ := field.Extension(validate.E_Rules, &rules) + if ok { + return ok + } + } + + return false +} + // Replace invalid identifier characters with an underscore func makeInvalidClassnameCharactersUnderscores(name string) string { var sb string diff --git a/templates/php_yaml/required.go b/templates/php_yaml/required.go index 30ed5f362..fd431a69a 100644 --- a/templates/php_yaml/required.go +++ b/templates/php_yaml/required.go @@ -1,6 +1,6 @@ package php_yaml -const requiredTpl = `{{ $f := .Field }} +const requiredTpl = ` {{- if .Rules.GetRequired }} - NotBlank: ~ - Valid: ~