forgejo/models/forgefed/nodeinfo.go

124 lines
3 KiB
Go
Raw Normal View History

2023-12-29 11:10:07 +00:00
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package forgefed
import (
"net/url"
"code.gitea.io/gitea/modules/validation"
2024-05-06 14:01:23 +00:00
2023-12-29 11:10:07 +00:00
"github.com/valyala/fastjson"
)
// ToDo: Search for full text SourceType and Source, also in .md files
2023-12-29 11:10:07 +00:00
type (
SoftwareNameType string
2023-12-29 11:10:07 +00:00
)
const (
ForgejoSourceType SoftwareNameType = "forgejo"
GiteaSourceType SoftwareNameType = "gitea"
2023-12-29 11:10:07 +00:00
)
2023-12-29 14:48:45 +00:00
var KnownSourceTypes = []any{
ForgejoSourceType, GiteaSourceType,
2023-12-29 11:10:07 +00:00
}
2023-12-29 14:48:45 +00:00
// ------------------------------------------------ NodeInfoWellKnown ------------------------------------------------
2023-12-29 11:10:07 +00:00
// NodeInfo data type
// swagger:model
type NodeInfoWellKnown struct {
Href string
}
2023-12-29 14:48:45 +00:00
// Factory function for PersonID. Created struct is asserted to be valid
2023-12-29 11:10:07 +00:00
func NewNodeInfoWellKnown(body []byte) (NodeInfoWellKnown, error) {
result, err := NodeInfoWellKnownUnmarshalJSON(body)
if err != nil {
return NodeInfoWellKnown{}, err
}
2023-12-29 14:48:45 +00:00
if valid, err := validation.IsValid(result); !valid {
return NodeInfoWellKnown{}, err
2023-12-29 11:10:07 +00:00
}
2023-12-29 14:48:45 +00:00
return result, nil
}
func NodeInfoWellKnownUnmarshalJSON(data []byte) (NodeInfoWellKnown, error) {
p := fastjson.Parser{}
val, err := p.ParseBytes(data)
if err != nil {
return NodeInfoWellKnown{}, err
}
href := string(val.GetStringBytes("links", "0", "href"))
return NodeInfoWellKnown{Href: href}, nil
2023-12-29 11:10:07 +00:00
}
// Validate collects error strings in a slice and returns this
func (node NodeInfoWellKnown) Validate() []string {
var result []string
result = append(result, validation.ValidateNotEmpty(node.Href, "Href")...)
2024-01-13 15:22:49 +00:00
parsedURL, err := url.Parse(node.Href)
2023-12-29 11:10:07 +00:00
if err != nil {
result = append(result, err.Error())
return result
}
2024-01-13 15:22:49 +00:00
if parsedURL.Host == "" {
2023-12-29 11:10:07 +00:00
result = append(result, "Href has to be absolute")
}
2024-05-01 13:02:27 +00:00
result = append(result, validation.ValidateOneOf(parsedURL.Scheme, []any{"http", "https"}, "parsedURL.Scheme")...)
2023-12-29 11:10:07 +00:00
2024-01-13 15:22:49 +00:00
if parsedURL.RawQuery != "" {
2023-12-29 11:10:07 +00:00
result = append(result, "Href may not contain query")
}
return result
}
2023-12-29 14:48:45 +00:00
// ------------------------------------------------ NodeInfo ------------------------------------------------
// NodeInfo data type
// swagger:model
type NodeInfo struct {
SoftwareName SoftwareNameType
2023-12-29 14:48:45 +00:00
}
func NodeInfoUnmarshalJSON(data []byte) (NodeInfo, error) {
p := fastjson.Parser{}
val, err := p.ParseBytes(data)
if err != nil {
return NodeInfo{}, err
}
source := string(val.GetStringBytes("software", "name"))
result := NodeInfo{}
result.SoftwareName = SoftwareNameType(source)
2023-12-29 14:48:45 +00:00
return result, nil
}
func NewNodeInfo(body []byte) (NodeInfo, error) {
result, err := NodeInfoUnmarshalJSON(body)
if err != nil {
return NodeInfo{}, err
}
if valid, err := validation.IsValid(result); !valid {
return NodeInfo{}, err
}
return result, nil
}
// Validate collects error strings in a slice and returns this
func (node NodeInfo) Validate() []string {
var result []string
2024-05-01 13:46:03 +00:00
result = append(result, validation.ValidateNotEmpty(string(node.SoftwareName), "node.SoftwareName")...)
2024-05-01 13:02:27 +00:00
result = append(result, validation.ValidateOneOf(node.SoftwareName, KnownSourceTypes, "node.SoftwareName")...)
2023-12-29 14:48:45 +00:00
return result
}