-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
affb2e8
commit 7e73d30
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# simple_json_schema_like | ||
|
||
## Parse | ||
|
||
`Parse` is a Go function that receives anything and tries to parse it to a JSON schema-like representation that has the same type as the input. | ||
|
||
### Usage | ||
|
||
The function takes any input value and returns a value with the same type as the input, but represented in a JSON schema-like format. The returned value can be of any type, including struct, map, slice, array, pointer to a struct, slice, or map, or any primitive type. | ||
|
||
To use the `Parse` function, simply call it with the input value: | ||
|
||
```go | ||
v := struct { | ||
Age int | ||
Name string | ||
}{ | ||
Age: 30, | ||
Name: "John", | ||
} | ||
|
||
parsed := Parse(v) | ||
``` | ||
|
||
The `parsed` variable will now contain a map with the same fields as the input struct: | ||
|
||
```go | ||
map[string]interface{}{ | ||
"Age": "int", | ||
"Name": "string", | ||
} | ||
``` | ||
|
||
### Output Format | ||
|
||
The output format of the `Parse` function depends on the input type: | ||
|
||
- Struct: the output is a map with the same fields as the input struct. The keys of the map are the field names, and the values are the field types represented as strings. | ||
|
||
- Map: the output is a map with the same keys as the input map. The values of the map are the corresponding types of the input map values, represented as strings. | ||
|
||
- Slice or Array: the output is a slice of the same length as the input slice or array. Each element of the output slice is the corresponding type of the input slice or array elements, represented as strings. | ||
|
||
- Pointer to Struct, Slice, or Map: the output is a pointer to the corresponding JSON schema-like representation. | ||
|
||
- Other types: the output is the type name as a string. | ||
|
||
### Examples | ||
|
||
Here are some examples of input and output for the `Parse` function: | ||
|
||
```go | ||
// Struct | ||
v := struct { | ||
Age int | ||
Name string | ||
}{ | ||
Age: 30, | ||
Name: "John", | ||
} | ||
|
||
parsed := Parse(v) | ||
// parsed = map[string]interface{}{ | ||
// "Age": "int", | ||
// "Name": "string", | ||
// } | ||
|
||
// Map | ||
v := map[string]int{ | ||
"foo": 42, | ||
"bar": 69, | ||
} | ||
|
||
parsed := Parse(v) | ||
// parsed = map[string]interface{}{ | ||
// "foo": "int", | ||
// "bar": "int", | ||
// } | ||
|
||
// Slice | ||
v := []interface{}{ | ||
"foo", | ||
42, | ||
} | ||
|
||
parsed := Parse(v) | ||
// parsed = []interface{}{ | ||
// "string", | ||
// "int", | ||
// } | ||
|
||
// Pointer to Struct | ||
v := &struct { | ||
Age int | ||
Name string | ||
}{ | ||
Age: 30, | ||
Name: "John", | ||
} | ||
|
||
parsed := Parse(v) | ||
// parsed = *map[string]interface{}{ | ||
// "Age": "int", | ||
// "Name": "string", | ||
// } | ||
|
||
// Other types | ||
v := true | ||
|
||
parsed := Parse(v) | ||
// parsed = "bool" | ||
``` |