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

feat(services): add tag-based query/params helper #92

Merged
merged 3 commits into from
Dec 20, 2020
Merged

Conversation

piksel
Copy link
Member

@piksel piksel commented Dec 5, 2020

This adds a format.PropKeyResolver helper that services can use to automatically implement the query/params serialization based on the config key tags.
Example:

import (
        "github.com/containrrr/shoutrrr/pkg/format"
	"github.com/containrrr/shoutrrr/pkg/types"
)

type Config struct {
	Title     string   `key:"subject" default:"Shoutrrr Notification"`
}

// Public version of GetURL that creates a new PropKeyResolver when accessed from outside the package
func (config *Config) GetURL() *url.URL {
	resolver := format.NewPropKeyResolver(config)
	return config.getURL(&resolver)
}

// Private version of GetURL that can use an instance of PropKeyResolver instead of rebuilding it's model from reflection
func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
	return &url.URL{
		Host:       "example",
		Scheme:     Scheme,
                // format.BuildQuery now requires a different interface,
                // which could either be a PropKeyResolver or a Config that implements the old ServiceConfig interface
		RawQuery:   format.BuildQuery(resolver),
	}
}

func (service *Service) Send(message string, params *types.Params) error {
        // Create a copy of the config this send
	config := service.config

        // Mutate the config with overrides from params
	if err := service.propKeyResolver.UpdateConfigFromParams(&config, params); err != nil {
		return err
	} else {
		return service.doSend(client, message, &config)
	}
}

Almost all services that uses query fields has been upgraded to use PropKeyResolver, and should automatically override the configuration keys when props are passed to the Send method.
This also means that services config no longer need to implement the Get(), Set() and QueryFields methods, so those along with standard.QuerylessConfig has been removed.

Fixes #78

@codecov-io
Copy link

codecov-io commented Dec 5, 2020

Codecov Report

Merging #92 (fd12d74) into master (2d29e9b) will decrease coverage by 5.17%.
The diff coverage is 55.20%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #92      +/-   ##
==========================================
- Coverage   56.57%   51.40%   -5.18%     
==========================================
  Files          52       52              
  Lines        1278     1142     -136     
==========================================
- Hits          723      587     -136     
+ Misses        526      524       -2     
- Partials       29       31       +2     
Impacted Files Coverage Δ
pkg/services/discord/discord_config.go 63.15% <ø> (ø)
pkg/services/gotify/gotify_config.go 0.00% <ø> (ø)
pkg/services/hangouts/hangouts_config.go 100.00% <ø> (ø)
pkg/services/join/join.go 0.00% <0.00%> (ø)
pkg/services/mattermost/mattermost_config.go 58.82% <ø> (ø)
pkg/services/pushbullet/pushbullet_config.go 54.54% <ø> (ø)
pkg/services/pushover/pushover.go 0.00% <0.00%> (ø)
pkg/services/rocketchat/rocketchat_config.go 69.23% <ø> (ø)
pkg/services/slack/slack.go 22.22% <0.00%> (+4.83%) ⬆️
pkg/services/standard/standard_failures.go 0.00% <0.00%> (ø)
... and 20 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2d29e9b...fd12d74. Read the comment docs.

@piksel piksel marked this pull request as ready for review December 6, 2020 17:48
@atighineanu
Copy link
Contributor

I've built from this PR. Indeed it doesn't show ":" preceeded by empty fields, but there is still a crash after token input.

@piksel
Copy link
Member Author

piksel commented Dec 9, 2020

@atighineanu I guess it's because of array index out of range?
The token needs to be entered as a comma-separated 3-part array ( aaaaaaaaaaaaa,bbbbbbbbbbbb,cccccccccccc ), there is nothing that tells the user that, but now at least it's possible to enter it.
I am looking into either using a custom Generator for Slack or make it possible to specify a minimum length for array fields so that the Basic generator handles them instead of crashing...

@piksel piksel changed the title feat(services): implement tag-based service composable feat(services): add tag-based query/params helper Dec 9, 2020
@piksel piksel linked an issue Dec 19, 2020 that may be closed by this pull request
@piksel piksel merged commit 895f8ae into master Dec 20, 2020
@piksel piksel deleted the feat/std-keyconf branch December 20, 2020 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Almost no services implements params overrides Basic generator doesn't work with all current services
4 participants