-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update error struct and OpenAPI specs (#312)
Co-authored-by: brucexc <[email protected]> Co-authored-by: Henry <[email protected]>
- Loading branch information
1 parent
fd01039
commit c818e11
Showing
10 changed files
with
745 additions
and
44 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,123 @@ | ||
package docs | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sort" | ||
|
||
"github.com/rss3-network/node/schema/worker" | ||
"github.com/rss3-network/protocol-go/schema" | ||
"github.com/rss3-network/protocol-go/schema/activity" | ||
"github.com/rss3-network/protocol-go/schema/network" | ||
"github.com/rss3-network/protocol-go/schema/tag" | ||
"github.com/samber/lo" | ||
"github.com/tidwall/sjson" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var FilePath = "./docs/openapi.json" | ||
|
||
func Generate(endpoint string) ([]byte, error) { | ||
// Read the existing openapi.json file | ||
file, err := os.ReadFile(FilePath) | ||
if err != nil { | ||
zap.L().Error("read file error", zap.Error(err)) | ||
|
||
return nil, err | ||
} | ||
|
||
// Generate servers. | ||
if file, err = generateServers(file, endpoint); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Generate network, tag, platform, direction enum. | ||
if file, err = generateEnum(file); err != nil { | ||
return nil, err | ||
} | ||
|
||
return file, nil | ||
} | ||
|
||
func generateServers(file []byte, endpoint string) ([]byte, error) { | ||
var err error | ||
|
||
file, err = sjson.SetBytes(file, "servers", []map[string]interface{}{ | ||
{"url": endpoint, "description": "Node Endpoint"}, | ||
{"url": "http://localhost", "description": "Localhost"}, | ||
}) | ||
if err != nil { | ||
zap.L().Error("sjson set servers error", zap.Error(err)) | ||
|
||
return nil, err | ||
} | ||
|
||
return file, nil | ||
} | ||
|
||
func generateEnum(file []byte) ([]byte, error) { | ||
var err error | ||
|
||
// Generate network values. | ||
networks := lo.Filter(network.NetworkStrings(), func(s string, _ int) bool { | ||
return !lo.Contains([]string{ | ||
network.Unknown.String(), | ||
network.Bitcoin.String(), | ||
network.SatoshiVM.String(), | ||
network.RSS.String(), | ||
}, s) | ||
}) | ||
|
||
sort.Strings(networks) | ||
|
||
file, err = sjson.SetBytes(file, "components.schemas.Network.enum", networks) | ||
if err != nil { | ||
return nil, fmt.Errorf("sjson set network enum err: %w", err) | ||
} | ||
|
||
// Generate tag values. | ||
tags := tag.TagStrings() | ||
|
||
sort.Strings(tags) | ||
|
||
file, err = sjson.SetBytes(file, "components.schemas.Tag.enum", tags) | ||
if err != nil { | ||
return nil, fmt.Errorf("sjson set tag enum err: %w", err) | ||
} | ||
|
||
// Generate platform values. | ||
platforms := worker.PlatformStrings() | ||
|
||
sort.Strings(platforms) | ||
|
||
file, err = sjson.SetBytes(file, "components.schemas.Platform.enum", platforms) | ||
if err != nil { | ||
return nil, fmt.Errorf("sjson set platform enum err: %w", err) | ||
} | ||
|
||
// Generate direction values. | ||
file, err = sjson.SetBytes(file, "components.schemas.Direction.enum", activity.DirectionStrings()) | ||
if err != nil { | ||
return nil, fmt.Errorf("sjson set direction enum err: %w", err) | ||
} | ||
|
||
// Generate type values. | ||
types := make([]string, 0) | ||
|
||
for _, v := range tag.TagValues() { | ||
for _, t := range schema.GetTypesByTag(v) { | ||
types = append(types, t.Name()) | ||
} | ||
} | ||
|
||
types = lo.Uniq(types) | ||
|
||
sort.Strings(types) | ||
|
||
file, err = sjson.SetBytes(file, "components.schemas.Type.enum", types) | ||
if err != nil { | ||
return nil, fmt.Errorf("sjson set type enum err: %w", err) | ||
} | ||
|
||
return file, nil | ||
} |
Oops, something went wrong.