Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Format and pattern (regex) for duration (#67)
Browse files Browse the repository at this point in the history
* Format and pattern (regex) for duration

* We only need seconds (not other time units)

Co-authored-by: Chrusty <>
Co-authored-by: chris <chris@Profanity.local>
  • Loading branch information
chrusty and chris authored Jul 20, 2021
1 parent b140dad commit cd4ff2f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions internal/converter/testdata/wellknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ const WellKnown = `{
"type": "array"
},
"duration": {
"additionalProperties": true,
"pattern": "^([0-9]+\\.?[0-9]*|\\.[0-9]+)s$",
"type": "string",
"description": "This is a duration:"
"description": "This is a duration:",
"format": "regex"
}
},
"additionalProperties": true,
Expand Down
5 changes: 5 additions & 0 deletions internal/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes

case descriptor.FieldDescriptorProto_TYPE_GROUP, descriptor.FieldDescriptorProto_TYPE_MESSAGE:
switch desc.GetTypeName() {
// Make sure that durations match a particular string pattern (eg 3.4s):
case ".google.protobuf.Duration":
jsonSchemaType.Type = gojsonschema.TYPE_STRING
jsonSchemaType.Format = "regex"
jsonSchemaType.Pattern = `^([0-9]+\.?[0-9]*|\.[0-9]+)s$`
case ".google.protobuf.Timestamp":
jsonSchemaType.Type = gojsonschema.TYPE_STRING
jsonSchemaType.Format = "date-time"
Expand Down

0 comments on commit cd4ff2f

Please sign in to comment.