Skip to content

Commit

Permalink
Add oneOf validation for php_yaml, tweak templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rauanmayemir committed May 5, 2022
1 parent 9d4497f commit e68af3c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion templates/php_yaml/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down
12 changes: 11 additions & 1 deletion templates/php_yaml/msg.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package php_yaml

const msgTpl = `
{{- if (hasDefinedRules .) }}
{{ if not (ignored .) -}}
{{ qualifiedName . }}:
properties:
{{- template "msgInner" . }}
{{- end -}}
{{- end -}}
`

const msgInnerTpl = `
Expand All @@ -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 }}:
Expand Down
9 changes: 4 additions & 5 deletions templates/php_yaml/oneof.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package php_yaml

//const oneOfConstTpl = `
//{{- range .Fields }}{{ renderConstants (context .) }}{{ end -}}
//`

const oneOfTpl = `
- TODOOneOf: ~
{{ range .Fields }}
{{ .Name }}:
{{ render (context .) }}:
{{- end -}}
`
50 changes: 50 additions & 0 deletions templates/php_yaml/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package php_yaml
import (
"bytes"
"fmt"
"github.com/envoyproxy/protoc-gen-validate/validate"
"strconv"
"strings"
"text/template"
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion templates/php_yaml/required.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package php_yaml

const requiredTpl = `{{ $f := .Field }}
const requiredTpl = `
{{- if .Rules.GetRequired }}
- NotBlank: ~
- Valid: ~
Expand Down

0 comments on commit e68af3c

Please sign in to comment.