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

Refactoring/externalized html #234

Merged
merged 5 commits into from
Feb 19, 2019
Merged

Refactoring/externalized html #234

merged 5 commits into from
Feb 19, 2019

Conversation

ziflex
Copy link
Member

@ziflex ziflex commented Feb 16, 2019

No description provided.

@ziflex ziflex added area/runtime Runtime issue area/stdlib Standard library issue area/drivers/cdp Cdp driver area/drivers/http Http driver area/drivers HTML drivers labels Feb 16, 2019
@ziflex ziflex requested a review from 3timeslazy February 16, 2019 02:06
@codecov
Copy link

codecov bot commented Feb 16, 2019

Codecov Report

Merging #234 into master will decrease coverage by 0.3%.
The diff coverage is 12%.

@@           Coverage Diff            @@
##           master    #234     +/-   ##
========================================
- Coverage    59.2%   58.9%   -0.3%     
========================================
  Files         181     179      -2     
  Lines        5234    5262     +28     
========================================
- Hits         3098    3097      -1     
- Misses       1791    1820     +29     
  Partials      345     345


switch result.Type() {
case types.Object:
if resultType == types.Object {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe switch-case would be better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe :)

case types.Array:
return input.Copy(), nil
case types.Object:
obj, ok := input.(*Object)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if input.Type() returns types.Object, can we get ok == false here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, we can switch input.(type), not input.Type()

It will looks like:

switch value := input.(type) {
	case Boolean,
		Int,
		Float,
		String,
		DateTime:

		return NewArrayWith(value), nil
	case *Array:
		return value.Copy(), nil
	case *Object:
		arr := NewArray(int(value.Length()))

		value.ForEach(func(value core.Value, key string) bool {
			arr.Push(value)

			return true
		})

		return value, nil
	case core.Iterable:
		iterator, err := value.Iterate(ctx)

		if err != nil {
			return None, err
		}

		arr := NewArray(10)

		for {
			val, _, err := iterator.Next(ctx)

			if err != nil {
				return None, err
			}

			if val == None {
				break
			}

			arr.Push(val)
		}

		return arr, nil
	default:
		return NewArray(0), nil
	}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point. But if a value has type of type.Object but isn't values.Object it means that someone messed up with types.
The reason why I created Type interface is that I wanted to avoid type assertion as much as I could because of its possible (in some cases) negative impact on performance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But ideally, we need to write benchmarks to see the impact. It seems in recent version of Go, it has been improved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will be main performance problem, but OK :)

obj, ok := attrs.(*Object)
return NewArrayWith(input), nil
case types.Array:
return input.Copy(), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referring to #8 (comment), we should return only new instances. Think we should use Clone instead of Copy.

It can shoot here:

func ToArray(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
return values.ToArray(ctx, args[0])
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I do not think it's valid here. We create a new array with the same content. I think it's an intended behavior, isn't it?

@ziflex ziflex merged commit 34c8c02 into master Feb 19, 2019
@ziflex ziflex deleted the refactoring/externalized-html branch February 20, 2019 01:19
3timeslazy pushed a commit to 3timeslazy/ferret that referenced this pull request Apr 10, 2019
* Externalized HTML drivers

* Fixed unit tests

* Updated logging

* Added support to set default driver

* Updated GetIn and SetIn helpers
3timeslazy pushed a commit to 3timeslazy/ferret that referenced this pull request Apr 10, 2019
* Externalized HTML drivers

* Fixed unit tests

* Updated logging

* Added support to set default driver

* Updated GetIn and SetIn helpers
3timeslazy pushed a commit to 3timeslazy/ferret that referenced this pull request Apr 10, 2019
* Externalized HTML drivers

* Fixed unit tests

* Updated logging

* Added support to set default driver

* Updated GetIn and SetIn helpers
3timeslazy pushed a commit to 3timeslazy/ferret that referenced this pull request Apr 10, 2019
* Externalized HTML drivers

* Fixed unit tests

* Updated logging

* Added support to set default driver

* Updated GetIn and SetIn helpers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/drivers/cdp Cdp driver area/drivers/http Http driver area/drivers HTML drivers area/runtime Runtime issue area/stdlib Standard library issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants