Skip to content

Commit

Permalink
chore: Update documents
Browse files Browse the repository at this point in the history
  • Loading branch information
uanid committed Sep 19, 2023
1 parent 1cf500e commit 5d9897e
Show file tree
Hide file tree
Showing 12 changed files with 1,726 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
proto2/
**/request.pb.bin

protoc-gen-jsonschema
39 changes: 39 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
options:
protoc \
--go_out=./pkg/proto \
--go_opt=paths=source_relative \
--doc_out=. \
--doc_opt=markdown,options.md \
./*.proto

generate-examples:
go build -o protoc-gen-jsonschema main.go
protoc \
--plugin=protoc-gen-jsonschema=./protoc-gen-jsonschema \
--jsonschema_out=./ \
--jsonschema_opt=draft=Draft04 \
--jsonschema_opt=output_file_suffix=.draft-04.json \
--plugin=protoc-gen-jsonschema06=./protoc-gen-jsonschema \
--jsonschema06_out=./ \
--jsonschema06_opt=draft=Draft06 \
--jsonschema06_opt=output_file_suffix=.draft-06.json \
--plugin=protoc-gen-jsonschema07=./protoc-gen-jsonschema \
--jsonschema07_out=./ \
--jsonschema07_opt=draft=Draft07 \
--jsonschema07_opt=output_file_suffix=.draft-07.json \
--plugin=protoc-gen-jsonschema19=./protoc-gen-jsonschema \
--jsonschema19_out=./ \
--jsonschema19_opt=draft=Draft201909 \
--jsonschema19_opt=output_file_suffix=.draft-2019-09.json \
--plugin=protoc-gen-jsonschema20=./protoc-gen-jsonschema \
--jsonschema20_out=./ \
--jsonschema20_opt=draft=Draft202012 \
--jsonschema20_opt=output_file_suffix=.draft-2020-12.json \
--plugin=protoc-gen-jsonschemayaml=./protoc-gen-jsonschema \
--jsonschemayaml_out=./ \
--jsonschemayaml_opt=output_file_suffix=.yaml \
-I ./ \
-I ./examples \
./examples/example.proto
rm protoc-gen-jsonschema

157 changes: 130 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,143 @@
# protoc-gen-jsonschema

목표:
`protoc-gen-jsonschema` is a plugin that converts `protocol buffer` formats to `json-schema`. It doesn't aim to support the entire json-schema specification, but rather focuses solely on converting the protobuf specification. Support json-schema `draft-04, draft-06, draft-07, draft-2019-09, draft-2020-12` versions.

1. protobuf 파일을 jsonschema로 변환한다
2. 전체 json schema 스펙을 지원하지 않는다. (protobuf의 스펙만 json-schema로 변환한다
# Installation

# 기능 대응
If you have go runtime, you can `go install` it.
```
go install github.com/pubg/protoc-gen-jsonschema
```

## protobuf field
Alternatively, you can download pre-built binary from [Github Release](https://github.com/pubg/protoc-gen-jsonschema/releases).

| protobuf label | jsonschema keyword |
|----------------|-----------------------|
| required | required 포함 |
| optional | required 미포함 |
| repeated | array instance로 전환 |
# Usage

| protobuf options | jsonschema keyword |
|------------------------|---------------------------|
| bool nullable | oneof {type: null} |
| stringOptions string | - |
| numericOptions numeric | - |
| arrayOptions array | -, only if has repeated label |
| rawOptions message | - |
Refer to the [Plugin Options](#plguin-options) section below for various options available for this plugin.

#### I'm not sure which options to use
This plugin provides default options that are ready to use. For testing or generating a basic json-schema file, the following command is sufficient without extra options.
```
protoc --jsonschema_out=. *.proto
```

#### Generate with yaml format
```
protoc --jsonschema_out=. --jsonschema_opt=output_file_suffix=.yaml *.proto
```

## protobuf message
#### Shrink bytes for transfer over network.
```
protoc --jsonschema_out=. --jsonschema_opt=pretty_json_output=false
```

| protobuf options | jsonschema keyword |
|---------------------------|----------------------|
| bool additionalProperties | additionalProperties |
#### I'd like to comply with the protobuf JSON mapping standard
By default, this plugin does not comply to the Protobuf standard because most plugins and other JSON libraries do not address integers larger than a 53-bit value. To ensure greater compatibility with other libraries, this plugin converts int64 values to integers instead of strings. However, to comply with the Protobuf standard, int64 values should be converted to strings. If you wish to comply to the Protocol Buffer standard, the option below will assist you.
```
protoc --jsonschema_out=. --jsonschema_opt=int64_as_string=true
```

## protobuf message, field
| protobuf options | jsonschema keyword |
|------------------|--------------------|
| string description | description |
| string $comment | $comment |
| any default | default |
# Options

### Plguin Options

### entrypoint_message
```
entrypoint_message is used which message should be entrypoint object of schema.
default: null or empty
example:
- --jsonschema_opt=entrypoint_message=MyMessage
```

### output_file_suffix
```
output_file_suffix is used to determine output file name suffix.
Values should end with '.json' or '.yaml' or '.yml'.
default: .schema.json
example:
- --jsonschema_opt=output_file_suffix=.schema.json
- --jsonschema_opt=output_file_suffix=.schema.yaml
```

### pretty_json_output
```
pretty_json_output is used to determine output json should be pretty printed.
This option is only used when output_file_suffix is '.json'.
default: true
example:
- --jsonschema_opt=pretty_json_output=true
- --jsonschema_opt=pretty_json_output=false
```

### draft
```
draft is used to determine which draft version should be used.
The value should be one of Draft04, Draft05, Draft06, Draft07, Draft201909, Draft202012.
default: Draft202012
example:
- --jsonschema_opt=draft=Draft202012
```

### mandatory_nullable
```
mandatory_nullable determines whether this plugin should treat optional field as nullable.
Many programming languages do not differentiate between undefined and null.
However, scripting languages like JavaScript and TypeScript can distinguish between them.
By default, optional field is treated as nullable and undefined.
default: false
example:
- --jsonschema_opt=mandatory_nullable=true
- --jsonschema_opt=mandatory_nullable=false
```

### int64_as_string
```
int64_as_string determines whether int64 field treat as string.
Depends on Javascript specification, The JS stores integer to only 53bits.
So, if you want to use int64 field in JS, you should use string type.
References:
default: false
example:
- --jsonschema_opt=int64_as_string=true
- --jsonschema_opt=int64_as_string=false
```

### Protobuf Options

Below tables are not auto-generated features.
Check out the [Options](./options.md) file to see auto-generated options.

| protobuf label | jsonschema |
|----------------|--------------------------------------|
| required | $.required.append(field) |
| optional | oneof {type: null, $original_schema} |
| repeated | type: array, items: $original_schema |

| WellKnown Types |
|-------------------------------------------------|
| k8s.io.apimachinery.pkg.util.intstr.IntOrString |
| k8s.io.api.core.v1.Volume |
| k8s.io.api.core.v1.SecretProjection |
| k8s.io.api.core.v1.ConfigMapVolumeSource |
| k8s.io.api.core.v1.ConfigMapProjection |
| k8s.io.api.core.v1.ConfigMapKeySelector |
| k8s.io.api.core.v1.SecretKeySelector |
| k8s.io.api.core.v1.ConfigMapEnvSource |
| k8s.io.api.core.v1.SecretEnvSource |
| k8s.io.api.core.v1.Probe |
| k8s.io.api.core.v1.EphemeralContainer |
| google.protobuf.Timestamp |
| google.protobuf.Duration |
| google.protobuf.Any |
| google.protobuf.NullValue |

If you'd like contribute well-known types, Please check [generator](./pkg/modules/1_middleend_generator.go) file.

# Output Examples
You can find basic example cases in the `./examples` directory and more complex cases in `./testdata/cases.`
Loading

0 comments on commit 5d9897e

Please sign in to comment.