forked from getkin/kin-openapi
-
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.
Add openapi3.Schema.OrderedPropertyKeys
This commit adds the `OrderedPropertyKeys` method to the `openapi3.Schema`: OrderedPropertyKeys returns the keys of the properties in the order they were defined. This is useful for generating code that needs to iterate over the properties in a consistent order. If the keys could not be extracted for some reason, then this method automatically sorts the keys to be deterministic. This is done via a temporary fork of the YAML-to-JSON transformation library. It will not be ready until these PRs are merged in this order: - silasdavis/yaml#1 - ghodss/yaml#62
- Loading branch information
1 parent
e9b36da
commit 7519cf6
Showing
7 changed files
with
141 additions
and
3 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
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
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,46 @@ | ||
package jsoninfo | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
// ExtractObjectKeys extracts the keys of an object in a JSON string. The keys | ||
// are returned in the order they appear in the JSON string. | ||
func ExtractObjectKeys(b []byte) ([]string, error) { | ||
if !bytes.HasPrefix(b, []byte{'{'}) { | ||
return nil, fmt.Errorf("expected '{' at start of JSON object") | ||
} | ||
|
||
dec := json.NewDecoder(bytes.NewReader(b)) | ||
var keys []string | ||
|
||
for dec.More() { | ||
// Read prop name | ||
t, err := dec.Token() | ||
if err != nil { | ||
log.Printf("Err: %v", err) | ||
break | ||
} | ||
|
||
name, ok := t.(string) | ||
if !ok { | ||
continue // May be a delimeter | ||
} | ||
|
||
keys = append(keys, name) | ||
|
||
var whatever nullMessage | ||
dec.Decode(&whatever) | ||
} | ||
|
||
return keys, nil | ||
} | ||
|
||
// nullMessage implements json.Unmarshaler and does nothing with the given | ||
// value. | ||
type nullMessage struct{} | ||
|
||
func (*nullMessage) UnmarshalJSON(data []byte) error { return nil } |
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,23 @@ | ||
package jsoninfo | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestExtractObjectKeys(t *testing.T) { | ||
const j = `{ | ||
"foo": {"bar": 1}, | ||
"baz": "qux", | ||
"quux": "quuz" | ||
}` | ||
|
||
keys, err := ExtractObjectKeys([]byte(j)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !reflect.DeepEqual(keys, []string{"foo", "baz", "quux"}) { | ||
t.Fatalf("expected %v, got %v", []string{"foo", "baz", "quux"}, keys) | ||
} | ||
} |
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
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
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