swaggest / jsonschema-go

JSON Schema mapping for Go
https://pkg.go.dev/github.com/swaggest/jsonschema-go
MIT License
102 stars 13 forks source link

Add `default` tag support (JSON value) for non-scalar schemas #71

Closed vearutop closed 1 year ago

vearutop commented 1 year ago

Fixes #70.

func ExampleReflector_Reflect_default() {
    type MyStruct struct {
        A []string       `json:"a" default:"[A,B,C]"` // For an array of strings, comma-separated values in square brackets can be used.
        B []int          `json:"b" default:"[1,2,3]"` // Other non-scalar values are parsed as JSON without type checking.
        C []string       `json:"c" default:"[\"C\",\"B\",\"A\"]"`
        D int            `json:"d" default:"123"` // Scalar values are parsed according to their type.
        E string         `json:"e" default:"abc"`
        F map[string]int `json:"f" default:"{\"foo\":1,\"bar\":2}"`
    }

    type Invalid struct {
        I []int `json:"i" default:"[C,B,A]"` // Value with invalid JSON is ignored for types other than []string (and equivalent).
    }

    r := jsonschema.Reflector{}
    s, _ := r.Reflect(MyStruct{})
    _, err := r.Reflect(Invalid{})

    j, _ := assertjson.MarshalIndentCompact(s, "", " ", 80)

    fmt.Println("MyStruct:", string(j))
    fmt.Println("Invalid error:", err.Error())
    // Output:
    // MyStruct: {
    //  "properties":{
    //   "a":{"default":["A","B","C"],"items":{"type":"string"},"type":["array","null"]},
    //   "b":{"default":[1,2,3],"items":{"type":"integer"},"type":["array","null"]},
    //   "c":{"default":["C","B","A"],"items":{"type":"string"},"type":["array","null"]},
    //   "d":{"default":123,"type":"integer"},"e":{"default":"abc","type":"string"},
    //   "f":{
    //    "default":{"bar":2,"foo":1},"additionalProperties":{"type":"integer"},
    //    "type":["object","null"]
    //   }
    //  },
    //  "type":"object"
    // }
    // Invalid error: I: parsing default as JSON: invalid character 'C' looking for beginning of value
}
github-actions[bot] commented 1 year ago

Lines Of Code

Language Files Lines Code Comments Blanks Complexity Bytes
Go 6 1899 (+26) 1290 (+19) 217 (+1) 392 (+6) 505 (+8) 46K (+699B)
Go (test) 11 2495 (+39) 1784 (+19) 280 (+15) 431 (+5) 45 63.1K (+1.6K)
github-actions[bot] commented 1 year ago

Unit Test Coverage

total: (statements) 78.6% changed lines: (statements) 85.2%, coverage is less than 90.0%, consider testing the changes more thoroughly

Coverage of changed lines | File | Function | Coverage | |----------------|------------------|----------| | Total | | 85.2% | | reflect.go | | 85.2% | | reflect.go:898 | checkInlineValue | 93.6% | | reflect.go:740 | walkProperties | 84.6% |
Coverage diff with base branch | File | Function | Base Coverage | Current Coverage | |----------------------------------------------|------------------|---------------|------------------| | Total | | 78.2% | 78.6% (+0.4%) | | github.com/swaggest/jsonschema-go/reflect.go | checkInlineValue | 80.0% | 86.8% (+6.8%) | | github.com/swaggest/jsonschema-go/reflect.go | walkProperties | 87.3% | 88.5% (+1.2%) |
github-actions[bot] commented 1 year ago

Go API Changes

# summary
Inferred base version: v0.3.43
Suggested version: v0.3.44
codecov[bot] commented 1 year ago

Codecov Report

Merging #71 (1b1f5f3) into master (22e0b30) will increase coverage by 0.61%. The diff coverage is 82.75%.

@@            Coverage Diff             @@
##           master      #71      +/-   ##
==========================================
+ Coverage   72.21%   72.82%   +0.61%     
==========================================
  Files           6        6              
  Lines        1040     1056      +16     
==========================================
+ Hits          751      769      +18     
+ Misses        219      218       -1     
+ Partials       70       69       -1     
Flag Coverage Δ
unittests 72.82% <82.75%> (+0.61%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
reflect.go 81.85% <82.75%> (+0.93%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

github-actions[bot] commented 1 year ago

Benchmark Result

Benchmark diff with base branch ``` name old time/op new time/op delta Schema_UnmarshalJSON_raw-2 73.9µs ± 5% 69.9µs ± 1% -5.40% (p=0.004 n=6+5) Schema_UnmarshalJSON-2 563µs ± 6% 556µs ± 5% ~ (p=0.485 n=6+6) Schema_MarshalJSON_raw-2 67.0µs ± 6% 65.9µs ± 4% ~ (p=0.937 n=6+6) Schema_MarshalJSON-2 218µs ± 2% 222µs ± 3% ~ (p=0.126 n=5+6) name old alloc/op new alloc/op delta Schema_UnmarshalJSON_raw-2 31.7kB ± 0% 31.7kB ± 0% ~ (p=0.264 n=6+6) Schema_UnmarshalJSON-2 184kB ± 0% 184kB ± 0% -0.00% (p=0.030 n=5+6) Schema_MarshalJSON_raw-2 21.9kB ± 0% 21.9kB ± 0% ~ (p=0.970 n=5+6) Schema_MarshalJSON-2 55.3kB ± 0% 55.3kB ± 0% ~ (p=0.762 n=6+5) name old allocs/op new allocs/op delta Schema_UnmarshalJSON_raw-2 457 ± 0% 457 ± 0% ~ (all equal) Schema_UnmarshalJSON-2 2.00k ± 0% 2.00k ± 0% ~ (all equal) Schema_MarshalJSON_raw-2 484 ± 0% 484 ± 0% ~ (all equal) Schema_MarshalJSON-2 477 ± 0% 477 ± 0% ~ (all equal) ```
Benchmark result ``` name time/op Schema_UnmarshalJSON_raw-2 69.9µs ± 1% Schema_UnmarshalJSON-2 556µs ± 5% Schema_MarshalJSON_raw-2 65.9µs ± 4% Schema_MarshalJSON-2 222µs ± 3% name alloc/op Schema_UnmarshalJSON_raw-2 31.7kB ± 0% Schema_UnmarshalJSON-2 184kB ± 0% Schema_MarshalJSON_raw-2 21.9kB ± 0% Schema_MarshalJSON-2 55.3kB ± 0% name allocs/op Schema_UnmarshalJSON_raw-2 457 ± 0% Schema_UnmarshalJSON-2 2.00k ± 0% Schema_MarshalJSON_raw-2 484 ± 0% Schema_MarshalJSON-2 477 ± 0% ```