Skip to content

Commit cd42837

Browse files
authored
Merge pull request #1 from brucechen7274/main
feat: Add support for datetime type
2 parents 5207a7f + f3dfcf5 commit cd42837

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ require (
1414
github.com/spf13/pflag v1.0.6 // indirect
1515
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
1616
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
17+
golang.org/x/mod v0.27.0 // indirect
18+
golang.org/x/sync v0.16.0 // indirect
19+
golang.org/x/tools v0.36.0 // indirect
1720
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
2121
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
2222
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
2323
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
24+
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
25+
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
26+
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
27+
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
28+
golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg=
29+
golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s=
2430
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2531
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2632
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

pkg/gen/gogen.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import (
1919
"bytes"
2020
"encoding/json"
2121
"fmt"
22-
"go/format"
2322
"strings"
2423

24+
"golang.org/x/tools/imports"
25+
2526
"github.com/ostafen/suricata/pkg/spec"
2627
)
2728

@@ -43,14 +44,6 @@ func (gen *CodeGenerator) Generate(spec *spec.Spec) ([]byte, error) {
4344
gen.write("// Code generated by suricata-gen; DO NOT EDIT.\n\n")
4445
gen.write("package %s\n\n", packageName(spec.Package))
4546

46-
gen.write(`import (
47-
"context"
48-
"fmt"
49-
"github.com/ostafen/suricata/runtime"
50-
"github.com/xeipuuv/gojsonschema"
51-
)
52-
` + "\n")
53-
5447
if len(spec.Messages) > 0 {
5548
if err := gen.generateMessageSchemas(spec.Messages); err != nil {
5649
return nil, err
@@ -63,7 +56,8 @@ func (gen *CodeGenerator) Generate(spec *spec.Spec) ([]byte, error) {
6356
gen.generateAgent(name, &svc, spec.Tools)
6457
}
6558

66-
src, err := format.Source(gen.buf.Bytes())
59+
// Use imports.Process to organize imports and format code
60+
src, err := imports.Process("", gen.buf.Bytes(), nil)
6761
if err != nil {
6862
return gen.buf.Bytes(), err
6963
}
@@ -270,6 +264,8 @@ func goTypeForField(f spec.Field) string {
270264
goType = "float64"
271265
case "bool":
272266
goType = "bool"
267+
case "datetime":
268+
goType = "time.Time" // RFC3339 format
273269
default:
274270
// Custom message type
275271
goType = f.Type

pkg/gen/jsonschema.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ func (gen *JSONSchemaGenerator) fieldToSchema(field spec.Field, allMessages map[
9292
baseSchema = map[string]any{"type": "number"}
9393
case "bool":
9494
baseSchema = map[string]any{"type": "boolean"}
95+
case "datetime":
96+
baseSchema = map[string]any{"type": "string", "format": "date-time"} // RFC3339
9597
default:
9698
// Custom type - lookup in allMessages
9799
msg, ok := allMessages[field.Type]

pkg/spec/spec.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ func LoadSpec(path string) (*Spec, error) {
7676
return &spec, spec.Validate()
7777
}
7878

79+
// isPrimitiveType checks if the given type is a built-in primitive type
80+
func isPrimitiveType(t string) bool {
81+
switch t {
82+
case "string", "int", "int32", "int64", "float", "float32", "float64", "bool", "datetime":
83+
return true
84+
default:
85+
return false
86+
}
87+
}
88+
7989
func (spec *Spec) Validate() error {
8090
if spec.Version == "" {
8191
return fmt.Errorf("spec: version is required")
@@ -106,6 +116,12 @@ func (spec *Spec) validateMessages() error {
106116
if field.Type == "" {
107117
return fmt.Errorf("spec: field %q in message %q has empty type", field.Name, name)
108118
}
119+
// Validate field type existence
120+
if !isPrimitiveType(field.Type) {
121+
if _, ok := spec.Messages[field.Type]; !ok {
122+
return fmt.Errorf("spec: field %q in message %q references undefined type %q", field.Name, name, field.Type)
123+
}
124+
}
109125
}
110126
}
111127
return nil

0 commit comments

Comments
 (0)