File: formatbase.go

package info (click to toggle)
golang-code.forgejo-f3-gof3 3.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,952 kB
  • sloc: sh: 100; makefile: 65
file content (99 lines) | stat: -rw-r--r-- 2,237 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT

package f3

import (
	"encoding/json"
	"strings"

	"code.forgejo.org/f3/gof3/v3/util"
)

type Interface interface {
	GetID() string
	GetName() string
	SetID(id string)
	IsNil() bool
	GetReferences() References
	ToReference() *Reference
	Clone() Interface
}

type ReferenceInterface interface {
	Get() string
	Set(reference string)
	GetIDAsString() string
	GetIDAsInt() int64
}

type Reference struct {
	ID string
}

func (r *Reference) Get() string {
	return r.ID
}

func (r *Reference) Set(reference string) {
	r.ID = reference
}

func (r *Reference) GetIDAsInt() int64 {
	return util.ParseInt(r.GetIDAsString())
}

func (r *Reference) GetIDAsString() string {
	s := strings.Split(r.ID, "/")
	return s[len(s)-1]
}

type References []ReferenceInterface

func (r *Reference) UnmarshalJSON(b []byte) error {
	return json.Unmarshal(b, &r.ID)
}

func (r Reference) MarshalJSON() ([]byte, error) {
	return json.Marshal(r.ID)
}

func (r *Reference) GetID() string             { return r.ID }
func (r *Reference) SetID(id string)           { r.ID = id }
func (r *Reference) IsNil() bool               { return r == nil || r.ID == "0" || r.ID == "" }
func (r Reference) Equal(other Reference) bool { return r.ID == other.ID }

func NewReferences() References {
	return make([]ReferenceInterface, 0, 1)
}

func NewReference(id string) *Reference {
	r := &Reference{}
	r.SetID(id)
	return r
}

type Common struct {
	Index Reference `json:"index"`
}

func (c *Common) GetID() string             { return c.Index.GetID() }
func (c *Common) GetName() string           { return c.GetID() }
func (c *Common) SetID(id string)           { c.Index.SetID(id) }
func (c *Common) IsNil() bool               { return c == nil || c.Index.IsNil() }
func (c *Common) GetReferences() References { return NewReferences() }
func (c *Common) ToReference() *Reference   { return &c.Index }
func (c Common) Equal(other Common) bool    { return true }

var Nil = &Common{}

func NewCommon(id string) Common {
	return Common{Index: *NewReference(id)}
}

func (c *Common) Clone() Interface {
	clone := &Common{}
	*clone = *c
	return clone
}