Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(48): Remove interfaces defined in subpackages of pkg/ #56

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/pawelWritesCode/gdutils/pkg/cache"
"github.com/pawelWritesCode/gdutils/pkg/debugger"
"github.com/pawelWritesCode/gdutils/pkg/formatter"
"github.com/pawelWritesCode/gdutils/pkg/httpctx"
"github.com/pawelWritesCode/gdutils/pkg/osutils"
"github.com/pawelWritesCode/gdutils/pkg/pathfinder"
"github.com/pawelWritesCode/gdutils/pkg/schema"
Expand All @@ -19,16 +18,16 @@ import (
// APIContext holds utility services for working with HTTP(s) API.
type APIContext struct {
// Debugger represents debugger.
Debugger debugger.Debugger
Debugger debuggable

// Cache is storage for data.
Cache cache.Cache
Cache cacheable

// RequestDoer is service that has ability to send HTTP(s) requests.
RequestDoer httpctx.RequestDoer
RequestDoer requestDoer

// TemplateEngine is entity that has ability to work with template values.
TemplateEngine template.Engine
TemplateEngine templateEngine

// SchemaValidators holds validators available to validate data against schemas.
SchemaValidators SchemaValidators
Expand All @@ -43,19 +42,19 @@ type APIContext struct {
TypeMappers TypeMappers

// fileRecognizer is entity that has ability to recognize file reference.
fileRecognizer osutils.FileRecognizer
fileRecognizer fileRecognizer
}

// Formatters is container for entities that know how to serialize and deserialize data.
type Formatters struct {
// JSON is entity that has ability to serialize and deserialize JSON bytes.
JSON formatter.Formatter
JSON serializable

// YAML is entity that has ability to serialize and deserialize YAML bytes.
YAML formatter.Formatter
YAML serializable

// XML is entity that has ability to serialize and deserialize XML bytes.
XML formatter.Formatter
XML serializable
}

// SchemaValidators is container for JSON schema validators.
Expand All @@ -71,28 +70,28 @@ type SchemaValidators struct {
// PathFinders is container for different data types pathfinders.
type PathFinders struct {
// JSON is entity that has ability to obtain data from bytes in JSON format.
JSON pathfinder.PathFinder
JSON pathFinder

// YAML is entity that has ability to obtain data from bytes in YAML format.
YAML pathfinder.PathFinder
YAML pathFinder

// XML is entity that has ability to obtain data from bytes in XML format.
XML pathfinder.PathFinder
XML pathFinder

// HTML is entity that has ability to obtain data from bytes in HTML format.
HTML pathfinder.PathFinder
HTML pathFinder
}

// TypeMappers is container for different data format mappers
type TypeMappers struct {
// JSON is entity that has ability to map underlying data type into JSON data type
JSON types.Mapper
JSON typeMapper

// YAML is entity that has ability to map underlying data type into YAML data type
YAML types.Mapper
YAML typeMapper

// GO is entity that has ability to map underlying data type into GO-like data type
GO types.Mapper
GO typeMapper
}

type CustomTransport struct {
Expand Down Expand Up @@ -150,7 +149,7 @@ func NewDefaultAPIContext(isDebug bool, jsonSchemaDir string) *APIContext {
}

// NewAPIContext returns *APIContext
func NewAPIContext(cli *http.Client, c cache.Cache, jv SchemaValidators, p PathFinders, f Formatters, t TypeMappers, d debugger.Debugger) *APIContext {
func NewAPIContext(cli *http.Client, c cacheable, jv SchemaValidators, p PathFinders, f Formatters, t TypeMappers, d debuggable) *APIContext {
fileRecognizer := osutils.NewOSFileRecognizer("file://", osutils.NewFileValidator())

return &APIContext{
Expand All @@ -173,22 +172,22 @@ func (apiCtx *APIContext) ResetState(isDebug bool) {
}

// SetDebugger sets new debugger for APIContext.
func (apiCtx *APIContext) SetDebugger(d debugger.Debugger) {
func (apiCtx *APIContext) SetDebugger(d debuggable) {
apiCtx.Debugger = d
}

// SetCache sets new Cache for APIContext.
func (apiCtx *APIContext) SetCache(c cache.Cache) {
// SetCache sets new cacheable for APIContext.
func (apiCtx *APIContext) SetCache(c cacheable) {
apiCtx.Cache = c
}

// SetRequestDoer sets new RequestDoer for APIContext.
func (apiCtx *APIContext) SetRequestDoer(r httpctx.RequestDoer) {
func (apiCtx *APIContext) SetRequestDoer(r requestDoer) {
apiCtx.RequestDoer = r
}

// SetTemplateEngine sets new template Engine for APIContext.
func (apiCtx *APIContext) SetTemplateEngine(t template.Engine) {
func (apiCtx *APIContext) SetTemplateEngine(t templateEngine) {
apiCtx.TemplateEngine = t
}

Expand All @@ -203,51 +202,51 @@ func (apiCtx *APIContext) SetSchemaReferenceValidator(j validator.SchemaValidato
}

// SetJSONPathFinder sets new JSON pathfinder for APIContext.
func (apiCtx *APIContext) SetJSONPathFinder(r pathfinder.PathFinder) {
func (apiCtx *APIContext) SetJSONPathFinder(r pathFinder) {
apiCtx.PathFinders.JSON = r
}

// SetYAMLPathFinder sets new YAML pathfinder for APIContext.
func (apiCtx *APIContext) SetYAMLPathFinder(r pathfinder.PathFinder) {
func (apiCtx *APIContext) SetYAMLPathFinder(r pathFinder) {
apiCtx.PathFinders.YAML = r
}

// SetXMLPathFinder sets new XML pathfinder for APIContext.
func (apiCtx *APIContext) SetXMLPathFinder(r pathfinder.PathFinder) {
func (apiCtx *APIContext) SetXMLPathFinder(r pathFinder) {
apiCtx.PathFinders.XML = r
}

// SetHTMLPathFinder sets new HTML pathfinder for APIContext.
func (apiCtx *APIContext) SetHTMLPathFinder(r pathfinder.PathFinder) {
func (apiCtx *APIContext) SetHTMLPathFinder(r pathFinder) {
apiCtx.PathFinders.HTML = r
}

// SetJSONFormatter sets new JSON formatter for APIContext.
func (apiCtx *APIContext) SetJSONFormatter(jf formatter.Formatter) {
func (apiCtx *APIContext) SetJSONFormatter(jf serializable) {
apiCtx.Formatters.JSON = jf
}

// SetYAMLFormatter sets new YAML formatter for APIContext.
func (apiCtx *APIContext) SetYAMLFormatter(yd formatter.Formatter) {
func (apiCtx *APIContext) SetYAMLFormatter(yd serializable) {
apiCtx.Formatters.YAML = yd
}

// SetXMLFormatter sets new XML formatter for APIContext.
func (apiCtx *APIContext) SetXMLFormatter(xf formatter.Formatter) {
func (apiCtx *APIContext) SetXMLFormatter(xf serializable) {
apiCtx.Formatters.XML = xf
}

// SetJSONTypeMapper sets new type mapper for JSON.
func (apiCtx *APIContext) SetJSONTypeMapper(c types.Mapper) {
func (apiCtx *APIContext) SetJSONTypeMapper(c typeMapper) {
apiCtx.TypeMappers.JSON = c
}

// SetYAMLTypeMapper sets new type mapper for YAML.
func (apiCtx *APIContext) SetYAMLTypeMapper(c types.Mapper) {
func (apiCtx *APIContext) SetYAMLTypeMapper(c typeMapper) {
apiCtx.TypeMappers.YAML = c
}

// SetGoTypeMapper sets new type mapper for Go.
func (apiCtx *APIContext) SetGoTypeMapper(c types.Mapper) {
func (apiCtx *APIContext) SetGoTypeMapper(c typeMapper) {
apiCtx.TypeMappers.GO = c
}
28 changes: 14 additions & 14 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
//
// No matter which way you choose, you can inject your custom services afterwards with one of available setters:
//
// func (apiCtx *APIContext) SetDebugger(d debugger.Debugger)
// func (apiCtx *APIContext) SetCache(c cache.Cache)
// func (apiCtx *APIContext) SetRequestDoer(r httpctx.RequestDoer)
// func (apiCtx *APIContext) SetTemplateEngine(t template.Engine)
// func (apiCtx *APIContext) SetDebugger(d debuggable)
// func (apiCtx *APIContext) SetCache(c cacheable)
// func (apiCtx *APIContext) SetRequestDoer(r requestDoer)
// func (apiCtx *APIContext) SetTemplateEngine(t templateEngine)
// func (apiCtx *APIContext) SetSchemaStringValidator(j validator.SchemaValidator)
// func (apiCtx *APIContext) SetSchemaReferenceValidator(j validator.SchemaValidator)
// func (apiCtx *APIContext) SetJSONPathFinder(r pathfinder.PathFinder)
// func (apiCtx *APIContext) SetJSONFormatter(jf formatter.Formatter)
// func (apiCtx *APIContext) SetXMLPathFinder(r pathfinder.PathFinder)
// func (apiCtx *APIContext) SetXMLFormatter(xf formatter.Formatter)
// func (apiCtx *APIContext) SetYAMLFormatter(yd formatter.Formatter)
// func (apiCtx *APIContext) SetYAMLPathFinder(r pathfinder.PathFinder)
// func (apiCtx *APIContext) SetHTMLPathFinder(r pathfinder.PathFinder)
// func (apiCtx *APIContext) SetJSONTypeMapper(c types.Checker)
// func (apiCtx *APIContext) SetYAMLTypeMapper(c types.Checker)
// func (apiCtx *APIContext) SetGoTypeMapper(c types.Checker)
// func (apiCtx *APIContext) SetJSONPathFinder(r pathFinder)
// func (apiCtx *APIContext) SetJSONFormatter(jf serializable)
// func (apiCtx *APIContext) SetXMLPathFinder(r pathFinder)
// func (apiCtx *APIContext) SetXMLFormatter(xf serializable)
// func (apiCtx *APIContext) SetYAMLPathFinder(r pathFinder)
// func (apiCtx *APIContext) SetYAMLFormatter(yd serializable)
// func (apiCtx *APIContext) SetHTMLPathFinder(r pathFinder)
// func (apiCtx *APIContext) SetJSONTypeMapper(c typeMapper)
// func (apiCtx *APIContext) SetYAMLTypeMapper(c typeMapper)
// func (apiCtx *APIContext) SetGoTypeMapper(c typeMapper)
//
// Those services will be used in utility methods and can be accessed directly if needed (to use in any custom methods).
// For example, if you want to use your own debugger - because default one is not suitable for you, create your own struct,
Expand Down
79 changes: 79 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package gdutils

import (
"net/http"

"github.com/pawelWritesCode/gdutils/pkg/osutils"
"github.com/pawelWritesCode/gdutils/pkg/types"
)

// cacheable represents ability to store/retrieve arbitrary values.
type cacheable interface {
// Save preserve provided value under given key.
Save(key string, value any)

// GetSaved retrieve value from given key.
GetSaved(key string) (any, error)

// Reset turns cache into init state - clears all entries.
Reset()

// All returns all cache entries.
All() map[string]any
}

// debuggable defines desired debugger behaviour.
type debuggable interface {
// Print prints provided info.
Print(info string)

// IsOn tells whether debugging mode is activated.
IsOn() bool

// TurnOn turns on debugging mode.
TurnOn()

// TurnOff turns off debugging mode.
TurnOff()

// Reset resets debugging mode to init state.
Reset(isOn bool)
}

// serializable describes ability to serialize and deserialize data
type serializable interface {
// Deserialize deserializes data on v
Deserialize(data []byte, v any) error

// Serialize serializes v
Serialize(v any) ([]byte, error)
}

// requestDoer describes ability to make HTTP(s) requests.
type requestDoer interface {
Do(req *http.Request) (*http.Response, error)
}

// fileRecognizer describes entity that has ability to find file reference in input
type fileRecognizer interface {
// Recognize recognizes file reference in provided input
Recognize(input string) (osutils.FileReference, bool)
}

// pathFinder describes ability to obtain node(s) from data in fixed data format
type pathFinder interface {
// Find obtains data from bytes according to given expression
Find(expr string, bytes []byte) (any, error)
}

// templateEngine is entity that has ability to work with templates.
type templateEngine interface {
// Replace replaces template values using provided storage.
Replace(templateValue string, storage map[string]any) (string, error)
}

// typeMapper represents entity that has ability to map data's type into corresponding types.DataType of given format.
type typeMapper interface {
// Map maps data type.
Map(data any) types.DataType
}
16 changes: 0 additions & 16 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Package cache holds definition of Cache used for storing and retrieving data.
package cache

import (
Expand All @@ -7,18 +6,3 @@ import (

// ErrMissingKey occurs when cache doesn't have any value under given key.
var ErrMissingKey = errors.New("missing key")

// Cache is entity that has ability to store/retrieve arbitrary values.
type Cache interface {
// Save preserve provided value under given key.
Save(key string, value any)

// GetSaved retrieve value from given key.
GetSaved(key string) (any, error)

// Reset turns cache into init state - clears all entries.
Reset()

// All returns all cache entries.
All() map[string]any
}
19 changes: 0 additions & 19 deletions pkg/debugger/debugger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Package debugger holds definition of Debugger.
package debugger

import (
Expand All @@ -11,24 +10,6 @@ import (
"github.com/pawelWritesCode/df"
)

// Debugger represents debugger.
type Debugger interface {
// Print prints provided info.
Print(info string)

// IsOn tells whether debugging mode is activated.
IsOn() bool

// TurnOn turns on debugging mode.
TurnOn()

// TurnOff turns off debugging mode.
TurnOff()

// Reset resets debugging mode to init state.
Reset(isOn bool)
}

// DebuggerService is utility tool for debugging
type DebuggerService struct {

Expand Down
9 changes: 0 additions & 9 deletions pkg/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ import (
"gopkg.in/yaml.v2"
)

// Formatter describes ability to serialize and deserialize data
type Formatter interface {
// Deserialize deserializes data on v
Deserialize(data []byte, v any) error

// Serialize serializes v
Serialize(v any) ([]byte, error)
}

// JSONFormatter is entity that has ability to work with JSON format
type JSONFormatter struct{}

Expand Down
15 changes: 0 additions & 15 deletions pkg/httpctx/httpctx.go

This file was deleted.

Loading
Loading